菜单绑定系统
This commit is contained in:
@@ -15,7 +15,8 @@ class UserController extends BaseController
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$uid = $request->user->uid;
|
$uid = $request->user->uid;
|
||||||
$result = (new UserService())->getUserInfo($uid);
|
$systemCode = $request->system_code;
|
||||||
|
$result = (new UserService())->getUserInfo($uid, $systemCode);
|
||||||
return $this->success($result, '获取用户信息');
|
return $this->success($result, '获取用户信息');
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return $this->error($e->getMessage());
|
return $this->error($e->getMessage());
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace app\common\logic;
|
|||||||
|
|
||||||
use app\common\model\Corp;
|
use app\common\model\Corp;
|
||||||
use app\common\model\CorpFeishuApp;
|
use app\common\model\CorpFeishuApp;
|
||||||
|
use app\common\model\Resource;
|
||||||
use app\common\model\UserTokenLog;
|
use app\common\model\UserTokenLog;
|
||||||
use app\common\model\Systems;
|
use app\common\model\Systems;
|
||||||
use app\common\model\User;
|
use app\common\model\User;
|
||||||
@@ -140,6 +141,7 @@ class AuthLogic
|
|||||||
$payload = $this->buildJwtPayload($user, $system);
|
$payload = $this->buildJwtPayload($user, $system);
|
||||||
$token = $this->generateAccessToken($payload);
|
$token = $this->generateAccessToken($payload);
|
||||||
$refreshToken = $this->generateRefreshToken($user->id, $data['system_code']);
|
$refreshToken = $this->generateRefreshToken($user->id, $data['system_code']);
|
||||||
|
$homeUrl = $this->getHome($system->id);
|
||||||
|
|
||||||
// 记录 token 到 Redis 和 MySQL
|
// 记录 token 到 Redis 和 MySQL
|
||||||
$this->recordUserToken($user->id, $token, $data['system_code']);
|
$this->recordUserToken($user->id, $token, $data['system_code']);
|
||||||
@@ -150,7 +152,8 @@ class AuthLogic
|
|||||||
'expires_in' => config('jwt.jwt_expiration'),
|
'expires_in' => config('jwt.jwt_expiration'),
|
||||||
'system_code' => $data['system_code'],
|
'system_code' => $data['system_code'],
|
||||||
'corp_abbr' => $data['corp_abbr'],
|
'corp_abbr' => $data['corp_abbr'],
|
||||||
'login_type' => $data['login_type']
|
'login_type' => $data['login_type'],
|
||||||
|
'home_url' => $homeUrl,
|
||||||
// 'payload' => $payload,
|
// 'payload' => $payload,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -500,4 +503,42 @@ class AuthLogic
|
|||||||
Redis::del("user_token_meta:{$token}");
|
Redis::del("user_token_meta:{$token}");
|
||||||
UserTokenLog::where('token', $token)->update(['is_valid'=>0]);
|
UserTokenLog::where('token', $token)->update(['is_valid'=>0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getHome($systemId)
|
||||||
|
{
|
||||||
|
$info = Resource::where('system_id', $systemId)
|
||||||
|
->where('is_home', 1)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$info) {
|
||||||
|
return '/dashboard/console';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递归拼接 URL
|
||||||
|
$url = $this->buildParentUrl($info);
|
||||||
|
|
||||||
|
return $url ?: '/dashboard/console';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归构建父级 URL
|
||||||
|
* @param Resource $resource
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function buildParentUrl(Resource $resource)
|
||||||
|
{
|
||||||
|
$url = $resource->url ?? '';
|
||||||
|
|
||||||
|
// 如果存在父级,则递归拼接
|
||||||
|
if ($resource->parent_id != 0) {
|
||||||
|
$parent = Resource::find($resource->parent_id);
|
||||||
|
if ($parent) {
|
||||||
|
$parentUrl = $this->buildParentUrl($parent);
|
||||||
|
$url = $parentUrl . ($url ? '/' . ltrim($url, '/') : '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,14 +4,17 @@ namespace app\common\logic;
|
|||||||
|
|
||||||
use app\common\model\User;
|
use app\common\model\User;
|
||||||
use app\common\model\Resource;
|
use app\common\model\Resource;
|
||||||
|
use app\common\model\Systems;
|
||||||
|
|
||||||
class PermLogic
|
class PermLogic
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* 获取用户所有资源(合并角色+直接分配),并加入公共菜单,排序按sort字段
|
* 获取用户所有资源(合并角色+直接分配),并加入公共菜单,排序按sort字段
|
||||||
*/
|
*/
|
||||||
public function getUserResources(int $userId)
|
public function getUserResources(int $userId, string $systemCode)
|
||||||
{
|
{
|
||||||
|
$system = Systems::where('code', $systemCode)->first();
|
||||||
|
|
||||||
// 获取用户及其关联角色资源和直接分配的资源
|
// 获取用户及其关联角色资源和直接分配的资源
|
||||||
$user = User::with(['roles.resources', 'resources'])->find($userId);
|
$user = User::with(['roles.resources', 'resources'])->find($userId);
|
||||||
|
|
||||||
@@ -24,9 +27,10 @@ class PermLogic
|
|||||||
// 合并角色资源,排除不需要权限认证的资源
|
// 合并角色资源,排除不需要权限认证的资源
|
||||||
foreach ($user->roles as $role) {
|
foreach ($user->roles as $role) {
|
||||||
$all = $all->merge(
|
$all = $all->merge(
|
||||||
$role->resources->filter(function ($resource) {
|
$role->resources->filter(function ($resource) use($system) {
|
||||||
|
// var_dump($resource->toArray());
|
||||||
// 只保留需要权限认证的资源(或公共资源)
|
// 只保留需要权限认证的资源(或公共资源)
|
||||||
return !$resource->no_auth;
|
return !$resource->no_auth && $system->id == $resource->system_id;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -269,9 +273,9 @@ class PermLogic
|
|||||||
/**
|
/**
|
||||||
* 获取用户所有权限code
|
* 获取用户所有权限code
|
||||||
*/
|
*/
|
||||||
public function getPermissionCodes(int $userId): array
|
public function getPermissionCodes(int $userId, string $systemCode): array
|
||||||
{
|
{
|
||||||
$resources = $this->getUserResources($userId);
|
$resources = $this->getUserResources($userId, $systemCode);
|
||||||
return $resources
|
return $resources
|
||||||
->filter(fn($res) => $res->type === 'permission')
|
->filter(fn($res) => $res->type === 'permission')
|
||||||
->pluck('code')
|
->pluck('code')
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class AuthService
|
|||||||
|
|
||||||
return match ($loginType) {
|
return match ($loginType) {
|
||||||
'feishu' => $logic->feishuLogin($data),
|
'feishu' => $logic->feishuLogin($data),
|
||||||
'qrcode' => $logic->qrCodeLogin($data),
|
// 'qrcode' => $logic->qrCodeLogin($data),
|
||||||
default => $logic->uPwdLogin($data),
|
default => $logic->uPwdLogin($data),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class PermService
|
|||||||
/**
|
/**
|
||||||
* 获取用户所有资源(合并角色+直接分配),结果缓存
|
* 获取用户所有资源(合并角色+直接分配),结果缓存
|
||||||
*/
|
*/
|
||||||
public function getUserResources(int $userId)
|
public function getUserResources(int $userId, string $systemCode)
|
||||||
{
|
{
|
||||||
$cacheKey = self::cacheKey($userId);
|
$cacheKey = self::cacheKey($userId);
|
||||||
$cached = Cache::get($cacheKey);
|
$cached = Cache::get($cacheKey);
|
||||||
@@ -31,7 +31,7 @@ class PermService
|
|||||||
return $cached;
|
return $cached;
|
||||||
}
|
}
|
||||||
// 调用 Logic 层获取资源
|
// 调用 Logic 层获取资源
|
||||||
$resources = $this->logic->getUserResources($userId);
|
$resources = $this->logic->getUserResources($userId, $systemCode);
|
||||||
// 缓存结果
|
// 缓存结果
|
||||||
Cache::set($cacheKey, $resources, 3600);
|
Cache::set($cacheKey, $resources, 3600);
|
||||||
return $resources;
|
return $resources;
|
||||||
@@ -40,19 +40,19 @@ class PermService
|
|||||||
/**
|
/**
|
||||||
* 构建目录/菜单树,节点包含权限与打开方式
|
* 构建目录/菜单树,节点包含权限与打开方式
|
||||||
*/
|
*/
|
||||||
public function getMenuTree(int $userId): array
|
public function getMenuTree(int $userId, string $systemCode): array
|
||||||
{
|
{
|
||||||
// 调用 Logic 层处理菜单构建
|
// 调用 Logic 层处理菜单构建
|
||||||
$resources = $this->getUserResources($userId);
|
$resources = $this->getUserResources($userId, $systemCode);
|
||||||
return $this->logic->buildTree($resources);
|
return $this->logic->buildTree($resources);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户所有权限code
|
* 获取用户所有权限code
|
||||||
*/
|
*/
|
||||||
public function getPermissionCodes(int $userId): array
|
public function getPermissionCodes(int $userId, string $systemCode): array
|
||||||
{
|
{
|
||||||
return $this->logic->getPermissionCodes($userId);
|
return $this->logic->getPermissionCodes($userId, $systemCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ class UserService
|
|||||||
/**
|
/**
|
||||||
* 获取用户信息(包括菜单和权限)
|
* 获取用户信息(包括菜单和权限)
|
||||||
*/
|
*/
|
||||||
public function getUserInfo(int $uid)
|
public function getUserInfo(int $uid, string $systemCode)
|
||||||
{
|
{
|
||||||
$user = $this->logic->userInfo($uid);
|
$user = $this->logic->userInfo($uid);
|
||||||
$permService = new PermService();
|
$permService = new PermService();
|
||||||
$menuTree = $permService->getMenuTree($uid);
|
$menuTree = $permService->getMenuTree($uid, $systemCode);
|
||||||
$permissionCodes = $permService->getPermissionCodes($uid);
|
$permissionCodes = $permService->getPermissionCodes($uid, $systemCode);
|
||||||
return [
|
return [
|
||||||
'user' => [
|
'user' => [
|
||||||
'id' => $user->id,
|
'id' => $user->id,
|
||||||
|
|||||||
Reference in New Issue
Block a user