Words.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * User: nickbai
  4. * Date: 2017/10/23 13:33
  5. * Email: 1902822973@qq.com
  6. */
  7. namespace app\admin\controller;
  8. use think\Loader;
  9. use think\Controller;
  10. class Words extends Base
  11. {
  12. // 常用语列表
  13. public function index()
  14. {
  15. if(request()->isAjax()){
  16. $param = input('param.');
  17. $limit = $param['pageSize'];
  18. $offset = ($param['pageNumber'] - 1) * $limit;
  19. $where['user_id'] = $param['type'] == 1? 0 : ['neq',0];
  20. if (strlen($param['searchText'])) {
  21. $where['content'] = ['like', '%' . $param['searchText'] . '%'];
  22. }
  23. $result = db('words')
  24. ->field(['a.id', 'title', 'content', 'update_time', 'user_name', 'a.status'])
  25. ->alias('a')
  26. ->join('admins b', 'a.admin_id = b.id','left')
  27. ->where($where)
  28. ->limit($offset, $limit)
  29. ->select();
  30. foreach($result as $key=>$vo){
  31. // 优化显示状态
  32. if(1 == $vo['status']){
  33. $result[$key]['status'] = '<span class="label label-primary">启用</span>';
  34. }else{
  35. $result[$key]['status'] = '<span class="label label-danger">禁用</span>';
  36. }
  37. if ($param['type'] == 1) {
  38. // 生成操作按钮
  39. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  40. }
  41. }
  42. $return['total'] = db('words')->where($where)->count(); //总数据
  43. $return['rows'] = $result;
  44. return json($return);
  45. }
  46. return $this->fetch();
  47. }
  48. // 添加常用语
  49. public function addWord()
  50. {
  51. if(request()->isPost()){
  52. $param = input('post.');
  53. $param['content'] = trim($param['content']);
  54. $has = db('words')->field('id')->where(['title' => $param['title'], 'user_id' => 0])->find();
  55. if(!empty($has)){
  56. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  57. }
  58. $param['update_time'] = date('Y-m-d H:i:s');
  59. $param['admin_id'] = session('user_id');
  60. try{
  61. db('words')->insert($param);
  62. }catch(\Exception $e){
  63. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  64. }
  65. return json(['code' => 1, 'data' => '', 'msg' => '添加常用语成功']);
  66. }
  67. $this->assign([
  68. 'status' => config('kf_status')
  69. ]);
  70. return $this->fetch('addword');
  71. }
  72. // 编辑常用语
  73. public function editWord()
  74. {
  75. if(request()->isAjax()){
  76. $param = input('post.');
  77. $param['content'] = trim($param['content']);
  78. $param['update_time'] = date('Y-m-d H:i:s');
  79. $param['admin_id'] = session('user_id');
  80. // 检测用户修改的常用语是否重复
  81. $has = db('words')->where(['title' => $param['title'], 'user_id' => 0])->where('id', '<>', $param['id'])->find();
  82. if(!empty($has)){
  83. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  84. }
  85. try{
  86. db('words')->where('id', $param['id'])->update($param);
  87. }catch(\Exception $e){
  88. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  89. }
  90. return json(['code' => 1, 'data' => '', 'msg' => '编辑常用语成功']);
  91. }
  92. $id = input('param.id/d');
  93. $info = db('words')->where('id', $id)->find();
  94. $this->assign([
  95. 'info' => $info,
  96. 'status' => config('kf_status')
  97. ]);
  98. return $this->fetch('editword');
  99. }
  100. // 删除常用语
  101. public function delWord()
  102. {
  103. if(request()->isAjax()){
  104. $id = input('param.id/d');
  105. try{
  106. db('words')->where('id', $id)->delete();
  107. }catch(\Exception $e){
  108. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  109. }
  110. return json(['code' => 1, 'data' => '', 'msg' => '删除常用语成功']);
  111. }
  112. }
  113. // 生成按钮
  114. private function makeBtn($id)
  115. {
  116. $operate = '<a href="' . url('words/editword', ['id' => $id]) . '">';
  117. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  118. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  119. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  120. return $operate;
  121. }
  122. //导入快捷回复
  123. function inserExcel()
  124. {
  125. Loader::import('PHPExcel.PHPExcel');
  126. Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
  127. Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell');
  128. //获取表单上传文件
  129. $file = request()->file('excel');
  130. $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  131. //上传验证后缀名,以及上传之后移动的地址
  132. if ($info) {
  133. // echo $info->getFilename();
  134. $exclePath = $info->getSaveName(); //获取文件名
  135. $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath; //上传文件的地址
  136. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  137. $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
  138. $excel_array = $obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
  139. array_shift($excel_array); //删除第一个数组(标题);
  140. $info = [];
  141. foreach ($excel_array as $k => $v) {
  142. $info[$k]['title'] = $v[0];
  143. $info[$k]['content'] = $v[1];
  144. $info[$k]['status'] = $v[2];
  145. $info[$k]['update_time'] = date('Y-m-d H:m:s',time());
  146. $info[$k]['user_id'] = 0;
  147. $info[$k]['admin_id'] = session('user_id');
  148. }
  149. //检查表中数据是否有重复
  150. for($a=0;$a<count($info);$a++){
  151. for($b=$a+1;$b<count($info);$b++){
  152. if($info[$a]['title'] == $info[$b]['title']){
  153. return json(['code' => -1, 'data' => url('words/index'), 'msg' => 'excel表第'.($a+1).'行与第'.($b+1).'行快捷词重复']);
  154. }
  155. }
  156. }
  157. //检查表格中数据是否已存在与数据库
  158. $words = db('words')->where('user_id',0)->select();
  159. for($i=0;$i<count($info);$i++){
  160. for($j=0;$j<count($words);$j++){
  161. if($info[$i]['title'] == $words[$j]['title']){
  162. return json(['code' => -1, 'data' => url('words/index'), 'msg' => 'excel表第'.($i+1).'行快捷回复已存在']);
  163. }
  164. }
  165. }
  166. db('words')->insertAll($info); //批量插入数据
  167. return json(['code' => 1, 'data' => $this->redirect('words/index'), 'msg' => '导入快捷回复成功']);
  168. } else {
  169. return json(['code' => -2, 'data' => url('words/index'), 'msg' => '导入快捷回复失败']);
  170. }
  171. }
  172. }