Fastreply.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\admin\controller\kefu;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use addons\kefu\library\Common;
  9. /**
  10. * 快捷回复管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Fastreply extends Backend
  15. {
  16. /**
  17. * KeFuFastReply模型对象
  18. * @var \app\admin\model\KeFuFastReply
  19. */
  20. protected $model = null;
  21. protected $dataLimit = 'auth';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\KeFuFastReply;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign('isSuperAdmin', $this->auth->isSuperAdmin());
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = true;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $total = $this->model->with(['admin'])->where($where)->order($sort, $order)->count();
  45. $list = $this->model->with(['admin'])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->limit($offset, $limit)
  49. ->select();
  50. foreach ($list as $row) {
  51. $row->content = strip_tags($row->content);
  52. $row->getRelation('admin')->visible(['nickname']);
  53. }
  54. $list = collection($list)->toArray();
  55. $result = ["total" => $total, "rows" => $list];
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. if (isset($params['is_general']) && $params['is_general']) {
  69. $params['admin_id'] = false;
  70. } else {
  71. $params['admin_id'] = true;
  72. }
  73. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  74. $params['content'] = Common::htmlImgUrlHandle($params['content']);
  75. if ($this->dataLimit && $this->dataLimitFieldAutoFill && $params['admin_id']) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. $result = false;
  79. Db::startTrans();
  80. try {
  81. //是否采用模型验证
  82. if ($this->modelValidate) {
  83. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  84. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  85. $this->model->validateFailException(true)->validate($validate);
  86. }
  87. $result = $this->model->allowField(true)->save($params);
  88. Db::commit();
  89. } catch (ValidateException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (PDOException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result !== false) {
  100. $this->success();
  101. } else {
  102. $this->error(__('No rows were inserted'));
  103. }
  104. }
  105. $this->error(__('Parameter %s can not be empty', ''));
  106. }
  107. return $this->view->fetch();
  108. }
  109. /**
  110. * 编辑
  111. */
  112. public function edit($ids = null)
  113. {
  114. $row = $this->model->get($ids);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $adminIds = $this->getDataLimitAdminIds();
  119. if (is_array($adminIds)) {
  120. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  121. $this->error(__('You have no permission'));
  122. }
  123. }
  124. if ($this->request->isPost()) {
  125. $params = $this->request->post("row/a");
  126. if ($params) {
  127. if (isset($params['is_general']) && $params['is_general'] && $this->auth->isSuperAdmin()) {
  128. $params['admin_id'] = 0;
  129. } else {
  130. $params['admin_id'] = $this->auth->id;
  131. }
  132. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  133. $params['content'] = Common::htmlImgUrlHandle($params['content']);
  134. $result = false;
  135. Db::startTrans();
  136. try {
  137. //是否采用模型验证
  138. if ($this->modelValidate) {
  139. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  140. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  141. $row->validateFailException(true)->validate($validate);
  142. }
  143. $result = $row->allowField(true)->save($params);
  144. Db::commit();
  145. } catch (ValidateException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (PDOException $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. } catch (Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if ($result !== false) {
  156. $this->success();
  157. } else {
  158. $this->error(__('No rows were updated'));
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. if (!$row->admin_id && $this->auth->isSuperAdmin()) {
  164. $row->is_general = '1';
  165. } else {
  166. $row->is_general = '0';
  167. }
  168. $this->view->assign("row", $row);
  169. return $this->view->fetch();
  170. }
  171. }