Autoreply.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\controller\xmwechat\offiaccount;
  3. use app\common\controller\Backend;
  4. use Throwable;
  5. /**
  6. * 公众号自动回复
  7. */
  8. class Autoreply extends Backend
  9. {
  10. /**
  11. * Autoreply模型对象
  12. * @var object
  13. * @phpstan-var \app\admin\model\xmwechat\offiaccount\Autoreply
  14. */
  15. protected object $model;
  16. protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];
  17. protected string|array $quickSearchField = ['keyword'];
  18. public function initialize(): void
  19. {
  20. parent::initialize();
  21. $this->model = new \app\admin\model\xmwechat\offiaccount\Autoreply;
  22. $this->request->filter('trim,htmlspecialchars');
  23. }
  24. public function add(): void
  25. {
  26. if ($this->request->isPost()) {
  27. $data = $this->request->post();
  28. if (!$data) {
  29. $this->error(__('Parameter %s can not be empty', ['']));
  30. }
  31. $data = $this->excludeFields($data);
  32. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  33. $data[$this->dataLimitField] = $this->auth->id;
  34. }
  35. if ($data['type'] == 'follow' || $data['type'] == 'default') {
  36. $data['type'] == 'follow' ? $replyType = '关注回复' : $replyType = '默认回复';
  37. $reply = $this->model->where('type', $data['type'])->find();
  38. if (!empty($reply)) {
  39. $this->error('当前已设置' . $replyType . ',请前往修改');
  40. }
  41. }
  42. $result = false;
  43. $this->model->startTrans();
  44. try {
  45. // 模型验证
  46. if ($this->modelValidate) {
  47. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  48. if (class_exists($validate)) {
  49. $validate = new $validate;
  50. if ($this->modelSceneValidate) $validate->scene('add');
  51. $validate->check($data);
  52. }
  53. }
  54. $result = $this->model->save($data);
  55. $this->model->commit();
  56. } catch (Throwable $e) {
  57. $this->model->rollback();
  58. $this->error($e->getMessage());
  59. }
  60. if ($result !== false) {
  61. $this->success(__('Added successfully'));
  62. } else {
  63. $this->error(__('No rows were added'));
  64. }
  65. }
  66. $this->error(__('Parameter error'));
  67. }
  68. public function edit(): void
  69. {
  70. $id = $this->request->param($this->model->getPk());
  71. $row = $this->model->find($id);
  72. if (!$row) {
  73. $this->error(__('Record not found'));
  74. }
  75. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  76. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. if ($this->request->isPost()) {
  80. $data = $this->request->post();
  81. if (!$data) {
  82. $this->error(__('Parameter %s can not be empty', ['']));
  83. }
  84. $data = $this->excludeFields($data);
  85. if ($data['type'] == 'follow' || $data['type'] == 'default') {
  86. $data['type'] == 'follow' ? $replyType = '关注回复' : $replyType = '默认回复';
  87. $reply = $this->model->where('type', $data['type'])->where('id', '<>', $row['id'])->find();
  88. if (!empty($reply)) {
  89. $this->error('当前已设置' . $replyType . ',请前往修改');
  90. }
  91. }
  92. $result = false;
  93. $this->model->startTrans();
  94. try {
  95. // 模型验证
  96. if ($this->modelValidate) {
  97. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. if (class_exists($validate)) {
  99. $validate = new $validate;
  100. if ($this->modelSceneValidate) $validate->scene('edit');
  101. $validate->check($data);
  102. }
  103. }
  104. $result = $row->save($data);
  105. $this->model->commit();
  106. } catch (Throwable $e) {
  107. $this->model->rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if ($result !== false) {
  111. $this->success(__('Update successful'));
  112. } else {
  113. $this->error(__('No rows updated'));
  114. }
  115. }
  116. $this->success('', [
  117. 'row' => $row
  118. ]);
  119. }
  120. }