AppIcon.php 4.1 KB

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