Robot.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $join = [
  26. 'groups b' => 'a.groups_id = b.id',
  27. 'robotgroups c' => 'a.robotgroups_id = c.robotgroups_id',
  28. ];
  29. $result = model('Robot')->selectJoin($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. }
  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. 'groups_id' => $param['groups_id'],
  63. 'robotgroups_id' => $param['robotgroups_id'],
  64. ];
  65. $has = db('robot')->field('robot_id')->where($robotWhere)->find();
  66. if(!empty($has)){
  67. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  68. }
  69. $param['robot_addTime'] = date('Y-m-d H:i:s');
  70. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  71. try{
  72. db('robot')->insert($param);
  73. }catch(\Exception $e){
  74. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  75. }
  76. return json(['code' => 1, 'data' => '', 'msg' => '添加智能问答成功']);
  77. }
  78. $groups = db('groups')->where('status', 1)->select();
  79. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  80. $this->assign([
  81. 'status' => config('kf_status'),
  82. 'host' => config('host'),
  83. 'groups' => $groups,
  84. 'robotgroups' => $robotgroups,
  85. ]);
  86. return $this->fetch('addword');
  87. }
  88. // 编辑智能问答
  89. public function editWord()
  90. {
  91. if(request()->isAjax()){
  92. $param = input('post.');
  93. $param['robot_content'] = trim($param['robot_content']);
  94. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  95. $robotWhere = [
  96. 'robot_name' => $param['robot_name'],
  97. 'groups_id' => $param['groups_id'],
  98. 'robotgroups_id' => $param['robotgroups_id'],
  99. ];
  100. // 检测用户修改的智能问答是否重复
  101. $has = db('robot')->where($robotWhere)->count();
  102. if($has>1){
  103. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  104. }
  105. try{
  106. db('robot')->where('robot_id', $param['robot_id'])->update($param);
  107. }catch(\Exception $e){
  108. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  109. }
  110. return json(['code' => 1, 'data' => '', 'msg' => '编辑智能问答成功']);
  111. }
  112. $id = input('param.robot_id/d');
  113. $info = db('robot')->where('robot_id', $id)->find();
  114. $groups = db('groups')->where('status', 1)->select();
  115. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  116. $this->assign([
  117. 'info' => $info,
  118. 'host' => config('host'),
  119. 'groups' => $groups,
  120. 'robotgroups' => $robotgroups,
  121. 'status' => config('kf_status')
  122. ]);
  123. return $this->fetch('editword');
  124. }
  125. // 删除智能问答
  126. public function delWord()
  127. {
  128. if(request()->isAjax()){
  129. $id = input('param.id/d');
  130. try{
  131. db('robot')->where('robot_id', $id)->delete();
  132. }catch(\Exception $e){
  133. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  134. }
  135. return json(['code' => 1, 'data' => '', 'msg' => '删除智能问答成功']);
  136. }
  137. }
  138. // 生成按钮
  139. private function makeBtn($id)
  140. {
  141. $operate = '<a href="' . url('robot/editword', ['robot_id' => $id]) . '">';
  142. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  143. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  144. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  145. return $operate;
  146. }
  147. }