You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemRoleServices.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\services\system;
  3. use app\Request;
  4. use app\services\BaseServices;
  5. use app\dao\system\SystemRoleDao;
  6. use app\services\store\SystemStoreStaffServices;
  7. use crmeb\exceptions\AuthException;
  8. use crmeb\utils\ApiErrorCode;
  9. use crmeb\services\CacheService;
  10. /**
  11. * Class SystemRoleServices
  12. * @package app\services\system
  13. * @method update($id, array $data, ?string $key = null) 修改数据
  14. * @method save(array $data) 保存数据
  15. * @method get(int $id, ?array $field = []) 获取数据
  16. * @method delete(int $id, ?string $key = null) 删除数据
  17. */
  18. class SystemRoleServices extends BaseServices
  19. {
  20. /**
  21. * 当前管理员权限缓存前缀
  22. */
  23. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  24. /**
  25. * SystemRoleServices constructor.
  26. * @param SystemRoleDao $dao
  27. */
  28. public function __construct(SystemRoleDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取权限
  34. * @return mixed
  35. */
  36. public function getRoleArray(array $where = [], string $field = '', string $key = '')
  37. {
  38. return $this->dao->getRoule($where, $field, $key);
  39. }
  40. /**
  41. * 获取表单所需的权限名称列表
  42. * @param int $level
  43. * @return array
  44. */
  45. public function getRoleFormSelect(int $level, int $type = 1, int $store_id = 0)
  46. {
  47. $list = $this->getRoleArray(['level' => $level, 'type' => $type, 'store_id' => $store_id, 'status' => 1]);
  48. $options = [];
  49. foreach ($list as $id => $roleName) {
  50. $options[] = ['label' => $roleName, 'value' => $id];
  51. }
  52. return $options;
  53. }
  54. /**
  55. * 身份管理列表
  56. * @param array $where
  57. * @return array
  58. */
  59. public function getRoleList(array $where)
  60. {
  61. [$page, $limit] = $this->getPageValue();
  62. $list = $this->dao->getRouleList($where, $page, $limit);
  63. $count = $this->dao->count($where);
  64. /** @var SystemMenusServices $service */
  65. $service = app()->make(SystemMenusServices::class);
  66. foreach ($list as &$item) {
  67. $item['rules'] = implode(',', array_merge($service->column(['id' => $item['rules']], 'menu_name', 'id')));
  68. }
  69. return compact('count', 'list');
  70. }
  71. /**
  72. * 后台验证权限
  73. * @param Request $request
  74. */
  75. public function verifiAuth(Request $request)
  76. {
  77. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  78. $rule = str_replace('adminapi/', '', trim(strtolower($request->rule()->getRule())));
  79. $method = trim(strtolower($request->method()));
  80. if (in_array($rule, ['setting/admin/logout', 'menuslist'])) {
  81. return true;
  82. }
  83. //验证访问接口是否存在
  84. if (!in_array($rule, array_map(function ($item) {
  85. return trim(strtolower(str_replace(' ', '', $item)));
  86. }, array_column($auth, 'api_url')))) {
  87. return true;
  88. }
  89. //验证访问接口是否有权限
  90. if (empty(array_filter($auth, function ($item) use ($rule, $method) {
  91. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  92. return true;
  93. }))) {
  94. throw new AuthException(ApiErrorCode::ERR_AUTH);
  95. }
  96. }
  97. /**
  98. * 获取指定权限
  99. * @param array $rules
  100. * @param int $auth_type
  101. * @param int $type
  102. * @param string $cachePrefix
  103. * @return array|bool|mixed|null
  104. */
  105. public function getRolesByAuth(array $rules, int $auth_type = 1, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  106. {
  107. if (empty($rules)) return [];
  108. $cacheName = md5($cachePrefix . '_' . $auth_type . '_' . $type . '_' . implode('_', $rules));
  109. return CacheService::remember($cacheName, function () use ($rules, $auth_type, $type) {
  110. /** @var SystemMenusServices $menusService */
  111. $menusService = app()->make(SystemMenusServices::class);
  112. return $menusService->getColumn([['id', 'IN', $this->getRoleIds($rules)], ['auth_type', '=', $auth_type], ['type', '=', $type]], 'api_url,methods');
  113. });
  114. }
  115. /**
  116. * 获取权限id
  117. * @param array $rules
  118. * @return array
  119. */
  120. public function getRoleIds(array $rules)
  121. {
  122. $rules = $this->dao->getColumn([['id', 'IN', $rules], ['status', '=', '1']], 'rules', 'id');
  123. return array_unique(explode(',', implode(',', $rules)));
  124. }
  125. /**
  126. * 门店角色状态更改改变角色下店员、管理员状态
  127. * @param int $store_id
  128. * @param int $role_id
  129. * @param $status
  130. * @return mixed
  131. */
  132. public function setStaffStatus(int $store_id, int $role_id, $status)
  133. {
  134. /** @var SystemStoreStaffServices $storeStaffServices */
  135. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  136. if ($status) {
  137. return $storeStaffServices->update(['store_id' => $store_id, 'roles' => $role_id, 'is_del' => 0, 'status' => 0], ['status' => 1]);
  138. } else {
  139. return $storeStaffServices->update(['store_id' => $store_id, 'roles' => $role_id, 'status' => 1], ['status' => 0]);
  140. }
  141. }
  142. }