Keys.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\controller;
  14. use controller\BasicAdmin;
  15. use service\DataService;
  16. use think\Db;
  17. /**
  18. * 微信文章管理
  19. * Class Article
  20. * @package app\wechat\controller
  21. * @author Anyon <zoujingli@qq.com>
  22. * @date 2017/03/27 14:43
  23. */
  24. class Keys extends BasicAdmin
  25. {
  26. /**
  27. * 指定当前数据表
  28. * @var string
  29. */
  30. public $table = 'WechatKeys';
  31. /**
  32. * 显示关键字列表
  33. */
  34. public function index()
  35. {
  36. $this->assign('title', '微信关键字');
  37. $db = Db::name($this->table)->where('keys', 'not in', ['subscribe', 'default']);
  38. return $this->_list($db);
  39. }
  40. /**
  41. * 列表数据处理
  42. * @param array $data
  43. */
  44. protected function _index_data_filter(&$data)
  45. {
  46. $types = [
  47. 'keys' => '关键字', 'image' => '图片', 'news' => '图文',
  48. 'music' => '音乐', 'text' => '文字', 'video' => '视频', 'voice' => '语音'
  49. ];
  50. $wechat = load_wechat('Extends');
  51. foreach ($data as &$vo) {
  52. $result = $wechat->getQRCode($vo['keys'], 1);
  53. $vo['qrc'] = $wechat->getQRUrl($result['ticket']);
  54. $vo['type'] = isset($types[$vo['type']]) ? $types[$vo['type']] : $vo['type'];
  55. }
  56. }
  57. /**
  58. * 添加关键字
  59. * @return string
  60. */
  61. public function add()
  62. {
  63. $this->title = '添加关键字规则';
  64. return $this->_form($this->table, 'form');
  65. }
  66. /**
  67. * 编辑关键字
  68. * @return string
  69. */
  70. public function edit()
  71. {
  72. $this->title = '编辑关键字规则';
  73. return $this->_form($this->table, 'form');
  74. }
  75. /**
  76. * 表单处理
  77. * @param $data
  78. */
  79. protected function _form_filter($data)
  80. {
  81. if ($this->request->isPost() && isset($data['keys'])) {
  82. $db = Db::name($this->table)->where('keys', $data['keys']);
  83. !empty($data['id']) && $db->where('id', 'neq', $data['id']);
  84. $db->count() > 0 && $this->error('关键字已经存在,请使用其它关键字!');
  85. }
  86. }
  87. /**
  88. * 删除关键字
  89. */
  90. public function del()
  91. {
  92. if (DataService::update($this->table)) {
  93. $this->success("关键字删除成功!", '');
  94. }
  95. $this->error("关键字删除失败,请稍候再试!");
  96. }
  97. /**
  98. * 关键字禁用
  99. */
  100. public function forbid()
  101. {
  102. if (DataService::update($this->table)) {
  103. $this->success("关键字禁用成功!", '');
  104. }
  105. $this->error("关键字禁用失败,请稍候再试!");
  106. }
  107. /**
  108. * 关键字禁用
  109. */
  110. public function resume()
  111. {
  112. if (DataService::update($this->table)) {
  113. $this->success("关键字启用成功!", '');
  114. }
  115. $this->error("关键字启用失败,请稍候再试!");
  116. }
  117. /**
  118. * 关注默认回复
  119. */
  120. public function subscribe()
  121. {
  122. $this->assign('title', '编辑默认回复');
  123. return $this->_form($this->table, 'form');
  124. }
  125. /**
  126. * 关注默认回复表单处理
  127. * @param $data
  128. */
  129. protected function _subscribe_form_filter(&$data)
  130. {
  131. if ($this->request->isGet()) {
  132. $data = Db::name($this->table)->where('keys', 'subscribe')->find();
  133. }
  134. $data['keys'] = 'subscribe';
  135. }
  136. /**
  137. * 无配置默认回复
  138. */
  139. public function defaults()
  140. {
  141. $this->assign('title', '编辑无配置默认回复');
  142. return $this->_form($this->table, 'form');
  143. }
  144. /**
  145. * 无配置默认回复表单处理
  146. * @param $data
  147. */
  148. protected function _defaults_form_filter(&$data)
  149. {
  150. if ($this->request->isGet()) {
  151. $data = Db::name($this->table)->where('keys', 'default')->find();
  152. }
  153. $data['keys'] = 'default';
  154. }
  155. }