Robot.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 智能问答类
  5. */
  6. class Robot extends Base
  7. {
  8. /**
  9. * 智能问答列表
  10. *
  11. * @access public
  12. * @return array JsonString
  13. */
  14. public function index()
  15. {
  16. if (request()->isAjax()) {
  17. $param = input('param.');
  18. $limit = $param['pageSize'];
  19. $offset = (($param['pageNumber'] - 1) * $limit);
  20. $where = [];
  21. if (empty($param['searchText']) === false) {
  22. // $where['robot_name'] = $param['searchText'];
  23. $where['robot_name'] = ['like', '%' . $param['searchText'] . '%'];
  24. }
  25. $field = ['robot_id', 'robot_name', 'robot_content', 'robot_host', 'robot_status', 'user_name', 'robot_updateTime'];
  26. $join = [
  27. 'admins b' => 'a.admin_id = b.id',
  28. ];
  29. $result = model('Robot')->selectJoin($field, $join, $where, $offset, $limit);
  30. foreach ($result as $key => $vo) {
  31. // 优化显示状态.
  32. if(1 === $vo['robot_status']) {
  33. $result[$key]['robot_status'] = '<span style="color: #2fbe1b">启用</span>';
  34. } else {
  35. $result[$key]['robot_status'] = '<span style="color: red">禁用</span>';
  36. }
  37. // 优化显示热点问题.
  38. if (1 === $vo['robot_host']) {
  39. $result[$key]['robot_host'] = '<span style="color: #2fbe1b">是</span>';
  40. } else {
  41. $result[$key]['robot_host'] = '<span style="color: red">否</span>';
  42. }
  43. // 生成操作按钮.
  44. $result[$key]['operate'] = $this->makeBtn($vo['robot_id']);
  45. }
  46. // var_dump($where);die;
  47. // 总数据.
  48. $return['total'] = model('Robot')->count($where);
  49. $return['rows'] = $result;
  50. return json($return);
  51. }
  52. return $this->fetch();
  53. }//end index()
  54. // 添加智能问答
  55. public function addWord()
  56. {
  57. if(request()->isPost()){
  58. $param = input('post.');
  59. $param['robot_content'] = trim($param['robot_content']);
  60. $robotWhere = [
  61. 'robot_name' => $param['robot_name'],
  62. ];
  63. $has = db('robot')->field('robot_id')->where($robotWhere)->find();
  64. if(!empty($has)){
  65. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  66. }
  67. $param['robot_addTime'] = date('Y-m-d H:i:s');
  68. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  69. $param['groups_id'] = 1;
  70. $param['robotgroups_id'] = 1;
  71. $param['admin_id'] = session('user_id');
  72. try{
  73. db('robot')->insert($param);
  74. }catch(\Exception $e){
  75. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  76. }
  77. return json(['code' => 1, 'data' => '', 'msg' => '添加智能问答成功']);
  78. }
  79. $groups = db('groups')->where('status', 1)->select();
  80. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  81. $this->assign([
  82. 'status' => config('kf_status'),
  83. 'host' => config('host'),
  84. 'groups' => $groups,
  85. 'robotgroups' => $robotgroups,
  86. ]);
  87. return $this->fetch('addword');
  88. }
  89. // 编辑智能问答
  90. public function editWord()
  91. {
  92. if(request()->isAjax()){
  93. $param = input('post.');
  94. $param['robot_content'] = trim($param['robot_content']);
  95. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  96. $robotWhere = [
  97. 'robot_name' => $param['robot_name'],
  98. ];
  99. // 检测用户修改的智能问答是否重复
  100. $has = db('robot')->where($robotWhere)->count();
  101. if($has>1){
  102. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  103. }
  104. $param['groups_id'] = 1;
  105. $param['robotgroups_id'] = 1;
  106. $param['admin_id'] = session('user_id');
  107. try{
  108. db('robot')->where('robot_id', $param['robot_id'])->update($param);
  109. }catch(\Exception $e){
  110. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  111. }
  112. return json(['code' => 1, 'data' => '', 'msg' => '编辑智能问答成功']);
  113. }
  114. $id = input('param.robot_id/d');
  115. $info = db('robot')->where('robot_id', $id)->find();
  116. $groups = db('groups')->where('status', 1)->select();
  117. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  118. $this->assign([
  119. 'info' => $info,
  120. 'host' => config('host'),
  121. 'groups' => $groups,
  122. 'robotgroups' => $robotgroups,
  123. 'status' => config('kf_status')
  124. ]);
  125. return $this->fetch('editword');
  126. }
  127. // 删除智能问答
  128. public function delWord()
  129. {
  130. if(request()->isAjax()){
  131. $id = input('param.id/d');
  132. try{
  133. db('robot')->where('robot_id', $id)->delete();
  134. }catch(\Exception $e){
  135. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  136. }
  137. return json(['code' => 1, 'data' => '', 'msg' => '删除智能问答成功']);
  138. }
  139. }
  140. // 生成按钮
  141. private function makeBtn($id)
  142. {
  143. $operate = '<a href="' . url('robot/editword', ['robot_id' => $id]) . '">';
  144. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  145. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  146. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  147. return $operate;
  148. }
  149. }