You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreServiceSpeechcraftServices.php 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\services\message\service;
  3. use app\dao\message\service\StoreServiceSpeechcraftDao;
  4. use app\services\BaseServices;
  5. use crmeb\services\FormBuilder;
  6. use think\exception\ValidateException;
  7. use think\Model;
  8. /**
  9. * 话术
  10. * Class StoreServiceSpeechcraftServices
  11. * @package app\services\message\service
  12. * @method array|Model|null get($id, ?array $field = [], ?array $with = []) 获取一条数据
  13. * @method update($id, array $data, ?string $key = null) 更新数据
  14. */
  15. class StoreServiceSpeechcraftServices extends BaseServices
  16. {
  17. /**
  18. * StoreServiceSpeechcraftServices constructor.
  19. * @param StoreServiceSpeechcraftDao $dao
  20. */
  21. public function __construct(StoreServiceSpeechcraftDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * @param array $where
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function getSpeechcraftList(array $where)
  33. {
  34. [$page, $limit] = $this->getPageValue();
  35. $list = $this->dao->getSpeechcraftList($where, $page, $limit);
  36. $count = $this->dao->count($where);
  37. return compact('list', 'count');
  38. }
  39. /**
  40. * 创建form表单
  41. * @param int $cate_id
  42. * @return mixed
  43. */
  44. public function createForm(int $cate_id = 0)
  45. {
  46. return create_form('添加话术', $this->speechcraftForm([], $cate_id), $this->url('/app/wechat/speechcraft'), 'POST');
  47. }
  48. /**
  49. * @param int $id
  50. * @return array
  51. * @throws \FormBuilder\Exception\FormBuilderException
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function updateForm(int $id)
  57. {
  58. $info = $this->dao->get($id);
  59. if (!$info) {
  60. throw new ValidateException('您修改的话术内容不存在');
  61. }
  62. return create_form('编辑话术', $this->speechcraftForm($info->toArray()), $this->url('/app/wechat/speechcraft/' . $id), 'PUT');
  63. }
  64. /**
  65. * @param array $infoData
  66. * @param int $cate_id
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. protected function speechcraftForm(array $infoData = [], int $cate_id = 0)
  73. {
  74. /** @var StoreServiceSpeechcraftCateServices $services */
  75. $services = app()->make(StoreServiceSpeechcraftCateServices::class);
  76. $cateList = $services->getCateList(['owner_id' => 0, 'type' => 1, 'group' => 1]);
  77. $data = [];
  78. foreach ($cateList['data'] as $item) {
  79. $data[] = ['value' => $item['id'], 'label' => $item['name']];
  80. }
  81. $form[] = FormBuilder::select('cate_id', '话术分类', $infoData['cate_id'] ?? $cate_id)->setOptions($data);
  82. $form[] = FormBuilder::textarea('title', '话术标题', $infoData['title'] ?? '');
  83. $form[] = FormBuilder::textarea('message', '话术内容', $infoData['message'] ?? '')->required();
  84. $form[] = FormBuilder::number('sort', '排序', (int)($infoData['sort'] ?? 0))->min(0);
  85. return $form;
  86. }
  87. }