Cate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $dep= Db::name('dep')
  39. ->where('org_id','=',$this->orgId)
  40. ->where('del','=',0)
  41. ->where('enable','=',1)
  42. ->column('title','id');
  43. foreach ($lists as $k=>$v){
  44. $depList = explode(',',$v['deps']);
  45. foreach ($depList as $k1=>$v1){
  46. $depList[$k1] = $dep[$v1] ?? '';
  47. }
  48. $lists[$k]['depName'] = implode(',',$depList);
  49. }
  50. //数据返回
  51. $totalCount = Db::name('cate')->alias('c')->Leftjoin('org o','c.org_id=o.id')->where($map)->count();
  52. $totalPage = ceil($totalCount/$length);
  53. $result['page'] = $page;
  54. $result['total'] = $totalPage;
  55. $result['records'] = $totalCount;
  56. $result['rows'] = $lists;
  57. return json($result);
  58. }else{
  59. $this->assign('meta_title','服务列表');
  60. return $this->fetch();
  61. }
  62. }
  63. /**
  64. * 新增/编辑
  65. */
  66. public function add($id=0){
  67. if(request()->isPost()){
  68. $model = new \app\common\model\Cate();
  69. $res = $model->updates($this->orgId);
  70. if($res){
  71. $this->success('操作成功',url('index'));
  72. }else{
  73. $this->error($model->getError());
  74. }
  75. }else{
  76. $meta_title = '新增服务';
  77. if($id){
  78. $info = Db::name('cate')->where('id',$id)->find();
  79. $info['deps'] = $info['deps'] ? explode(',',$info['deps']):[];
  80. $this->assign('info',$info);
  81. $meta_title = '编辑服务';
  82. }
  83. $depList = model('Dep')->getList();
  84. $this->assign('depList', $depList);
  85. $this->assign('meta_title',$meta_title);
  86. return $this->fetch();
  87. }
  88. }
  89. /**
  90. * 删除记录
  91. * @param int $id
  92. */
  93. public function del($id=0){
  94. if(!$id){
  95. $this->error('参数错误');
  96. }
  97. $res = Db::name('cate')->where('id',$id)->update(['del' => 1]);
  98. if($res){
  99. $this->success('删除成功');
  100. }else{
  101. $this->error('删除失败');
  102. }
  103. }
  104. /**
  105. * 改变字段值
  106. * @param int $fv
  107. * @param string $fn
  108. * @param int $fv
  109. */
  110. public function changeField($id=0,$fn='',$fv=0){
  111. if(!$fn||!$id){
  112. $this->error('参数错误');
  113. }
  114. $res = Db::name('cate')->where('id',$id)->update([$fn => $fv]);
  115. if($res){
  116. $this->success('操作成功');
  117. }else{
  118. $this->error('操作失败');
  119. }
  120. }
  121. /**
  122. * 排序
  123. * @param int $id
  124. * @param int $sort
  125. */
  126. public function changeSort($id=0,$sort=0){
  127. if($id<0||$sort<0){
  128. $this->error('参数错误');
  129. }
  130. $res = Db::name('cate')->where('id',$id)->update(['sort'=>$sort]);
  131. if($res){
  132. $this->success('操作成功');
  133. }else{
  134. $this->error('操作失败');
  135. }
  136. }
  137. }