0
0

GreenCate.php 3.0 KB

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