Robot.php 5.7 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. }
  24. $join = [
  25. 'groups b' => 'a.groups_id = b.id',
  26. 'robotgroups c' => 'a.robotgroups_id = c.robotgroups_id',
  27. ];
  28. $result = model('Robot')->selectJoin($join, $where, $offset, $limit);
  29. foreach ($result as $key => $vo) {
  30. // 优化显示状态.
  31. if(1 === $vo['robot_status']) {
  32. $result[$key]['robot_status'] = '<span style="color: #2fbe1b">启用</span>';
  33. } else {
  34. $result[$key]['robot_status'] = '<span style="color: red">禁用</span>';
  35. }
  36. // 优化显示热点问题.
  37. if (1 === $vo['robot_host']) {
  38. $result[$key]['robot_host'] = '<span style="color: #2fbe1b">是</span>';
  39. } else {
  40. $result[$key]['robot_host'] = '<span style="color: red">否</span>';
  41. }
  42. // 生成操作按钮.
  43. $result[$key]['operate'] = $this->makeBtn($vo['robot_id']);
  44. }
  45. // 总数据.
  46. $return['total'] = model('Robot')->count($where);
  47. $return['rows'] = $result;
  48. return json($return);
  49. }
  50. return $this->fetch();
  51. }
  52. // 添加智能问答
  53. public function addWord()
  54. {
  55. if(request()->isPost()){
  56. $param = input('post.');
  57. $param['robot_content'] = trim($param['robot_content']);
  58. $robotWhere = [
  59. 'robot_name' => $param['robot_name'],
  60. 'groups_id' => $param['groups_id'],
  61. 'robotgroups_id' => $param['robotgroups_id'],
  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. try{
  70. db('robot')->insert($param);
  71. }catch(\Exception $e){
  72. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  73. }
  74. return json(['code' => 1, 'data' => '', 'msg' => '添加智能问答成功']);
  75. }
  76. $groups = db('groups')->where('status', 1)->select();
  77. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  78. $this->assign([
  79. 'status' => config('kf_status'),
  80. 'host' => config('host'),
  81. 'groups' => $groups,
  82. 'robotgroups' => $robotgroups,
  83. ]);
  84. return $this->fetch();
  85. }
  86. // 编辑智能问答
  87. public function editWord()
  88. {
  89. if(request()->isAjax()){
  90. $param = input('post.');
  91. $param['robot_content'] = trim($param['robot_content']);
  92. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  93. $robotWhere = [
  94. 'robot_name' => $param['robot_name'],
  95. 'groups_id' => $param['groups_id'],
  96. 'robotgroups_id' => $param['robotgroups_id'],
  97. ];
  98. // 检测用户修改的智能问答是否重复
  99. $has = db('robot')->where($robotWhere)->count();
  100. if($has>1){
  101. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  102. }
  103. try{
  104. db('robot')->where('robot_id', $param['robot_id'])->update($param);
  105. }catch(\Exception $e){
  106. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  107. }
  108. return json(['code' => 1, 'data' => '', 'msg' => '编辑智能问答成功']);
  109. }
  110. $id = input('param.robot_id/d');
  111. $info = db('robot')->where('robot_id', $id)->find();
  112. $groups = db('groups')->where('status', 1)->select();
  113. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  114. $this->assign([
  115. 'info' => $info,
  116. 'host' => config('host'),
  117. 'groups' => $groups,
  118. 'robotgroups' => $robotgroups,
  119. 'status' => config('kf_status')
  120. ]);
  121. return $this->fetch();
  122. }
  123. // 删除智能问答
  124. public function delWord()
  125. {
  126. if(request()->isAjax()){
  127. $id = input('param.id/d');
  128. try{
  129. db('robot')->where('robot_id', $id)->delete();
  130. }catch(\Exception $e){
  131. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  132. }
  133. return json(['code' => 1, 'data' => '', 'msg' => '删除智能问答成功']);
  134. }
  135. }
  136. // 生成按钮
  137. private function makeBtn($id)
  138. {
  139. $operate = '<a href="' . url('robot/editword', ['robot_id' => $id]) . '">';
  140. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  141. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  142. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  143. return $operate;
  144. }
  145. }