Keys.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\controller;
  16. use app\wechat\model\WechatKeys;
  17. use app\wechat\service\WechatService;
  18. use think\admin\Controller;
  19. use think\admin\helper\QueryHelper;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 回复规则管理
  23. * Class Keys
  24. * @package app\wechat\controller
  25. */
  26. class Keys extends Controller
  27. {
  28. /**
  29. * 消息类型
  30. * @var array
  31. */
  32. public $types = [
  33. 'text' => '文字', 'news' => '图文', 'image' => '图片', 'music' => '音乐',
  34. 'video' => '视频', 'voice' => '语音', 'customservice' => '转客服',
  35. ];
  36. /**
  37. * 回复规则管理
  38. * @auth true
  39. * @menu true
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function index()
  45. {
  46. // 关键字二维码生成
  47. if ($this->request->get('action') === 'qrc') try {
  48. $wechat = WechatService::WeChatQrcode();
  49. $result = $wechat->create($this->request->get('keys', ''));
  50. $this->success('生成二维码成功!', "javascript:$.previewImage('{$wechat->url($result['ticket'])}')");
  51. } catch (HttpResponseException $exception) {
  52. throw $exception;
  53. } catch (\Exception $exception) {
  54. $this->error("生成二维码失败,请稍候再试!<br> {$exception->getMessage()}");
  55. }
  56. // 数据列表分页处理
  57. $this->type = input('get.type', 'index');
  58. WechatKeys::mQuery()->layTable(function () {
  59. $this->title = '回复规则管理';
  60. }, function (QueryHelper $query) {
  61. $query->whereNotIn('keys', ['subscribe', 'default']);
  62. $query->like('keys,type#mtype')->dateBetween('create_at');
  63. $query->where(['status' => intval($this->type === 'index')]);
  64. });
  65. }
  66. /**
  67. * 列表数据处理
  68. * @param array $data
  69. */
  70. protected function _index_page_filter(array &$data)
  71. {
  72. foreach ($data as &$vo) {
  73. $vo['type'] = $this->types[$vo['type']] ?? $vo['type'];
  74. $vo['qrc'] = sysuri('wechat/keys/index') . "?action=qrc&keys={$vo['keys']}";
  75. }
  76. }
  77. /**
  78. * 添加回复规则
  79. * @auth true
  80. */
  81. public function add()
  82. {
  83. $this->_applyFormToken();
  84. $this->title = '添加回复规则';
  85. WechatKeys::mForm('form');
  86. }
  87. /**
  88. * 编辑回复规则
  89. * @auth true
  90. */
  91. public function edit()
  92. {
  93. $this->_applyFormToken();
  94. $this->title = '编辑回复规则';
  95. WechatKeys::mForm('form');
  96. }
  97. /**
  98. * 修改规则状态
  99. * @auth true
  100. */
  101. public function state()
  102. {
  103. $this->_applyFormToken();
  104. WechatKeys::mSave($this->_vali([
  105. 'status.in:0,1' => '状态值范围异常!',
  106. 'status.require' => '状态值不能为空!',
  107. ]));
  108. }
  109. /**
  110. * 删除回复规则
  111. * @auth true
  112. */
  113. public function remove()
  114. {
  115. $this->_applyFormToken();
  116. WechatKeys::mDelete();
  117. }
  118. /**
  119. * 配置订阅回复
  120. * @auth true
  121. * @menu true
  122. */
  123. public function subscribe()
  124. {
  125. $this->_applyFormToken();
  126. $this->title = '编辑订阅回复规则';
  127. WechatKeys::mForm('form', 'keys', [], ['keys' => 'subscribe']);
  128. }
  129. /**
  130. * 配置默认回复
  131. * @auth true
  132. * @menu true
  133. */
  134. public function defaults()
  135. {
  136. $this->_applyFormToken();
  137. $this->title = '编辑默认回复规则';
  138. WechatKeys::mForm('form', 'keys', [], ['keys' => 'default']);
  139. }
  140. /**
  141. * 添加数据处理
  142. * @param array $data
  143. */
  144. protected function _form_filter(array &$data)
  145. {
  146. if ($this->request->isPost()) {
  147. $map = [['keys', '=', $data['keys']], ['id', '<>', $data['id'] ?? 0]];
  148. if (WechatKeys::mk()->where($map)->count() > 0) {
  149. $this->error('该关键字已经存在!');
  150. }
  151. $data['content'] = strip_tags($data['content'] ?? '', '<a>');
  152. } elseif ($this->request->isGet()) {
  153. $public = dirname($this->request->basefile(true));
  154. $this->defaultImage = "{$public}/static/theme/img/image.png";
  155. }
  156. }
  157. /**
  158. * 表单结果处理
  159. * @param boolean $result
  160. */
  161. protected function _form_result(bool $result)
  162. {
  163. if ($result !== false) {
  164. $iskeys = in_array(input('keys'), ['subscribe', 'default']);
  165. $location = $iskeys ? 'javascript:$.form.reload()' : 'javascript:history.back()';
  166. $this->success('恭喜, 关键字保存成功!', $location);
  167. } else {
  168. $this->error('关键字保存失败, 请稍候再试!');
  169. }
  170. }
  171. }