AutoReply.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 微信管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class AutoReply extends Backend
  14. {
  15. /**
  16. * Wechat模型对象
  17. * @var \app\admin\model\shopro\Wechat
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shopro\wechat\Wechat;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = false;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $type = $this->request->param('type');
  46. $total = $this->model
  47. ->where($where)
  48. ->where('type', $type)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->where($where)
  53. ->where('type', $type)
  54. ->order($sort, $order)
  55. ->limit($offset, $limit)
  56. ->select();
  57. $list = collection($list)->toArray();
  58. $result = array("total" => $total, "rows" => $list);
  59. $this->success('自动回复', null, $result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加
  65. */
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post();
  70. if ($params) {
  71. $params = json_decode($params['data'], true);
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. $params['content'] = json_encode($params['content'], JSON_UNESCAPED_UNICODE);
  76. $result = $this->model->allowField(true)->save($params);
  77. Db::commit();
  78. } catch (ValidateException $e) {
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. } catch (PDOException $e) {
  82. Db::rollback();
  83. $this->error($e->getMessage());
  84. } catch (Exception $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. }
  88. if ($result !== false) {
  89. $this->success();
  90. } else {
  91. $this->error(__('No rows were inserted'));
  92. }
  93. }
  94. $this->error(__('Parameter %s can not be empty', ''));
  95. }
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 编辑
  100. */
  101. public function edit($id = null)
  102. {
  103. switch ($id) {
  104. case 'subscribe':
  105. $row = $this->model->get(['type' => 'subscribe']);
  106. if (!$row) {
  107. $this->model->save([
  108. 'name' => '关注回复',
  109. 'type' => 'subscribe',
  110. 'content' => ''
  111. ]);
  112. $row = $this->model;
  113. }
  114. break;
  115. case 'default_reply':
  116. $row = $this->model->get(['type' => 'default_reply']);
  117. if (!$row) {
  118. $this->model->save([
  119. 'name' => '默认回复',
  120. 'type' => 'default_reply',
  121. 'content' => ''
  122. ]);
  123. $row = $this->model;
  124. }
  125. break;
  126. default:
  127. $row = $this->model->get($id);
  128. break;
  129. }
  130. if (!$row) {
  131. $this->error(__('No Results were found'));
  132. }
  133. if ($this->request->isPost()) {
  134. $params = $this->request->post();
  135. if ($params) {
  136. $params = json_decode($params['data'], true);
  137. $result = false;
  138. Db::startTrans();
  139. try {
  140. $params['content'] = json_encode($params['content'], JSON_UNESCAPED_UNICODE);
  141. $result = $row->allowField(true)->save($params);
  142. Db::commit();
  143. } catch (ValidateException $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. } catch (PDOException $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. } catch (Exception $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. }
  153. if ($result !== false) {
  154. $this->success();
  155. } else {
  156. $this->error(__('No rows were updated'));
  157. }
  158. }
  159. $this->error(__('Parameter %s can not be empty', ''));
  160. }
  161. $this->assignconfig("row", $row);
  162. return $this->view->fetch();
  163. }
  164. }