Keys.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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 library\Controller;
  17. use think\Db;
  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. protected $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\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. * @throws \think\exception\PDOException
  48. */
  49. public function index()
  50. {
  51. // 关键字二维码生成
  52. if ($this->request->get('action') === 'qrc') {
  53. try {
  54. $wechat = WechatService::WeChatQrcode();
  55. $result = $wechat->create($this->request->get('keys', ''));
  56. $this->success('生成二维码成功!', "javascript:$.previewImage('{$wechat->url($result['ticket'])}')");
  57. } catch (HttpResponseException $exception) {
  58. throw $exception;
  59. } catch (\Exception $e) {
  60. $this->error("生成二维码失败,请稍候再试!<br> {$e->getMessage()}");
  61. }
  62. }
  63. // 关键字列表显示
  64. $this->title = '回复规则管理';
  65. $query = $this->_query($this->table)->like('keys,type')->equal('status')->dateBetween('create_at');
  66. $query->whereNotIn('keys', ['subscribe', 'default'])->order('sort desc,id desc')->page();
  67. }
  68. /**
  69. * 列表数据处理
  70. * @param array $data
  71. */
  72. protected function _index_page_filter(&$data)
  73. {
  74. foreach ($data as &$vo) {
  75. $vo['qrc'] = url('@wechat/keys/index') . "?action=qrc&keys={$vo['keys']}";
  76. $vo['type'] = isset($this->types[$vo['type']]) ? $this->types[$vo['type']] : $vo['type'];
  77. }
  78. }
  79. /**
  80. * 添加关键字
  81. * @auth true
  82. */
  83. public function add()
  84. {
  85. $this->applyCsrfToken();
  86. $this->title = '添加关键字规则';
  87. $this->_form($this->table, 'form');
  88. }
  89. /**
  90. * 编辑关键字
  91. * @auth true
  92. */
  93. public function edit()
  94. {
  95. $this->applyCsrfToken();
  96. $this->title = '编辑关键字规则';
  97. $this->_form($this->table, 'form');
  98. }
  99. /**
  100. * 删除关键字
  101. * @auth true
  102. */
  103. public function remove()
  104. {
  105. $this->applyCsrfToken();
  106. $this->_delete($this->table);
  107. }
  108. /**
  109. * 禁用关键字
  110. * @auth true
  111. */
  112. public function forbid()
  113. {
  114. $this->applyCsrfToken();
  115. $this->_save($this->table, ['status' => '0']);
  116. }
  117. /**
  118. * 启用关键字
  119. * @auth true
  120. */
  121. public function resume()
  122. {
  123. $this->applyCsrfToken();
  124. $this->_save($this->table, ['status' => '1']);
  125. }
  126. /**
  127. * 配置关注回复
  128. * @auth true
  129. */
  130. public function subscribe()
  131. {
  132. $this->applyCsrfToken();
  133. $this->title = '编辑关注回复规则';
  134. $this->_form($this->table, 'form', 'keys', [], ['keys' => 'subscribe']);
  135. }
  136. /**
  137. * 配置默认回复
  138. * @auth true
  139. */
  140. public function defaults()
  141. {
  142. $this->applyCsrfToken();
  143. $this->title = '编辑默认回复规则';
  144. $this->_form($this->table, 'form', 'keys', [], ['keys' => 'default']);
  145. }
  146. /**
  147. * 添加数据处理
  148. * @param array $data
  149. */
  150. protected function _form_filter(array &$data)
  151. {
  152. if ($this->request->isPost() && isset($data['keys'])) {
  153. $db = Db::name($this->table)->where('keys', $data['keys']);
  154. empty($data['id']) || $db->where('id', 'neq', $data['id']);
  155. if ($db->count() > 0) {
  156. $this->error('关键字已经存在,请使用其它关键字!');
  157. }
  158. }
  159. if ($this->request->isGet()) {
  160. $this->msgTypes = $this->types;
  161. $root = rtrim(dirname(request()->basefile(true)), '\\/');
  162. $this->defaultImage = "{$root}/static/theme/img/image.png";
  163. }
  164. }
  165. /**
  166. * 表单结果处理
  167. * @param boolean $result
  168. */
  169. protected function _form_result($result)
  170. {
  171. if ($result !== false) {
  172. list($url, $keys) = ['', $this->request->post('keys')];
  173. if (!in_array($keys, ['subscribe', 'default'])) {
  174. $url = url('@admin') . '#' . url('wechat/keys/index') . '?spm=' . $this->request->get('spm');
  175. }
  176. $this->success('恭喜, 关键字保存成功!', $url);
  177. } else {
  178. $this->error('关键字保存失败, 请稍候再试!');
  179. }
  180. }
  181. }