Autoreply.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\admin\model\WechatResponse;
  5. /**
  6. * 微信自动回复管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Autoreply extends Backend
  11. {
  12. protected $model = null;
  13. protected $noNeedRight = ['check_text_unique'];
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = model('WechatAutoreply');
  18. }
  19. /**
  20. * 编辑
  21. */
  22. public function edit($ids = null)
  23. {
  24. $row = $this->model->get(['id' => $ids]);
  25. if (!$row) {
  26. $this->error(__('No Results were found'));
  27. }
  28. if ($this->request->isPost()) {
  29. $params = $this->request->post("row/a");
  30. if ($params) {
  31. $row->save($params);
  32. $this->success();
  33. }
  34. $this->error();
  35. }
  36. $response = WechatResponse::get(['eventkey' => $row['eventkey']]);
  37. $this->view->assign("response", $response);
  38. $this->view->assign("row", $row);
  39. return $this->view->fetch();
  40. }
  41. /**
  42. * 判断文本是否唯一
  43. * @internal
  44. */
  45. public function check_text_unique()
  46. {
  47. $row = $this->request->post("row/a");
  48. $except = $this->request->post("except");
  49. $text = isset($row['text']) ? $row['text'] : '';
  50. if ($this->model->where('text', $text)->where(function ($query) use ($except) {
  51. if ($except) {
  52. $query->where('text', '<>', $except);
  53. }
  54. })->count() == 0) {
  55. $this->success();
  56. } else {
  57. $this->error(__('Text already exists'));
  58. }
  59. }
  60. }