MenuLogic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\auth;
  15. use app\common\enum\YesNoEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\auth\Admin;
  18. use app\common\model\auth\SystemMenu;
  19. use app\common\model\auth\SystemRoleMenu;
  20. /**
  21. * 系统菜单
  22. * Class MenuLogic
  23. * @package app\adminapi\logic\auth
  24. */
  25. class MenuLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 获取管理员对应的角色菜单
  29. * @param $adminId
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @author 段誉
  35. * @date 2022/7/1 10:50
  36. */
  37. public static function getMenuByAdminId($adminId)
  38. {
  39. // 缓存优化
  40. $data = cache('MenuLogic:getMenuByAdminId:'.$adminId);
  41. if(empty($data)){
  42. $admin = Admin::findOrEmpty($adminId);
  43. $where = [];
  44. $where[] = ['type', 'in', ['M', 'C']];
  45. $where[] = ['is_disable', '=', 0];
  46. if ($admin['root'] != 1) {
  47. $roleMenu = SystemRoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id');
  48. $where[] = ['id', 'in', $roleMenu];
  49. }
  50. $menu = SystemMenu::where($where)
  51. ->order(['sort' => 'desc', 'id' => 'asc'])
  52. ->select();
  53. $data = linear_to_tree($menu, 'children');
  54. cache('MenuLogic:getMenuByAdminId:'.$adminId,$data);
  55. }
  56. return $data;
  57. }
  58. /**
  59. * @notes 添加菜单
  60. * @param array $params
  61. * @return SystemMenu|\think\Model
  62. * @author 段誉
  63. * @date 2022/6/30 10:06
  64. */
  65. public static function add(array $params)
  66. {
  67. return SystemMenu::create([
  68. 'pid' => $params['pid'],
  69. 'type' => $params['type'],
  70. 'name' => $params['name'],
  71. 'icon' => $params['icon'] ?? '',
  72. 'sort' => $params['sort'],
  73. 'perms' => $params['perms'] ?? '',
  74. 'paths' => $params['paths'] ?? '',
  75. 'component' => $params['component'] ?? '',
  76. 'selected' => $params['selected'] ?? '',
  77. 'params' => $params['params'] ?? '',
  78. 'is_cache' => $params['is_cache'],
  79. 'is_show' => $params['is_show'],
  80. 'is_disable' => $params['is_disable'],
  81. ]);
  82. }
  83. /**
  84. * @notes 编辑菜单
  85. * @param array $params
  86. * @return SystemMenu
  87. * @author 段誉
  88. * @date 2022/6/30 10:07
  89. */
  90. public static function edit(array $params)
  91. {
  92. return SystemMenu::update([
  93. 'id' => $params['id'],
  94. 'pid' => $params['pid'],
  95. 'type' => $params['type'],
  96. 'name' => $params['name'],
  97. 'icon' => $params['icon'] ?? '',
  98. 'sort' => $params['sort'],
  99. 'perms' => $params['perms'] ?? '',
  100. 'paths' => $params['paths'] ?? '',
  101. 'component' => $params['component'] ?? '',
  102. 'selected' => $params['selected'] ?? '',
  103. 'params' => $params['params'] ?? '',
  104. 'is_cache' => $params['is_cache'],
  105. 'is_show' => $params['is_show'],
  106. 'is_disable' => $params['is_disable'],
  107. ]);
  108. }
  109. /**
  110. * @notes 详情
  111. * @param $params
  112. * @return array
  113. * @author 段誉
  114. * @date 2022/6/30 9:54
  115. */
  116. public static function detail($params)
  117. {
  118. return SystemMenu::findOrEmpty($params['id'])->toArray();
  119. }
  120. /**
  121. * @notes 删除菜单
  122. * @param $params
  123. * @author 段誉
  124. * @date 2022/6/30 9:47
  125. */
  126. public static function delete($params)
  127. {
  128. // 删除菜单
  129. SystemMenu::destroy($params['id']);
  130. // 删除角色-菜单表中 与该菜单关联的记录
  131. SystemRoleMenu::where(['menu_id' => $params['id']])->delete();
  132. }
  133. /**
  134. * @notes 更新状态
  135. * @param array $params
  136. * @return SystemMenu
  137. * @author 段誉
  138. * @date 2022/7/6 17:02
  139. */
  140. public static function updateStatus(array $params)
  141. {
  142. return SystemMenu::update([
  143. 'id' => $params['id'],
  144. 'is_disable' => $params['is_disable']
  145. ]);
  146. }
  147. /**
  148. * @notes 全部数据
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @author 段誉
  154. * @date 2022/10/13 11:03
  155. */
  156. public static function getAllData()
  157. {
  158. $data = SystemMenu::where(['is_disable' => YesNoEnum::NO])
  159. ->field('id,pid,name')
  160. ->order(['sort' => 'desc', 'id' => 'desc'])
  161. ->select()
  162. ->toArray();
  163. return linear_to_tree($data, 'children');
  164. }
  165. }