SystemRoleServices.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\Request;
  13. use app\services\BaseServices;
  14. use app\dao\system\SystemRoleDao;
  15. use app\services\store\SystemStoreStaffServices;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\utils\ApiErrorCode;
  18. use crmeb\services\CacheService;
  19. /**
  20. * Class SystemRoleServices
  21. * @package app\services\system
  22. * @mixin SystemRoleDao
  23. */
  24. class SystemRoleServices extends BaseServices
  25. {
  26. /**
  27. * 当前管理员权限缓存前缀
  28. */
  29. const ADMIN_RULES_LEVEL = 'Admin_rules_level_';
  30. /**
  31. * SystemRoleServices constructor.
  32. * @param SystemRoleDao $dao
  33. */
  34. public function __construct(SystemRoleDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取权限
  40. * @return mixed
  41. */
  42. public function getRoleArray(array $where = [], string $field = '', string $key = '')
  43. {
  44. return $this->dao->getRoule($where, $field, $key);
  45. }
  46. /**
  47. * 获取表单所需的权限名称列表
  48. * @param int $level
  49. * @param int $type
  50. * @param int $relation_id
  51. * @return array
  52. */
  53. public function getRoleFormSelect(int $level, int $type = 0, int $relation_id = 0)
  54. {
  55. $list = $this->getRoleArray(['level' => $level, 'type' => $type, 'relation_id' => $relation_id, 'status' => 1]);
  56. $options = [];
  57. foreach ($list as $id => $roleName) {
  58. $options[] = ['label' => $roleName, 'value' => $id];
  59. }
  60. return $options;
  61. }
  62. /**
  63. * 身份管理列表
  64. * @param array $where
  65. * @return array
  66. */
  67. public function getRoleList(array $where)
  68. {
  69. [$page, $limit] = $this->getPageValue();
  70. $list = $this->dao->getRouleList($where, $page, $limit);
  71. $count = $this->dao->count($where);
  72. /** @var SystemMenusServices $service */
  73. $service = app()->make(SystemMenusServices::class);
  74. foreach ($list as &$item) {
  75. $item['rules'] = implode(',', array_merge($service->column(['id' => $item['rules']], 'menu_name', 'id')));
  76. }
  77. return compact('count', 'list');
  78. }
  79. /**
  80. * 后台验证权限
  81. * @param Request $request
  82. */
  83. public function verifiAuth(Request $request)
  84. {
  85. $rule = str_replace('adminapi/', '', trim(strtolower($request->rule()->getRule())));
  86. if (in_array($rule, ['setting/admin/logout', 'menuslist'])) {
  87. return true;
  88. }
  89. $method = trim(strtolower($request->method()));
  90. $auth = $this->getAllRoles(2);
  91. //验证访问接口是否存在
  92. if (!in_array($method . '@@' . $rule, array_map(function ($item) {
  93. return trim(strtolower($item['methods'])). '@@'. trim(strtolower(str_replace(' ', '', $item['api_url'])));
  94. }, $auth))) {
  95. return true;
  96. }
  97. $auth = $this->getRolesByAuth($request->adminInfo()['roles'], 2);
  98. //验证访问接口是否有权限
  99. if ($auth && empty(array_filter($auth, function ($item) use ($rule, $method) {
  100. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  101. return true;
  102. }))) {
  103. throw new AuthException(ApiErrorCode::ERR_AUTH);
  104. }
  105. }
  106. /**
  107. * 获取所有权限
  108. * @param int $auth_type
  109. * @param int $type
  110. * @param string $cachePrefix
  111. * @return array|bool|mixed|null
  112. */
  113. public function getAllRoles(int $auth_type = 1, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  114. {
  115. $cacheName = md5($cachePrefix . '_' . $auth_type . '_' . $type . '_ALl' );
  116. return CacheService::redisHandler('system_menus')->remember($cacheName, function () use ($auth_type, $type) {
  117. /** @var SystemMenusServices $menusService */
  118. $menusService = app()->make(SystemMenusServices::class);
  119. return $menusService->getColumn([['auth_type', '=', $auth_type], ['type', '=', $type]], 'api_url,methods');
  120. });
  121. }
  122. /**
  123. * 获取指定权限
  124. * @param array $roles
  125. * @param int $auth_type
  126. * @param int $type
  127. * @param string $cachePrefix
  128. * @return array|bool|mixed|null
  129. */
  130. public function getRolesByAuth(array $roles, int $auth_type = 1, int $type = 1, string $cachePrefix = self::ADMIN_RULES_LEVEL)
  131. {
  132. if (empty($roles)) return [];
  133. $cacheName = md5($cachePrefix . '_' . $auth_type . '_' . $type . '_' . implode('_', $roles));
  134. CacheService::redisHandler('system_menus')->clear();
  135. return CacheService::redisHandler('system_menus')->remember($cacheName, function () use ($roles, $auth_type, $type) {
  136. /** @var SystemMenusServices $menusService */
  137. $menusService = app()->make(SystemMenusServices::class);
  138. return $menusService->getColumn([['id', 'IN', $this->getRoleIds($roles, $type == 3 ? 'cashier_rules' : 'rules')], ['auth_type', '=', $auth_type], ['type', '=', $type]], 'api_url,methods');
  139. });
  140. }
  141. /**
  142. * 获取权限id
  143. * @param array $roles
  144. * @return array
  145. */
  146. public function getRoleIds(array $roles, string $field = 'rules', string $key = 'id')
  147. {
  148. $rules = $this->dao->getColumn([['id', 'IN', $roles], ['status', '=', '1']], $field, $key);
  149. return $rules ? array_unique(explode(',', implode(',', $rules))) : [];
  150. }
  151. /**
  152. * 门店角色状态更改改变角色下店员、管理员状态
  153. * @param int $store_id
  154. * @param int $role_id
  155. * @param $status
  156. * @return mixed
  157. */
  158. public function setStaffStatus(int $store_id, int $role_id, $status)
  159. {
  160. /** @var SystemStoreStaffServices $storeStaffServices */
  161. $storeStaffServices = app()->make(SystemStoreStaffServices::class);
  162. if ($status) {
  163. return $storeStaffServices->update(['store_id' => $store_id, 'roles' => $role_id, 'is_del' => 0, 'status' => 0], ['status' => 1]);
  164. } else {
  165. return $storeStaffServices->update(['store_id' => $store_id, 'roles' => $role_id, 'status' => 1], ['status' => 0]);
  166. }
  167. }
  168. }