Keys.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * @throws \think\Exception
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. * @throws \think\exception\PDOException
  87. */
  88. public function add()
  89. {
  90. $this->applyCsrfToken();
  91. $this->title = '添加关键字规则';
  92. $this->_form($this->table, 'form');
  93. }
  94. /**
  95. * 编辑关键字
  96. * @auth true
  97. * @throws \think\Exception
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @throws \think\exception\DbException
  101. * @throws \think\exception\PDOException
  102. */
  103. public function edit()
  104. {
  105. $this->applyCsrfToken();
  106. $this->title = '编辑关键字规则';
  107. $this->_form($this->table, 'form');
  108. }
  109. /**
  110. * 删除关键字
  111. * @auth true
  112. * @throws \think\Exception
  113. * @throws \think\exception\PDOException
  114. */
  115. public function remove()
  116. {
  117. $this->applyCsrfToken();
  118. $this->_delete($this->table);
  119. }
  120. /**
  121. * 禁用关键字
  122. * @auth true
  123. * @throws \think\Exception
  124. * @throws \think\exception\PDOException
  125. */
  126. public function forbid()
  127. {
  128. $this->applyCsrfToken();
  129. $this->_save($this->table, ['status' => '0']);
  130. }
  131. /**
  132. * 启用关键字
  133. * @auth true
  134. * @throws \think\Exception
  135. * @throws \think\exception\PDOException
  136. */
  137. public function resume()
  138. {
  139. $this->applyCsrfToken();
  140. $this->_save($this->table, ['status' => '1']);
  141. }
  142. /**
  143. * 配置关注回复
  144. * @auth true
  145. * @throws \think\Exception
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. * @throws \think\exception\PDOException
  150. */
  151. public function subscribe()
  152. {
  153. $this->applyCsrfToken();
  154. $this->title = '编辑关注回复规则';
  155. $this->_form($this->table, 'form', 'keys', [], ['keys' => 'subscribe']);
  156. }
  157. /**
  158. * 配置默认回复
  159. * @auth true
  160. * @throws \think\Exception
  161. * @throws \think\db\exception\DataNotFoundException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. * @throws \think\exception\DbException
  164. * @throws \think\exception\PDOException
  165. */
  166. public function defaults()
  167. {
  168. $this->applyCsrfToken();
  169. $this->title = '编辑默认回复规则';
  170. $this->_form($this->table, 'form', 'keys', [], ['keys' => 'default']);
  171. }
  172. /**
  173. * 添加数据处理
  174. * @param array $data
  175. */
  176. protected function _form_filter(array &$data)
  177. {
  178. if ($this->request->isPost() && isset($data['keys'])) {
  179. $db = Db::name($this->table)->where('keys', $data['keys']);
  180. empty($data['id']) || $db->where('id', 'neq', $data['id']);
  181. if ($db->count() > 0) {
  182. $this->error('关键字已经存在,请使用其它关键字!');
  183. }
  184. }
  185. if ($this->request->isGet()) {
  186. $this->msgTypes = $this->types;
  187. $root = rtrim(dirname(request()->basefile(true)), '\\/');
  188. $this->defaultImage = "{$root}/static/theme/img/image.png";
  189. }
  190. }
  191. /**
  192. * 表单结果处理
  193. * @param boolean $result
  194. */
  195. protected function _form_result($result)
  196. {
  197. if ($result !== false) {
  198. list($url, $keys) = ['', $this->request->post('keys')];
  199. if (!in_array($keys, ['subscribe', 'default'])) {
  200. $url = url('@admin') . '#' . url('wechat/keys/index') . '?spm=' . $this->request->get('spm');
  201. }
  202. $this->success('恭喜, 关键字保存成功!', $url);
  203. } else {
  204. $this->error('关键字保存失败, 请稍候再试!');
  205. }
  206. }
  207. }