Robot.php 10 KB

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