MenuLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\AdminMenuModel;
  4. use app\model\CommonModel;
  5. use think\facade\Db;
  6. use think\response\Json;
  7. class MenuLogic extends BaseLogic
  8. {
  9. //获取用户菜单列表
  10. public static function list(): Json
  11. {
  12. //menu_action是一个视图
  13. $data = Db::name('menu_action')
  14. ->where(['status' => 1, 'cstatus' => 1, 'is_show' => 1])
  15. ->order(['weight' => 'desc', 'id' => 'asc', 'cweight' => 'desc', 'cid' => 'asc'])
  16. ->cursor();
  17. $list = [];
  18. $act = [];
  19. foreach ($data as $value) {
  20. $list[$value['id']]['menu_name'] = $value['menu_name'];
  21. $list[$value['id']]['menu_type'] = $value['menu_type'];
  22. $list[$value['id']]['menu_img'] = $value['menu_img'];
  23. $list[$value['id']]['menu_route'] = $value['menu_route'];
  24. $list[$value['id']]['menu_url'] = $value['menu_url'];
  25. $list[$value['id']]['status'] = $value['status'];
  26. $list[$value['id']]['weight'] = $value['weight'];
  27. $list[$value['id']]['addtime'] = $value['addtime'];
  28. $temp = [];
  29. $temp['menu_name'] = $value['cname'];
  30. $temp['menu_type'] = $value['ctype'];
  31. $temp['menu_img'] = $value['cmenu_img'];
  32. $temp['menu_route'] = $value['cmenu_route'];
  33. $temp['menu_url'] = $value['cmenu_url'];
  34. $temp['status'] = $value['cstatus'];
  35. $temp['weight'] = $value['cweight'];
  36. $temp['addtime'] = $value['caddtime'];
  37. $temp['is_private'] = $value['cprivate'];
  38. $list[$value['id']]['child'][$value['cid']] = $temp;
  39. $act[$value['id']][$value['cid']][] = $value['acode'];
  40. $list[$value['id']]['child'][$value['cid']]['action'] = $act[$value['id']][$value['cid']];
  41. }
  42. array_walk($list, function (&$value) {
  43. $value['child'] = array_values($value['child']);
  44. });
  45. return json_show(CommonModel::$success, '获取成功', array_values($list));
  46. }
  47. //获取全部菜单
  48. public static function all(): Json
  49. {
  50. $list = AdminMenuModel::field('id,menu_name,menu_type,menu_img,menu_route,menu_url,status,weight,addtime,is_private private')
  51. ->where(['pid' => 0, 'is_show' => AdminMenuModel::$show])
  52. ->append(['child'])
  53. ->withAttr('child', function ($val, $data) {
  54. return AdminMenuModel::field('id,menu_name,menu_type,menu_img,menu_route,menu_url,status,weight,addtime,is_private private,pid')
  55. ->where(['pid' => $data['id'], 'is_show' => AdminMenuModel::$show])
  56. ->order('weight', 'desc')
  57. ->select()
  58. ->toArray();
  59. })
  60. ->order('weight', 'desc')
  61. ->select()
  62. ->toArray();
  63. return json_show(CommonModel::$success, '获取成功', $list);
  64. }
  65. //添加菜单
  66. public static function add(array $data = []): Json
  67. {
  68. $data = array_merge($data, [
  69. 'is_private' => $data['private'],
  70. 'is_show' => AdminMenuModel::$show,
  71. 'status' => AdminMenuModel::$status_normal,
  72. ]);
  73. $res = AdminMenuModel::create($data)->save();
  74. //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
  75. return $res ? json_show(CommonModel::$success, '添加菜单成功') : json_show(CommonModel::$error_param, '添加菜单失败');
  76. }
  77. //编辑菜单
  78. public static function edit(array $data = []): Json
  79. {
  80. $rs = AdminMenuModel::field('id')
  81. ->where(['id' => $data['id']])
  82. ->findOrEmpty()
  83. ->isEmpty();
  84. if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在');
  85. $data = array_merge($data, ['is_private' => $data['private'], 'updatetime' => date('Y-m-d H:i:s')]);
  86. $res = AdminMenuModel::where('id', $data['id'])->strict(false)->save($data);
  87. return $res ? json_show(CommonModel::$success, '编辑菜单成功') : json_show(CommonModel::$error_param, '编辑菜单失败');
  88. }
  89. //删除菜单
  90. public static function delete(int $id = 0): Json
  91. {
  92. $rs = AdminMenuModel::field('id')
  93. ->where(['id' => $id, 'is_show' => AdminMenuModel::$show, 'status' => AdminMenuModel::$status_normal])
  94. ->findOrEmpty()
  95. ->isEmpty();
  96. if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在');
  97. $res = AdminMenuModel::where('id', $id)
  98. ->save([
  99. 'is_show' => AdminMenuModel::$hide,
  100. 'status' => AdminMenuModel::$status_disable
  101. ]);
  102. //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
  103. return $res ? json_show(CommonModel::$success, '删除菜单成功') : json_show(CommonModel::$error_param, '删除菜单失败');
  104. }
  105. //启禁用菜单
  106. public static function status(array $data = []): Json
  107. {
  108. $rs = AdminMenuModel::field('id')
  109. ->where('id', $data['id'])
  110. ->where('status', '<>', $data['status'])
  111. ->findOrEmpty()
  112. ->isEmpty();
  113. if ($rs) return json_show(CommonModel::$error_param, '菜单信息不存在或重复操作');
  114. $res = AdminMenuModel::where('id', $data['id'])
  115. ->where('status', '<>', $data['status'])
  116. ->save(['status' => $data['status']]);
  117. //write_log('菜单{$data['menu_name']}新建成功',$this->userinfo,'order','add','0');
  118. return $res ? json_show(CommonModel::$success, '状态更新成功') : json_show(CommonModel::$error_param, '状态更新失败');
  119. }
  120. }