StoreServiceSpeechcraftServices.php 3.7 KB

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