MenuLogic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. $admin = Admin::findOrEmpty($adminId);
  40. $where = [];
  41. $where[] = ['type', 'in', ['M', 'C']];
  42. $where[] = ['is_disable', '=', 0];
  43. if ($admin['root'] != 1) {
  44. $roleMenu = SystemRoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id');
  45. $where[] = ['id', 'in', $roleMenu];
  46. }
  47. $menu = SystemMenu::where($where)
  48. ->order(['sort' => 'desc', 'id' => 'asc'])
  49. ->select();
  50. return linear_to_tree($menu, 'children');
  51. }
  52. /**
  53. * @notes 添加菜单
  54. * @param array $params
  55. * @return SystemMenu|\think\Model
  56. * @author 段誉
  57. * @date 2022/6/30 10:06
  58. */
  59. public static function add(array $params)
  60. {
  61. return SystemMenu::create([
  62. 'pid' => $params['pid'],
  63. 'type' => $params['type'],
  64. 'name' => $params['name'],
  65. 'icon' => $params['icon'] ?? '',
  66. 'sort' => $params['sort'],
  67. 'perms' => $params['perms'] ?? '',
  68. 'paths' => $params['paths'] ?? '',
  69. 'component' => $params['component'] ?? '',
  70. 'selected' => $params['selected'] ?? '',
  71. 'params' => $params['params'] ?? '',
  72. 'is_cache' => $params['is_cache'],
  73. 'is_show' => $params['is_show'],
  74. 'is_disable' => $params['is_disable'],
  75. ]);
  76. }
  77. /**
  78. * @notes 编辑菜单
  79. * @param array $params
  80. * @return SystemMenu
  81. * @author 段誉
  82. * @date 2022/6/30 10:07
  83. */
  84. public static function edit(array $params)
  85. {
  86. return SystemMenu::update([
  87. 'id' => $params['id'],
  88. 'pid' => $params['pid'],
  89. 'type' => $params['type'],
  90. 'name' => $params['name'],
  91. 'icon' => $params['icon'] ?? '',
  92. 'sort' => $params['sort'],
  93. 'perms' => $params['perms'] ?? '',
  94. 'paths' => $params['paths'] ?? '',
  95. 'component' => $params['component'] ?? '',
  96. 'selected' => $params['selected'] ?? '',
  97. 'params' => $params['params'] ?? '',
  98. 'is_cache' => $params['is_cache'],
  99. 'is_show' => $params['is_show'],
  100. 'is_disable' => $params['is_disable'],
  101. ]);
  102. }
  103. /**
  104. * @notes 详情
  105. * @param $params
  106. * @return array
  107. * @author 段誉
  108. * @date 2022/6/30 9:54
  109. */
  110. public static function detail($params)
  111. {
  112. return SystemMenu::findOrEmpty($params['id'])->toArray();
  113. }
  114. /**
  115. * @notes 删除菜单
  116. * @param $params
  117. * @author 段誉
  118. * @date 2022/6/30 9:47
  119. */
  120. public static function delete($params)
  121. {
  122. // 删除菜单
  123. SystemMenu::destroy($params['id']);
  124. // 删除角色-菜单表中 与该菜单关联的记录
  125. SystemRoleMenu::where(['menu_id' => $params['id']])->delete();
  126. }
  127. /**
  128. * @notes 更新状态
  129. * @param array $params
  130. * @return SystemMenu
  131. * @author 段誉
  132. * @date 2022/7/6 17:02
  133. */
  134. public static function updateStatus(array $params)
  135. {
  136. return SystemMenu::update([
  137. 'id' => $params['id'],
  138. 'is_disable' => $params['is_disable']
  139. ]);
  140. }
  141. /**
  142. * @notes 全部数据
  143. * @return array
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @author 段誉
  148. * @date 2022/10/13 11:03
  149. */
  150. public static function getAllData()
  151. {
  152. $data = SystemMenu::where(['is_disable' => YesNoEnum::NO])
  153. ->field('id,pid,name')
  154. ->order(['sort' => 'desc', 'id' => 'desc'])
  155. ->select()
  156. ->toArray();
  157. return linear_to_tree($data, 'children');
  158. }
  159. }