Words.php 4.1 KB

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