Role.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\service\store;
  13. use app\common\service\BaseService;
  14. use app\store\model\store\Menu as MenuModel;
  15. use app\store\model\store\UserRole as UserRoleModel;
  16. use app\store\model\store\RoleMenu as RoleMenuModel;
  17. use app\store\service\store\User as UserService;
  18. /**
  19. * 商家后台角色服务类
  20. * Class Role
  21. * @package app\store\service\store
  22. */
  23. class Role extends BaseService
  24. {
  25. /**
  26. * 获取当前登录用户菜单权限
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public static function getLoginPermissions()
  33. {
  34. // 获取当前登录用户的ID
  35. $userInfo = UserService::getLoginInfo();
  36. // 根据当前用户ID获取有权限的菜单列表
  37. $permittedMenuList = static::getPermittedMenuList((int)$userInfo['user']['store_user_id']);
  38. // 生成权限列表
  39. $permissions = static::buildPermissions($permittedMenuList);
  40. return [
  41. // 是否为超级管理员, 拥有所有权限
  42. 'isSuper' => $userInfo['user']['is_super'],
  43. // 权限列表
  44. 'permissions' => $permissions
  45. ];
  46. }
  47. /**
  48. * 生成权限列表
  49. * @param $menuList
  50. * @return array
  51. */
  52. private static function buildPermissions($menuList)
  53. {
  54. $data = [];
  55. foreach ($menuList as $menu) {
  56. $data[] = [
  57. // 菜单唯一标示
  58. 'permissionId' => $menu['path'],
  59. // 菜单名称
  60. 'name' => $menu['name'],
  61. // 页面操作项 例如: 新增 编辑 删除
  62. 'actionEntitySet' => static::getActionEntitySet($menu)
  63. ];
  64. !empty($menu['children']) && $data = array_merge($data, static::buildPermissions($menu['children']));
  65. }
  66. return $data;
  67. }
  68. /**
  69. * 整理页面操作项
  70. * @param $menu
  71. * @return array
  72. */
  73. private static function getActionEntitySet($menu)
  74. {
  75. if (!isset($menu['actions']) || empty($menu['actions'])) {
  76. return [];
  77. }
  78. $actionEntitySet = [];
  79. foreach ($menu['actions'] as $action) {
  80. $actionEntitySet[] = [
  81. 'describe' => $action['name'],
  82. 'action' => $action['action_mark'],
  83. ];
  84. }
  85. return $actionEntitySet;
  86. }
  87. private static function filterChildrenAction($menuList)
  88. {
  89. $list = [];
  90. foreach ($menuList as $item) {
  91. if ($item['module'] != 10) continue;
  92. if (!empty($item['children'])) {
  93. // 整理actions
  94. $item['actions'] = array_filter($item['children'], function ($val) {
  95. return $val['module'] == 20;
  96. });
  97. // 整理children
  98. $item['children'] = static::filterChildrenAction($item['children']);
  99. }
  100. $list[] = $item;
  101. }
  102. return $list;
  103. }
  104. /**
  105. * 根据指定用户ID获取有权限的菜单列表
  106. * @param $storeUserId
  107. * @return mixed
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. private static function getPermittedMenuList(int $storeUserId)
  113. {
  114. // 获取指定用户的所有角色ID
  115. $roleIds = UserRoleModel::getRoleIdsByUserId($storeUserId);
  116. // 根据角色ID集获取菜单列表集
  117. $menuIds = RoleMenuModel::getMenuIds($roleIds);
  118. // 获取指定角色ID的菜单列表
  119. $menuList = MenuModel::getListByIds($menuIds);
  120. // 整理菜单列表的actions
  121. return static::filterChildrenAction($menuList);
  122. }
  123. }