0
0

DepCate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. //数据返回
  37. $totalCount = db($this->table)->where($map)->count();
  38. $totalPage = ceil($totalCount/$length);
  39. $result['page'] = $page;
  40. $result['total'] = $totalPage;
  41. $result['records'] = $totalCount;
  42. $result['rows'] = $lists;
  43. return json($result);
  44. }else{
  45. return $this->fetch();
  46. }
  47. }
  48. /**
  49. * 新增/编辑
  50. */
  51. public function add($id=0){
  52. if(request()->isPost()){
  53. $res = $this->model->updates();
  54. if($res){
  55. $this->success('操作成功',url('index'));
  56. }else{
  57. $this->error($this->model->getError());
  58. }
  59. }else{
  60. if($id){
  61. $info =db($this->table)->where('id',$id)->find();
  62. $this->assign('info',$info);
  63. }
  64. return $this->fetch();
  65. }
  66. }
  67. /**
  68. * 删除记录
  69. * @param int $id
  70. */
  71. public function del($id=0){
  72. if(!$id){
  73. $this->error('参数错误');
  74. }
  75. $dep = Db::name('dep')
  76. ->where('cate_id',$id)
  77. ->where('del',0)
  78. ->find();
  79. if($dep){
  80. $this->error('当前分类下有部门,暂不能删除');
  81. }
  82. $res = db($this->table)->where('id',$id)->setField('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($this->table)->where('id',$id)->setField($fn,$fv);
  100. if($res){
  101. $this->success('操作成功');
  102. }else{
  103. $this->error('操作失败');
  104. }
  105. }
  106. public function batchSort(){
  107. $data = input('data','','trim');
  108. if(!$data){
  109. $this->error('参数错误');
  110. }
  111. $data = json_decode($data,true);
  112. if(!$data){
  113. $this->error('参数错误');
  114. }
  115. Db::startTrans();
  116. try{
  117. foreach ($data as $k=>$v){
  118. Db::name('dep')->where('id',$v['id'])->setField('sort',$v['sort']);
  119. }
  120. Db::commit();
  121. }catch (Exception $e){
  122. Db::rollback();
  123. $this->error('操作失败');
  124. }
  125. $this->success('操作成功');
  126. }
  127. }