Keys.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\exception\HttpResponseException;
  20. /**
  21. * 回复规则管理
  22. * Class Keys
  23. * @package app\wechat\controller
  24. */
  25. class Keys extends Controller
  26. {
  27. /**
  28. * 消息类型
  29. * @var array
  30. */
  31. public $types = [
  32. 'text' => '文字', 'news' => '图文', 'image' => '图片', 'music' => '音乐',
  33. 'video' => '视频', 'voice' => '语音', 'customservice' => '转客服',
  34. ];
  35. /**
  36. * 回复规则管理
  37. * @auth true
  38. * @menu true
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index()
  44. {
  45. // 关键字二维码生成
  46. if ($this->request->get('action') === 'qrc') try {
  47. $wechat = WechatService::WeChatQrcode();
  48. $result = $wechat->create($this->request->get('keys', ''));
  49. $this->success('生成二维码成功!', "javascript:$.previewImage('{$wechat->url($result['ticket'])}')");
  50. } catch (HttpResponseException $exception) {
  51. throw $exception;
  52. } catch (\Exception $exception) {
  53. $this->error("生成二维码失败,请稍候再试!<br> {$exception->getMessage()}");
  54. }
  55. // 数据列表分页处理
  56. $this->title = '回复规则管理';
  57. $query = WechatKeys::mQuery()->whereNotIn('keys', ['subscribe', 'default']);
  58. $query->equal('status')->like('keys,type')->dateBetween('create_at')->order('sort desc,id desc')->page();
  59. }
  60. /**
  61. * 列表数据处理
  62. * @param array $data
  63. */
  64. protected function _index_page_filter(array &$data)
  65. {
  66. foreach ($data as &$vo) {
  67. $vo['type'] = $this->types[$vo['type']] ?? $vo['type'];
  68. $vo['qrc'] = sysuri('wechat/keys/index') . "?action=qrc&keys={$vo['keys']}";
  69. }
  70. }
  71. /**
  72. * 添加回复规则
  73. * @auth true
  74. */
  75. public function add()
  76. {
  77. $this->_applyFormToken();
  78. $this->title = '添加回复规则';
  79. WechatKeys::mForm('form');
  80. }
  81. /**
  82. * 编辑回复规则
  83. * @auth true
  84. */
  85. public function edit()
  86. {
  87. $this->_applyFormToken();
  88. $this->title = '编辑回复规则';
  89. WechatKeys::mForm('form');
  90. }
  91. /**
  92. * 修改规则状态
  93. * @auth true
  94. */
  95. public function state()
  96. {
  97. $this->_applyFormToken();
  98. WechatKeys::mSave($this->_vali([
  99. 'status.in:0,1' => '状态值范围异常!',
  100. 'status.require' => '状态值不能为空!',
  101. ]));
  102. }
  103. /**
  104. * 删除回复规则
  105. * @auth true
  106. */
  107. public function remove()
  108. {
  109. $this->_applyFormToken();
  110. WechatKeys::mDelete();
  111. }
  112. /**
  113. * 配置订阅回复
  114. * @auth true
  115. * @menu true
  116. */
  117. public function subscribe()
  118. {
  119. $this->_applyFormToken();
  120. $this->title = '编辑订阅回复规则';
  121. WechatKeys::mForm('form', 'keys', [], ['keys' => 'subscribe']);
  122. }
  123. /**
  124. * 配置默认回复
  125. * @auth true
  126. * @menu true
  127. */
  128. public function defaults()
  129. {
  130. $this->_applyFormToken();
  131. $this->title = '编辑默认回复规则';
  132. WechatKeys::mForm('form', 'keys', [], ['keys' => 'default']);
  133. }
  134. /**
  135. * 添加数据处理
  136. * @param array $data
  137. */
  138. protected function _form_filter(array &$data)
  139. {
  140. if ($this->request->isPost()) {
  141. $map = [['keys', '=', $data['keys']], ['id', '<>', $data['id'] ?? 0]];
  142. if (WechatKeys::mk()->where($map)->count() > 0) {
  143. $this->error('该关键字已经存在!');
  144. }
  145. $data['content'] = strip_tags($data['content'] ?? '', '<a>');
  146. } elseif ($this->request->isGet()) {
  147. $public = dirname($this->request->basefile(true));
  148. $this->defaultImage = "{$public}/static/theme/img/image.png";
  149. }
  150. }
  151. /**
  152. * 表单结果处理
  153. * @param boolean $result
  154. */
  155. protected function _form_result(bool $result)
  156. {
  157. if ($result !== false) {
  158. $iskeys = in_array(input('keys'), ['subscribe', 'default']);
  159. $location = $iskeys ? 'javascript:$.form.reload()' : 'javascript:history.back()';
  160. $this->success('恭喜, 关键字保存成功!', $location);
  161. } else {
  162. $this->error('关键字保存失败, 请稍候再试!');
  163. }
  164. }
  165. }