MenuLogic.php 5.4 KB

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