StoreServiceSpeechcraftServices.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\kefu\service;
  12. use app\dao\service\StoreServiceSpeechcraftDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder;
  16. use think\Model;
  17. /**
  18. * 话术
  19. * Class StoreServiceSpeechcraftServices
  20. * @package app\services\kefu\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. foreach ($list as &$item) {
  46. if (!$item['cate_name']) $item['cate_name'] = '系统默认';
  47. }
  48. $count = $this->dao->count($where);
  49. return compact('list', 'count');
  50. }
  51. /**
  52. * 创建form表单
  53. * @return mixed
  54. */
  55. public function createForm()
  56. {
  57. return create_form('添加话术', $this->speechcraftForm(), $this->url('/app/wechat/speechcraft'), 'POST');
  58. }
  59. /**
  60. * @param int $id
  61. * @return array
  62. * @throws \FormBuilder\Exception\FormBuilderException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function updateForm(int $id)
  68. {
  69. $info = $this->dao->get($id);
  70. if (!$info) {
  71. throw new AdminException(400461);
  72. }
  73. return create_form('编辑话术', $this->speechcraftForm($info->toArray()), $this->url('/app/wechat/speechcraft/' . $id), 'PUT');
  74. }
  75. /**
  76. * @param array $infoData
  77. * @return mixed
  78. */
  79. protected function speechcraftForm(array $infoData = [])
  80. {
  81. /** @var StoreServiceSpeechcraftCateServices $services */
  82. $services = app()->make(StoreServiceSpeechcraftCateServices::class);
  83. $cateList = $services->getCateList(['owner_id' => 0, 'type' => 1]);
  84. $data = [];
  85. $data[] = ['value' => 0, 'label' => '默认分类'];
  86. foreach ($cateList['data'] as $item) {
  87. $data[] = ['value' => $item['id'], 'label' => $item['name']];
  88. }
  89. $form[] = FormBuilder::select('cate_id', '话术分类', $infoData['cate_id'] ?? '')->setOptions($data);
  90. $form[] = FormBuilder::textarea('title', '话术标题', $infoData['title'] ?? '')->required();
  91. $form[] = FormBuilder::textarea('message', '话术内容', $infoData['message'] ?? '')->required();
  92. $form[] = FormBuilder::number('sort', '排序', (int)($infoData['sort'] ?? 0));
  93. return $form;
  94. }
  95. }