RoutineTemplate.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\CacheService;
  5. use crmeb\services\FormBuilder as Form;
  6. use crmeb\services\UtilService as Util;
  7. use crmeb\services\JsonService as Json;
  8. use think\facade\Route as Url;
  9. use app\admin\model\routine\RoutineTemplate as RoutineTemplateModel;
  10. use crmeb\services\MiniProgramService;
  11. /**
  12. * 小程序模板消息控制器
  13. * Class RoutineTemplate
  14. * @package app\admin\controller\routine
  15. */
  16. class RoutineTemplate extends AuthController
  17. {
  18. public function index()
  19. {
  20. $where = Util::getMore([
  21. ['name', ''],
  22. ['status', '']
  23. ], $this->request);
  24. $this->assign('where', $where);
  25. $this->assign(RoutineTemplateModel::SystemPage($where));
  26. return $this->fetch();
  27. }
  28. /**
  29. * 添加模板消息
  30. * @return mixed
  31. */
  32. public function create()
  33. {
  34. $f = array();
  35. $f[] = Form::input('tempkey', '模板编号');
  36. $f[] = Form::input('tempid', '模板ID');
  37. $f[] = Form::input('name', '模板名');
  38. $f[] = Form::input('content', '回复内容')->type('textarea');
  39. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  40. $form = Form::make_post_form('添加模板消息', $f, Url::buildUrl('save'));
  41. $this->assign(compact('form'));
  42. return $this->fetch('public/form-builder');
  43. }
  44. public function save()
  45. {
  46. $data = Util::postMore([
  47. 'tempkey',
  48. 'tempid',
  49. 'name',
  50. 'content',
  51. ['status', 0]
  52. ]);
  53. if ($data['tempkey'] == '') return Json::fail('请输入模板编号');
  54. if ($data['tempkey'] != '' && RoutineTemplateModel::be($data['tempkey'], 'tempkey'))
  55. return Json::fail('请输入模板编号已存在,请重新输入');
  56. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  57. if ($data['name'] == '') return Json::fail('请输入模板名');
  58. if ($data['content'] == '') return Json::fail('请输入回复内容');
  59. $data['add_time'] = time();
  60. $data['type'] = 0;
  61. RoutineTemplateModel::create($data);
  62. CacheService::clear();
  63. return Json::successful('添加模板消息成功!');
  64. }
  65. /**
  66. * 编辑模板消息
  67. * @param $id
  68. * @return mixed|\think\response\Json|void
  69. */
  70. public function edit($id)
  71. {
  72. if (!$id) return $this->failed('数据不存在');
  73. $product = RoutineTemplateModel::get($id);
  74. if (!$product) return Json::fail('数据不存在!');
  75. $f = array();
  76. $f[] = Form::input('tempkey', '模板编号', $product->getData('tempkey'))->disabled(1);
  77. $f[] = Form::input('name', '模板名', $product->getData('name'))->disabled(1);
  78. $f[] = Form::input('tempid', '模板ID', $product->getData('tempid'));
  79. $f[] = Form::radio('status', '状态', $product->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  80. $form = Form::make_post_form('编辑模板消息', $f, Url::buildUrl('update', compact('id')));
  81. $this->assign(compact('form'));
  82. return $this->fetch('public/form-builder');
  83. }
  84. public function update($id)
  85. {
  86. $data = Util::postMore([
  87. 'tempid',
  88. ['status', 0]
  89. ]);
  90. if ($data['tempid'] == '') return Json::fail('请输入模板ID');
  91. if (!$id) return $this->failed('数据不存在');
  92. $product = RoutineTemplateModel::get($id);
  93. if (!$product) return Json::fail('数据不存在!');
  94. RoutineTemplateModel::edit($data, $id);
  95. CacheService::clear();
  96. return Json::successful('修改成功!');
  97. }
  98. /**
  99. * 删除模板消息
  100. * @param $id
  101. * @return \think\response\Json
  102. */
  103. public function delete($id)
  104. {
  105. if (!$id) return Json::fail('数据不存在!');
  106. if (!RoutineTemplateModel::del($id))
  107. return Json::fail(RoutineTemplateModel::getErrorInfo('删除失败,请稍候再试!'));
  108. else {
  109. CacheService::clear();
  110. return Json::successful('删除成功!');
  111. }
  112. }
  113. /**
  114. * 同步订阅消息
  115. * @return mixed
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. */
  120. public function syncSubscribe()
  121. {
  122. $all = RoutineTemplateModel::where(['status' => 1, 'type' => 0])->select();
  123. $errData = [];
  124. if ($all) {
  125. $time = time();
  126. foreach ($all as $template) {
  127. if ($template['tempkey']) {
  128. if (!isset($template['kid'])) {
  129. return Json::fail('数据库模版表(template_message)缺少字段:kid');
  130. }
  131. $works = [];
  132. try {
  133. $works = MiniProgramService::getSubscribeTemplateKeyWords($template['tempkey']);
  134. } catch (\Throwable $e) {
  135. $errData[] = $template['name'] . '获取关键词列表失败';
  136. }
  137. $kid = [];
  138. if ($works) {
  139. $works = array_combine(array_column($works, 'name'), $works);
  140. $content = is_array($template['content']) ? $template['content'] : explode("\n", $template['content']);
  141. foreach ($content as $c) {
  142. $name = explode('{{', $c)[0] ?? '';
  143. if ($name && isset($works[$name])) {
  144. $kid[] = $works[$name]['kid'];
  145. }
  146. }
  147. }
  148. if ($kid && isset($template['kid']) && !$template['kid']) {
  149. $tempid = '';
  150. try {
  151. $tempid = MiniProgramService::addSubscribeTemplate($template['tempkey'], $kid, $template['name']);
  152. } catch (\Throwable $e) {
  153. $errData[] = $template['name'] . '添加订阅消息模版失败';
  154. }
  155. if ($tempid != $template['tempid']) {
  156. RoutineTemplateModel::update(['tempid' => $tempid, 'kid' => json_encode($kid), 'add_time' => $time], ['id' => $template['id']]);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. $msg = $errData ? implode('\n', $errData) : '同步成功';
  163. return Json::success($msg);
  164. }
  165. }