* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ declare(strict_types=1); namespace App\Service; use App\Repository\Company\CompanyModulesRepository; use Hyperf\Config\Annotation\Value; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Router\Dispatched; use Psr\Http\Message\ServerRequestInterface; use function Hyperf\Collection\collect; /** * Author: ykxiao * Date: 2025/6/4 * Time: 上午10:19 * Description: 系统服务 * * (c) ykxiao * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ class SysService { #[Inject] protected RelatedInfo $relatedInfo; #[Inject] protected CompanyModulesRepository $companyModulesRepository; #[Value('menu.menu')] protected array $menu; #[Value('menu.data_permissions')] protected array $dataPermissions; public function getDefaultMenu(): array { $list = treeToList($this->menu); foreach ($list as $item => $value) { $code = $value['meta']['code']; $value['meta']['data_permission'] = []; if (array_key_exists($code, $this->dataPermissions)) { $value['meta']['data_permission'] = $this->dataPermissions[$code]; } $list[$item] = $value; } return $list; } /** * 获取当前方法名 * @param ServerRequestInterface $request * @return string */ public function method(ServerRequestInterface $request): string { $dispatched = $request->getAttribute(Dispatched::class); $method = null; if ($dispatched instanceof Dispatched) { $handler = $dispatched->handler->callback ?? null; if ($handler) { $method = $handler[1]; } } return $method; } /** * 获取当前用户的角色权限。 * @param array $user * @return array */ public function roleAction(array $user): array { // 获取用户角色拥有的菜单权限 $default_menu = (new SysService())->getDefaultMenu(); return collect($default_menu) ->whereIn('meta.code', $this->menuPermissions($user)) ->pluck('actions') ->collapse() ->toArray(); } /** * 获取用户的菜单权限。 * * @param mixed $user 用户信息,预期包含用户ID * @return array 返回一个包含用户所有菜单权限的数组 */ public function menuPermissions(mixed $user): array { // 获取用户角色的所有权限 $role_permissions = $this->relatedInfo->getRolePermissions($user['id']); // 收集角色权限码,去除重复,转换为数组格式后返回 return $role_permissions ->pluck('role_permission_code') ->collapse() ->unique() ->toArray(); } /** * 菜单权限。 * 该函数用于获取指定用户的菜单权限。 * * @param mixed $user 用户信息,预期为包含用户ID的数组或对象 * @return array 返回一个包含用户菜单权限动作的数组 */ public function dataPermissions(mixed $user): array { $dataPermissions = $this->relatedInfo->dataPermissions($user['id']); // 获取并转换用户的所有数据权限 return collect($dataPermissions) ->pluck('data_permissions') ->collapse() ->toArray(); } /** * 获取客户端IP信息 * @param ServerRequestInterface $request * @return string */ public function getClientIpInfo(ServerRequestInterface $request): string { // 尝试从请求头获取客户端真实IP或转发的IP $realIP = $request->getHeaderLine('x-real-ip'); $forwardedFor = $request->getHeaderLine('x-forwarded-for'); // 确定客户端IP,优先使用 x-real-ip,其次是 x-forwarded-for,最后是默认IP $clientIP = $realIP ?: $forwardedFor; return $clientIP ?: '127.0.0.1'; } /** * 验证平台管理员权限。 */ public function verifyPlatformAdmin(mixed $user, mixed $request): bool { $companyInfo = $user['company_info']; $module = $this->companyModulesRepository->codeList($companyInfo['id']); // 菜单权限 $default_menu = (new SysService())->getDefaultMenu(); $role_permissions_result = collect($default_menu) ->whereIn('meta.code', $module['role_permission_code']) ->flatMap(fn($item) => $item['actions']) ->toArray(); $data_permissions_result = collect($module['data_permission_code']) ->pluck('actions') ->toArray(); $method = $this->method($request); if ($method) { return in_array($method, $role_permissions_result, true) || in_array($method, $data_permissions_result, true); } return false; } }