Words.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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(1 == $vo['status']){
  38. $result[$key]['status'] = '<span style="color: #2fbe1b">启用</span>';
  39. }else{
  40. $result[$key]['status'] = '<span style="color: red">禁用</span>';
  41. }
  42. if ($param['type'] == 1) {
  43. // 生成操作按钮
  44. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  45. }
  46. }
  47. $return['total'] = db('words')->where($where)->count(); //总数据
  48. $return['rows'] = $result;
  49. return json($return);
  50. }
  51. return $this->fetch();
  52. }
  53. // 添加常用语
  54. public function addWord()
  55. {
  56. if(request()->isPost()){
  57. $param = input('post.');
  58. $param['content'] = trim($param['content']);
  59. $has = db('words')->field('id')->where(['title' => $param['title'], 'user_id' => 0])->find();
  60. if(!empty($has)){
  61. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  62. }
  63. $param['update_time'] = date('Y-m-d H:i:s');
  64. $param['admin_id'] = session('user_id');
  65. try{
  66. db('words')->insert($param);
  67. }catch(\Exception $e){
  68. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  69. }
  70. return json(['code' => 1, 'data' => '', 'msg' => '添加常用语成功']);
  71. }
  72. $this->assign([
  73. 'status' => config('kf_status')
  74. ]);
  75. return $this->fetch('addword');
  76. }
  77. // 编辑常用语
  78. public function editWord()
  79. {
  80. if(request()->isAjax()){
  81. $param = input('post.');
  82. $param['content'] = trim($param['content']);
  83. $param['update_time'] = date('Y-m-d H:i:s');
  84. $param['admin_id'] = session('user_id');
  85. // 检测用户修改的常用语是否重复
  86. $has = db('words')->where(['title' => $param['title'], 'user_id' => 0])->where('id', '<>', $param['id'])->find();
  87. if(!empty($has)){
  88. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  89. }
  90. try{
  91. db('words')->where('id', $param['id'])->update($param);
  92. }catch(\Exception $e){
  93. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  94. }
  95. return json(['code' => 1, 'data' => '', 'msg' => '编辑常用语成功']);
  96. }
  97. $id = input('param.id/d');
  98. $info = db('words')->where('id', $id)->find();
  99. $this->assign([
  100. 'info' => $info,
  101. 'status' => config('kf_status')
  102. ]);
  103. return $this->fetch('editword');
  104. }
  105. // 删除快捷回复
  106. public function delWord()
  107. {
  108. if(request()->isAjax()){
  109. $id = input('param.id/d');
  110. try{
  111. db('words')->where('id', $id)->delete();
  112. }catch(\Exception $e){
  113. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  114. }
  115. return json(['code' => 1, 'data' => '', 'msg' => '删除常用语成功']);
  116. }
  117. }
  118. // 删除全部快捷回复
  119. public function delAll()
  120. {
  121. if(request()->isAjax()){
  122. try{
  123. db('words')->where('user_id',0)->delete();
  124. }catch(\Exception $e){
  125. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  126. }
  127. return json(['code' => 1, 'data' => '', 'msg' => '删除全部快捷回复成功']);
  128. }
  129. }
  130. // 生成按钮
  131. private function makeBtn($id)
  132. {
  133. $operate = '<a href="' . url('words/editword', ['id' => $id]) . '">';
  134. $operate .= '<button type="button" class="btn btn-primary btn-sm"> 编辑</button></a> ';
  135. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  136. $operate .= ' 删除</button></a> ';
  137. return $operate;
  138. }
  139. //导入快捷回复
  140. function inserExcel()
  141. {
  142. Loader::import('PHPExcel.PHPExcel');
  143. Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
  144. Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell');
  145. //获取表单上传文件
  146. $file = request()->file('excel');
  147. if(empty($file)){
  148. $this->error('请先上传文件');
  149. return json(['code' => -4, 'data' => '', 'msg' => '请先上传文件']);
  150. }
  151. $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  152. //上传验证后缀名,以及上传之后移动的地址
  153. if ($info) {
  154. // echo $info->getFilename();
  155. $exclePath = $info->getSaveName(); //获取文件名
  156. $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath; //上传文件的地址
  157. $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
  158. $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
  159. $excel_array = $obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
  160. array_shift($excel_array); //删除第一个数组(标题);
  161. $info = [];
  162. foreach ($excel_array as $k => $v) {
  163. $info[$k]['title'] = $v[0];
  164. $info[$k]['content'] = $v[1];
  165. $info[$k]['status'] = $v[2];
  166. $info[$k]['update_time'] = date('Y-m-d H:i:s',time());
  167. $info[$k]['user_id'] = 0;
  168. $info[$k]['admin_id'] = session('user_id');
  169. }
  170. //检查表中数据是否为空和重复
  171. for($a=0;$a<count($info);$a++){
  172. if(empty($info[$a]['title'])){
  173. $this->error('excel表第'.($a+2).'行快捷词为空');
  174. return json(['code' => -3, 'data' => url('words/index'), 'msg' => 'excel表第'.($a+2).'行快捷词为空']);
  175. }
  176. if(empty($info[$a]['content'])){
  177. $this->error('excel表第'.($a+2).'行内容为空');
  178. return json(['code' => -4, 'data' => url('words/index'), 'msg' => 'excel表第'.($a+2).'行内容为空']);
  179. }
  180. for($b=$a+1;$b<count($info);$b++){
  181. if($info[$a]['title'] == $info[$b]['title']){
  182. $this->error('excel表第'.($a+2).'行与第'.($b+2).'行快捷词重复');
  183. return json(['code' => -1, 'data' => url('words/index'), 'msg' => 'excel表第'.($a+2).'行与第'.($b+2).'行快捷词重复']);
  184. }
  185. }
  186. }
  187. //检查表格中数据是否已存在与数据库
  188. $words = db('words')->where('user_id',0)->select();
  189. for($i=0;$i<count($info);$i++){
  190. for($j=0;$j<count($words);$j++){
  191. if($info[$i]['title'] == $words[$j]['title']){
  192. $this->error('excel表第'.($i+2).'行快捷回复已存在');
  193. return json(['code' => -1, 'data' => url('words/index'), 'msg' => 'excel表第'.($i+2).'行快捷回复已存在']);
  194. }
  195. }
  196. }
  197. db('words')->insertAll($info); //批量插入数据
  198. $this->success('导入快捷回复成功');
  199. return json(['code' => 1, 'data' => $this->redirect('words/index'), 'msg' => '导入快捷回复成功']);
  200. } else {
  201. $this->error('导入快捷回复失败');
  202. return json(['code' => -2, 'data' => url('words/index'), 'msg' => '导入快捷回复失败']);
  203. }
  204. }
  205. }