Keys.php 5.3 KB

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