Words.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. class Words extends Base
  9. {
  10. // 常用语列表
  11. public function index()
  12. {
  13. if(request()->isAjax()){
  14. $param = input('param.');
  15. $limit = $param['pageSize'];
  16. $offset = ($param['pageNumber'] - 1) * $limit;
  17. $where['user_id'] = $param['type'] == 1? 0 : ['neq',0];
  18. if (!empty($param['searchText'])) {
  19. $where['content'] = $param['searchText'];
  20. }
  21. $result = db('words')
  22. ->field(['a.id', 'title', 'content', 'update_time', 'user_name', 'a.status'])
  23. ->alias('a')
  24. ->join('admins b', 'a.admin_id = b.id','left')
  25. ->where($where)
  26. ->limit($offset, $limit)
  27. ->select();
  28. foreach($result as $key=>$vo){
  29. // 优化显示状态
  30. if(1 == $vo['status']){
  31. $result[$key]['status'] = '<span class="label label-primary">启用</span>';
  32. }else{
  33. $result[$key]['status'] = '<span class="label label-danger">禁用</span>';
  34. }
  35. if ($param['type'] == 1) {
  36. // 生成操作按钮
  37. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  38. }
  39. }
  40. $return['total'] = db('words')->where($where)->count(); //总数据
  41. $return['rows'] = $result;
  42. return json($return);
  43. }
  44. return $this->fetch();
  45. }
  46. // 添加常用语
  47. public function addWord()
  48. {
  49. if(request()->isPost()){
  50. $param = input('post.');
  51. $param['content'] = trim($param['content']);
  52. $has = db('words')->field('id')->where(['title' => $param['title'], 'user_id' => 0])->find();
  53. if(!empty($has)){
  54. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  55. }
  56. $param['update_time'] = date('Y-m-d H:i:s');
  57. $param['admin_id'] = session('user_id');
  58. try{
  59. db('words')->insert($param);
  60. }catch(\Exception $e){
  61. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  62. }
  63. return json(['code' => 1, 'data' => '', 'msg' => '添加常用语成功']);
  64. }
  65. $this->assign([
  66. 'status' => config('kf_status')
  67. ]);
  68. return $this->fetch('addword');
  69. }
  70. // 编辑常用语
  71. public function editWord()
  72. {
  73. if(request()->isAjax()){
  74. $param = input('post.');
  75. $param['content'] = trim($param['content']);
  76. $param['update_time'] = date('Y-m-d H:i:s');
  77. $param['admin_id'] = session('user_id');
  78. // 检测用户修改的常用语是否重复
  79. $has = db('words')->where(['title' => $param['title'], 'user_id' => 0])->where('id', '<>', $param['id'])->find();
  80. if(!empty($has)){
  81. return json(['code' => -1, 'data' => '', 'msg' => '该常用语已经存在']);
  82. }
  83. try{
  84. db('words')->where('id', $param['id'])->update($param);
  85. }catch(\Exception $e){
  86. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  87. }
  88. return json(['code' => 1, 'data' => '', 'msg' => '编辑常用语成功']);
  89. }
  90. $id = input('param.id/d');
  91. $info = db('words')->where('id', $id)->find();
  92. $this->assign([
  93. 'info' => $info,
  94. 'status' => config('kf_status')
  95. ]);
  96. return $this->fetch('editword');
  97. }
  98. // 删除常用语
  99. public function delWord()
  100. {
  101. if(request()->isAjax()){
  102. $id = input('param.id/d');
  103. try{
  104. db('words')->where('id', $id)->delete();
  105. }catch(\Exception $e){
  106. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  107. }
  108. return json(['code' => 1, 'data' => '', 'msg' => '删除常用语成功']);
  109. }
  110. }
  111. // 生成按钮
  112. private function makeBtn($id)
  113. {
  114. $operate = '<a href="' . url('words/editword', ['id' => $id]) . '">';
  115. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  116. $operate .= '<a href="javascript:userDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  117. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  118. return $operate;
  119. }
  120. }