Autoreply.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use app\common\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. if ($this->request->isPost())
  28. {
  29. $this->code = -1;
  30. $params = $this->request->post("row/a");
  31. if ($params)
  32. {
  33. $row->save($params);
  34. $this->code = 1;
  35. }
  36. return;
  37. }
  38. $response = WechatResponse::get(['eventkey' => $row['eventkey']]);
  39. $this->view->assign("response", $response);
  40. $this->view->assign("row", $row);
  41. return $this->view->fetch();
  42. }
  43. /**
  44. * 判断文本是否唯一
  45. * @internal
  46. */
  47. public function check_text_unique()
  48. {
  49. $row = $this->request->post("row/a");
  50. $except = $this->request->post("except");
  51. $text = isset($row['text']) ? $row['text'] : '';
  52. if ($this->model->where('text', $text)->where(function($query) use($except) {
  53. if ($except)
  54. {
  55. $query->where('text', '<>', $except);
  56. }
  57. })->count() == 0)
  58. {
  59. return json(['ok' => '']);
  60. }
  61. else
  62. {
  63. return json(['error' => __('Text already exists')]);
  64. }
  65. }
  66. }