Keys.php 6.1 KB

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