Keys.php 6.1 KB

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