0
0

Menu.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\controller;
  3. class Menu extends Auth
  4. {
  5. /**
  6. * 菜单管理页
  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','sort','trim'); //排序列
  17. $sort = input('sord','asc','trim'); //排序方式
  18. $order = $sortRow.' '.$sort;
  19. $map['pid'] = $pid;
  20. $map['del'] = 0;
  21. //数据查询
  22. $lists = db('menu')->where($map)->limit($start,$length)->order($order)->select();
  23. foreach ($lists as $k=>$v){
  24. if($v['pid']){
  25. $lists[$k]['ptitle'] = db('menu')->where('id',$v['pid'])->value('title');
  26. }else{
  27. $lists[$k]['ptitle'] = '无';
  28. }
  29. }
  30. //数据返回
  31. $totalCount = db('menu')->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('menu')->where('id',$pid)->value('title').']子菜单列表';
  41. $ppid = db('menu')->where('id',$pid)->value('pid');
  42. }else{
  43. $ptitle = '菜单列表';
  44. $ppid = 0;
  45. }
  46. $this->assign('meta_title',$ptitle);
  47. $this->assign('pid',$pid);
  48. $this->assign('ppid',$ppid);
  49. return $this->fetch();
  50. }
  51. }
  52. /**
  53. * 新增/编辑
  54. */
  55. public function add($id=0,$pid=0){
  56. if(request()->isPost()){
  57. $res = model('Menu')->updates();
  58. if($res){
  59. $this->success('操作成功',url('index',array('pid'=>$pid)));
  60. }else{
  61. $this->error(model('Menu')->getError());
  62. }
  63. }else{
  64. $meta_title = '新增菜单';
  65. if($id){
  66. $info = db('menu')->where('id',$id)->find();
  67. $this->assign('info',$info);
  68. $meta_title = '编辑菜单';
  69. }
  70. $list = get_menu();
  71. $this->assign('list',$list);
  72. $this->assign('pid',$pid);
  73. $this->assign('meta_title',$meta_title);
  74. return $this->fetch();
  75. }
  76. }
  77. /**
  78. * 删除记录
  79. * @param int $id
  80. */
  81. public function del($id=0){
  82. if(!$id){
  83. $this->error('参数错误');
  84. }
  85. $map['pid'] = $id;
  86. $map['del'] = 0;
  87. $subid = db('menu')->where($map)->value('id');
  88. if($subid){
  89. $this->error('有下级不能删除');
  90. }
  91. $res = db('menu')->where('id',$id)->update(['del'=>1]);
  92. if($res){
  93. $this->success('删除成功');
  94. }else{
  95. $this->error('删除失败');
  96. }
  97. }
  98. /**
  99. * 改变字段值
  100. * @param int $fv
  101. * @param string $fn
  102. * @param int $fv
  103. */
  104. public function changeField($id=0,$fn='',$fv=0){
  105. if(!$fn||!$id){
  106. $this->error('参数错误');
  107. }
  108. $res = db('menu')->where('id',$id)->setField($fn,$fv);
  109. if($res){
  110. $this->success('操作成功');
  111. }else{
  112. $this->error('操作失败');
  113. }
  114. }
  115. /**
  116. * 排序
  117. * @param int $id
  118. * @param int $sort
  119. */
  120. public function changeSort($id=0,$sort=0){
  121. if($id<0||$sort<0){
  122. $this->error('参数错误');
  123. }
  124. $res = db('menu')->where('id',$id)->setField('sort',$sort);
  125. if($res){
  126. $this->success('操作成功');
  127. }else{
  128. $this->error('操作失败');
  129. }
  130. }
  131. }