Robot.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Loader;
  4. use think\Controller;
  5. /**
  6. * 智能问答类
  7. */
  8. class Robot extends Base
  9. {
  10. /**
  11. * 智能问答列表
  12. *
  13. * @access public
  14. * @return array JsonString
  15. */
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $param = input('param.');
  20. $limit = $param['pageSize'];
  21. $offset = (($param['pageNumber'] - 1) * $limit);
  22. $where = [];
  23. if (strlen($param['searchText'])) {
  24. // $where['robot_name'] = $param['searchText'];
  25. $where['robot_name'] = ['like', '%' . $param['searchText'] . '%'];
  26. }
  27. $field = ['robot_id', 'robot_name', 'robot_content', 'robot_host', 'robot_status', 'user_name', 'robot_updateTime'];
  28. $join = [
  29. 'admins b' => 'a.admin_id = b.id',
  30. ];
  31. $result = model('Robot')->selectJoin($field, $join, $where, $offset, $limit);
  32. foreach ($result as $key => $vo) {
  33. // 优化显示状态.
  34. if(1 == $vo['robot_status']) {
  35. $result[$key]['robot_status'] = '<span style="color: #2fbe1b">启用</span>';
  36. } else {
  37. $result[$key]['robot_status'] = '<span style="color: red">禁用</span>';
  38. }
  39. // 优化显示热点问题.
  40. if (1 == $vo['robot_host']) {
  41. $result[$key]['robot_host'] = '<span style="color: #2fbe1b">是</span>';
  42. } else {
  43. $result[$key]['robot_host'] = '<span style="color: red">否</span>';
  44. }
  45. // 生成操作按钮.
  46. $result[$key]['operate'] = $this->makeBtn($vo['robot_id']);
  47. }
  48. // var_dump($where);die;
  49. // 总数据.
  50. $return['total'] = model('Robot')->count($where);
  51. $return['rows'] = $result;
  52. return json($return);
  53. }
  54. return $this->fetch();
  55. }//end index()
  56. // 添加智能问答
  57. public function addWord()
  58. {
  59. if(request()->isPost()){
  60. $param = input('post.');
  61. $param['robot_content'] = trim($param['robot_content']);
  62. $robotWhere = [
  63. 'robot_name' => $param['robot_name'],
  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. $param['groups_id'] = 1;
  72. $param['robotgroups_id'] = 1;
  73. $param['admin_id'] = session('user_id');
  74. try{
  75. db('robot')->insert($param);
  76. }catch(\Exception $e){
  77. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  78. }
  79. return json(['code' => 1, 'data' => '', 'msg' => '添加智能问答成功']);
  80. }
  81. $groups = db('groups')->where('status', 1)->select();
  82. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  83. $this->assign([
  84. 'status' => config('kf_status'),
  85. 'host' => config('host'),
  86. 'groups' => $groups,
  87. 'robotgroups' => $robotgroups,
  88. ]);
  89. return $this->fetch('addword');
  90. }
  91. // 编辑智能问答
  92. public function editWord()
  93. {
  94. if(request()->isAjax()){
  95. $param = input('post.');
  96. $param['robot_content'] = trim($param['robot_content']);
  97. $param['robot_updateTime'] = date('Y-m-d H:i:s');
  98. $robotWhere = [
  99. 'robot_name' => $param['robot_name'],
  100. ];
  101. // 检测用户修改的智能问答是否重复
  102. $has = db('robot')->where($robotWhere)->count();
  103. if($has>1){
  104. return json(['code' => -1, 'data' => '', 'msg' => '该智能问答已经存在']);
  105. }
  106. $param['groups_id'] = 1;
  107. $param['robotgroups_id'] = 1;
  108. $param['admin_id'] = session('user_id');
  109. try{
  110. db('robot')->where('robot_id', $param['robot_id'])->update($param);
  111. }catch(\Exception $e){
  112. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  113. }
  114. return json(['code' => 1, 'data' => '', 'msg' => '编辑智能问答成功']);
  115. }
  116. $id = input('param.robot_id/d');
  117. $info = db('robot')->where('robot_id', $id)->find();
  118. $groups = db('groups')->where('status', 1)->select();
  119. $robotgroups = db('robotgroups')->where('robotgroups_status', 1)->select();
  120. $this->assign([
  121. 'info' => $info,
  122. 'host' => config('host'),
  123. 'groups' => $groups,
  124. 'robotgroups' => $robotgroups,
  125. 'status' => config('kf_status')
  126. ]);
  127. return $this->fetch('editword');
  128. }
  129. // 删除智能问答
  130. public function delWord()
  131. {
  132. if(request()->isAjax()){
  133. $id = input('param.id/d');
  134. try{
  135. db('robot')->where('robot_id', $id)->delete();
  136. }catch(\Exception $e){
  137. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  138. }
  139. return json(['code' => 1, 'data' => '', 'msg' => '删除智能问答成功']);
  140. }
  141. }
  142. // 生成按钮
  143. private function makeBtn($id)
  144. {
  145. $operate = '<a href="' . url('robot/editword', ['robot_id' => $id]) . '">';
  146. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  147. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  148. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  149. return $operate;
  150. }
  151. //导入智能问答
  152. function inserExcel()
  153. {
  154. Loader::import('PHPExcel.PHPExcel');
  155. Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
  156. Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell');
  157. //获取表单上传文件
  158. $file = request()->file('excel');
  159. $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  160. //上传验证后缀名,以及上传之后移动的地址
  161. if ($info) {
  162. // echo $info->getFilename();
  163. $exclePath = $info->getSaveName(); //获取文件名
  164. $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath; //上传文件的地址
  165. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  166. $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
  167. $excel_array = $obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
  168. array_shift($excel_array); //删除第一个数组(标题);
  169. $info = [];
  170. foreach ($excel_array as $k => $v) {
  171. $info[$k]['robot_name'] = $v[0];
  172. $info[$k]['robot_content'] = $v[1];
  173. $info[$k]['robot_status'] = $v[2];
  174. $info[$k]['robot_host'] = $v[3];
  175. $info[$k]['groups_id'] = 1;
  176. $info[$k]['robotgroups_id'] = 1;
  177. $info[$k]['robot_addTime'] = date('Y-m-d H:m:s',time());
  178. $info[$k]['robot_updateTime'] = date('Y-m-d H:m:s',time());
  179. $info[$k]['admin_id'] = session('user_id');
  180. }
  181. //检查表中数据是否为空和重复
  182. for($a=0;$a<count($info);$a++){
  183. if(empty($info[$a]['robot_name'])){
  184. return json(['code' => -3, 'data' => url('robot/index'), 'msg' => 'excel表第'.($a+2).'行智能问答问题为空']);
  185. }
  186. if(empty($info[$a]['robot_content'])){
  187. return json(['code' => -4, 'data' => url('robot/index'), 'msg' => 'excel表第'.($a+2).'行智能问答答案为空']);
  188. }
  189. for($b=$a+1;$b<count($info);$b++){
  190. if($info[$a]['robot_name'] == $info[$b]['robot_name']){
  191. return json(['code' => -2, 'data' => url('robot/index'), 'msg' => 'excel表第'.($a+2).'行与第'.($b+2).'行智能问答问题重复']);
  192. }
  193. }
  194. }
  195. //检查表格中数据是否已存在与数据库
  196. $robot = db('robot')->select();
  197. for($i=0;$i<count($info);$i++){
  198. for($j=0;$j<count($robot);$j++){
  199. if($info[$i]['robot_name'] == $robot[$j]['robot_name']){
  200. return json(['code' => -1, 'data' => url('robot/index'), 'msg' => 'excel表第'.($i+2).'行智能问答问题已存在']);
  201. }
  202. }
  203. }
  204. db('robot')->insertAll($info); //批量插入数据
  205. return json(['code' => 1, 'data' => $this->redirect('robot/index'), 'msg' => '插入智能问答数据成功']);
  206. } else {
  207. return json(['code' => -2, 'data' => url('robot/index'), 'msg' => '插入智能问答数据失败']);
  208. }
  209. }
  210. }