Kbs.php 6.1 KB

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