StoreServiceSpeechcraftServices.php 3.6 KB

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