Words.php 4.2 KB

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