NoticeCate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class NoticeCate extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sort','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['name','like','%'.$title.'%'];
  19. }
  20. $map[] = ['del','=',0];
  21. $map= empty($map) ? true: $map;
  22. //数据查询
  23. $lists = Db::name('notice_cate')->where($map)->limit($start,$length)->order('id desc')->select();
  24. //数据返回
  25. $totalCount = Db::name('notice_cate')->where($map)->count();
  26. $totalPage = ceil($totalCount/$length);
  27. $result['page'] = $page;
  28. $result['total'] = $totalPage;
  29. $result['records'] = $totalCount;
  30. $result['rows'] = $lists;
  31. return json($result);
  32. }else{
  33. return $this->fetch();
  34. }
  35. }
  36. /**
  37. * 新增/编辑
  38. */
  39. public function add($id=0){
  40. if(request()->isPost()){
  41. $res = model('NoticeCate')->updates();
  42. if($res){
  43. $this->success('操作成功',url('index'));
  44. }else{
  45. $this->error(model('NoticeCate')->getError());
  46. }
  47. }else{
  48. if($id){
  49. $info = Db::name('notice_cate')->where('id',$id)->find();
  50. $this->assign('info',$info);
  51. }
  52. return $this->fetch();
  53. }
  54. }
  55. /**
  56. * 删除记录
  57. * @param int $id
  58. */
  59. public function del($id=0){
  60. if(!$id){
  61. $this->error('参数错误');
  62. }
  63. $res = Db::name('notice_cate')->where('id',$id)->setField('del',1);
  64. if($res){
  65. $this->success('删除成功');
  66. }else{
  67. $this->error('删除失败');
  68. }
  69. }
  70. /**
  71. * 改变字段值
  72. * @param int $fv
  73. * @param string $fn
  74. * @param int $fv
  75. */
  76. public function changeField($id=0,$fn='',$fv=0){
  77. if(!$fn||!$id){
  78. $this->error('参数错误');
  79. }
  80. $res = Db::name('notice_cate')->where('id',$id)->setField($fn,$fv);
  81. if($res){
  82. $this->success('操作成功');
  83. }else{
  84. $this->error('操作失败');
  85. }
  86. }
  87. }