Cate.php 3.7 KB

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