Dep.php 4.3 KB

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