DepCate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class DepCate extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='dep_cate';
  11. $this->model= new \app\common\model\DepCate();
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $title = input('title','','trim');
  24. if($title){
  25. $map[] = ['title','like','%'.$title.'%'];
  26. }
  27. $enable = input('enable','','trim');
  28. if($enable != ''){
  29. $map[] = ['enable','=',$enable];
  30. }
  31. $map[] = ['del','=',0];
  32. $map[] = ['org_id','=',$this->orgId];
  33. $map= empty($map) ? true: $map;
  34. //数据查询
  35. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  36. foreach ($lists as $k=>$v){
  37. $deps = Db::name('dep')->where('cate_id',$v['id'])->column('title');
  38. $lists[$k]['deps'] = $deps ?implode(',',$deps) : '';
  39. }
  40. //数据返回
  41. $totalCount = db($this->table)->where($map)->count();
  42. $totalPage = ceil($totalCount/$length);
  43. $result['page'] = $page;
  44. $result['total'] = $totalPage;
  45. $result['records'] = $totalCount;
  46. $result['rows'] = $lists;
  47. return json($result);
  48. }else{
  49. return $this->fetch();
  50. }
  51. }
  52. /**
  53. * 新增/编辑
  54. */
  55. public function add($id=0){
  56. if(request()->isPost()){
  57. $res = $this->model->updates();
  58. if($res){
  59. $this->success('操作成功',url('index'));
  60. }else{
  61. $this->error($this->model->getError());
  62. }
  63. }else{
  64. if($id){
  65. $info =db($this->table)->where('id',$id)->find();
  66. $info['deps'] = Db::name('dep')->where('cate_id',$info['id'])->column('id');
  67. $this->assign('info',$info);
  68. }
  69. $deps = model('Dep')->getList();
  70. $this->assign('depList',$deps);
  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. $dep = Db::name('dep')
  83. ->where('cate_id',$id)
  84. ->where('del',0)
  85. ->find();
  86. if($dep){
  87. $this->error('当前分类下有部门,暂不能删除');
  88. }
  89. $res = db($this->table)->where('id',$id)->setField('del',1);
  90. if($res){
  91. $this->success('删除成功');
  92. }else{
  93. $this->error('删除失败');
  94. }
  95. }
  96. /**
  97. * 改变字段值
  98. * @param int $fv
  99. * @param string $fn
  100. * @param int $fv
  101. */
  102. public function changeField($id=0,$fn='',$fv=0){
  103. if(!$fn||!$id){
  104. $this->error('参数错误');
  105. }
  106. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  107. if($res){
  108. $this->success('操作成功');
  109. }else{
  110. $this->error('操作失败');
  111. }
  112. }
  113. public function batchSort(){
  114. $data = input('data','','trim');
  115. if(!$data){
  116. $this->error('参数错误');
  117. }
  118. $data = json_decode($data,true);
  119. if(!$data){
  120. $this->error('参数错误');
  121. }
  122. Db::startTrans();
  123. try{
  124. foreach ($data as $k=>$v){
  125. Db::name('dep')->where('id',$v['id'])->setField('sort',$v['sort']);
  126. }
  127. Db::commit();
  128. }catch (Exception $e){
  129. Db::rollback();
  130. $this->error('操作失败');
  131. }
  132. $this->success('操作成功');
  133. }
  134. }