Autoreply.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Config;
  4. use think\Db;
  5. class Autoreply extends Admin
  6. {
  7. const DEFAULTMSG = 'defaultmsg'; // 收到消息未匹配关键词时回复
  8. const SUBSCRIBE = 'subscribe'; // 关注回复
  9. protected function _initialize()
  10. {
  11. $this->model = model('WxAutoreplyMsg');
  12. parent::_initialize();
  13. }
  14. public function index()
  15. {
  16. $defaultmsg = $this->model->where('reply_type',self::DEFAULTMSG)->value('content');
  17. $subscribe = $this->model->where('reply_type',self::SUBSCRIBE)->value('content');
  18. $this->assign('defaultmsg',$defaultmsg);
  19. $this->assign('subscribe',$subscribe);
  20. return $this->fetch();
  21. }
  22. public function add()
  23. {
  24. if ( request()->isPost()) {
  25. $post = input('post.');
  26. // 判断是否新增
  27. $info = $this->model->where('reply_type',$post['reply_type'])->find();
  28. $data = [
  29. 'content' => $post[$post['reply_type']]
  30. ];
  31. if ( empty($info)) {
  32. $data['reply_type'] = $post['reply_type'];
  33. $result = $this->model->insert($data);
  34. }else{
  35. $result = $this->model->where('reply_type',$post['reply_type'])->update($data);
  36. }
  37. if ( $result !== false) {
  38. $this->success('更新成功');
  39. }else{
  40. $this->error('更新失败');
  41. }
  42. }
  43. }
  44. }