Merge branch 'develop'

This commit is contained in:
mzl
2025-07-29 09:00:04 +08:00
parent a551507e4d
commit d74b374198
106 changed files with 6754 additions and 1346 deletions
-9
View File
@@ -86,13 +86,4 @@ class AuthService
{
(new AuthLogic())->blockToken($token, $systemCode);
}
// public function perms()
// {
// $permService = new PermService();
// $menuTree = $permService->getMenuTree($uid);
// $permissionCodes = $permService->getPermissionCodes($uid);
// return (new PermService())->getMenuTree($this->userId)
// }
}
-155
View File
@@ -1,155 +0,0 @@
<?php
namespace app\common\service;
use app\common\logic\AuthLogic;
use app\common\model\User;
use support\Cache;
class AuthService_bak
{
// 缓存 Key
protected static function cacheKey(int $userId): string
{
return "user_resources_{$userId}";
}
/**
* 获取用户所有资源(目录/菜单/权限),合并角色+直接分配,并缓存
*/
public static function getUserResources(int $userId)
{
$cacheKey = self::cacheKey($userId);
$cached = Cache::get($cacheKey);
if ($cached !== null) {
return $cached;
}
$user = User::with(['roles.resources', 'resources'])->find($userId);
if (!$user) {
return collect();
}
// 合并:角色资源 + 用户直接资源
$all = collect();
foreach ($user->roles as $role) {
$all = $all->merge($role->resources);
}
$all = $all->merge($user->resources);
$unique = $all->unique('id')->values();
Cache::set($cacheKey, $unique, 3600);
return $unique;
}
/**
* 构建目录/菜单树,并给每个菜单节点挂载权限列表和打开方式
*/
public static function getMenuTree(int $userId): array
{
$resources = self::getUserResources($userId);
$menus = $resources
->filter(fn($r) => in_array($r->type, ['directory','menu']))
->values();
return self::buildTree($menus, $resources);
}
protected static function buildTree($nodes, $allResources, int $parentId = 0): array
{
$tree = [];
foreach ($nodes as $node) {
if ($node->parent_id !== $parentId) continue;
// 子节点
$children = self::buildTree($nodes, $allResources, $node->id);
// 本级权限列表
$authList = $allResources
->filter(fn($r) => $r->type === 'permission' && $r->parent_id === $node->id)
->map(fn($r) => [
'id' => $r->id,
'title' => $r->name,
'auth_mark' => $r->code,
])->values()->all();
$tree[] = [
'id' => $node->id,
'name' => $node->name,
'path' => $node->url,
'meta' => [
'title' => $node->name,
'icon' => $node->icon,
'keepAlive' => true,
'authList' => $authList,
// 新增打开方式字段
'openType' => $node->open_type, // 'internal' | 'external' | 'iframe'
],
'children' => $children,
];
}
return $tree;
}
/**
* 获取用户所有权限 code(仅 type=permission)
*/
public static function getPermissionCodes(int $userId): array
{
return self::getUserResources($userId)
->filter(fn($res) => $res->type === 'permission')
->pluck('code')
->unique()
->values()
->all();
}
/** 清除缓存(角色/资源变更时调用) */
public static function clearCache(int $userId): void
{
Cache::delete(self::cacheKey($userId));
}
public function login(array $data): array
{
$loginType = $data['login_type'] ?? 'upwd';
$logic = new AuthLogic();
return match ($loginType) {
'feishu' => $logic->feishuLogin($data),
'qrcode' => $logic->qrCodeLogin($data),
default => $logic->uPwdLogin($data),
};
}
/**
* 用户注册
*
* @param array $data
* @return User
*/
public function register(array $data): User
{
return (new AuthLogic())->register($data);
}
public function verifyToken(string $token): array
{
return (new AuthLogic())->verifyToken($token);
}
public function refreshToken(string $refreshToken): array
{
return (new AuthLogic())->refreshToken($refreshToken);
}
public function logout(?string $token)
{
(new AuthLogic())->logout($token);
}
public function blockToken(string $token)
{
(new AuthLogic())->blockToken($token);
}
}
+1 -1
View File
@@ -20,7 +20,7 @@ class BasicDataService
public function checkAndCacheBaseParams($loginType, $systemCode, $corpAbbr)
{
if (!$loginType && !$systemCode && !$corpAbbr) {
throw new BusinessException(htmlpage());
throw new BusinessException('header缺少必要参数');
}
// 检查并缓存登录类型
+2 -2
View File
@@ -14,8 +14,8 @@ class CorpFeishuAppService
$this->logic = (new CorpFeishuAppLogic());
}
public function getFeishuApp(int $corpId)
public function getFeishuApp(int $corpId, int $systemId)
{
return $this->logic->getFeishuApp($corpId);
return $this->logic->getFeishuApp($corpId, $systemId);
}
}
+11 -11
View File
@@ -15,23 +15,23 @@ class PermService
}
// 用户资源缓存key
protected static function cacheKey(int $userId): string
protected static function cacheKey(int $userId, string $systemCode): string
{
return "user_resources_{$userId}";
return "user_resources_{$userId}_{$systemCode}";
}
/**
* 获取用户所有资源(合并角色+直接分配),结果缓存
*/
public function getUserResources(int $userId)
public function getUserResources(int $userId, string $systemCode)
{
$cacheKey = self::cacheKey($userId);
$cacheKey = self::cacheKey($userId, $systemCode);
$cached = Cache::get($cacheKey);
if ($cached !== null) {
return $cached;
}
// 调用 Logic 层获取资源
$resources = $this->logic->getUserResources($userId);
$resources = $this->logic->getUserResources($userId, $systemCode);
// 缓存结果
Cache::set($cacheKey, $resources, 3600);
return $resources;
@@ -40,26 +40,26 @@ class PermService
/**
* 构建目录/菜单树,节点包含权限与打开方式
*/
public function getMenuTree(int $userId): array
public function getMenuTree(int $userId, string $systemCode): array
{
// 调用 Logic 层处理菜单构建
$resources = $this->getUserResources($userId);
$resources = $this->getUserResources($userId, $systemCode);
return $this->logic->buildTree($resources);
}
/**
* 获取用户所有权限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);
}
/**
* 清除资源相关缓存
*/
public function clearCache(int $userId): void
public function clearCache(int $userId, string $systemCode): void
{
Cache::delete(self::cacheKey($userId));
Cache::delete(self::cacheKey($userId, $systemCode));
}
}
-55
View File
@@ -1,55 +0,0 @@
<?php
namespace app\common\service;
use app\common\logic\RoleLogic;
class RoleService
{
protected $roleLogic;
public function __construct()
{
$this->roleLogic = (new RoleLogic());
}
/**
* 获取角色列表
*/
public function getRoleList(array $params)
{
return $this->roleLogic->getRoleList($params);
}
/**
* 获取角色详情
*/
public function getRoleDetail(int $roleId)
{
return $this->roleLogic->getRoleDetail($roleId);
}
/**
* 创建角色
*/
public function createRole(array $data)
{
return $this->roleLogic->createRole($data);
}
/**
* 更新角色
*/
public function updateRole(array $data)
{
return $this->roleLogic->updateRole($data);
}
/**
* 删除角色
*/
public function deleteRole(int $roleId)
{
return $this->roleLogic->deleteRole($roleId);
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace app\common\service;
use app\common\logic\SystemsLogic;
class SystemsService
{
protected $logic;
public function __construct()
{
$this->logic = (new SystemsLogic());
}
/**
* 获取系统list
*/
public function getList()
{
return $this->logic->getList();
}
}
+74 -3
View File
@@ -3,6 +3,11 @@
namespace app\common\service;
use app\common\logic\UserLogic;
use app\common\model\Corp;
use app\common\model\User;
use app\common\utils\CrudUtil;
use app\crm\model\Country;
use support\exception\BusinessException;
class UserService
{
@@ -17,12 +22,12 @@ class UserService
/**
* 获取用户信息(包括菜单和权限)
*/
public function getUserInfo(int $uid)
public function getUserInfo(int $uid, string $systemCode)
{
$user = $this->logic->userInfo($uid);
$permService = new PermService();
$menuTree = $permService->getMenuTree($uid);
$permissionCodes = $permService->getPermissionCodes($uid);
$menuTree = $permService->getMenuTree($uid, $systemCode);
$permissionCodes = $permService->getPermissionCodes($uid, $systemCode);
return [
'user' => [
'id' => $user->id,
@@ -37,4 +42,70 @@ class UserService
'permissions' => $permissionCodes
];
}
/**
* 获取用户信息(包括菜单和权限)
*/
public function getUser(int $uid, $corp_abbr)
{
$user = $this->logic->userInfo($uid);
$corp_name = Corp::where('abbr', $corp_abbr)->value('name');
$ossservice = new OssService();
return [
'id' => $user->id,
'name' => $user->name,
'position' => $user->position,
'socialize' => $user->socialize,
'socialize_pic' => $user->socialize_pic,
'socialize_pic_full' => (!empty($user->socialize_pic))?$ossservice->generatePrivateUrl($user->socialize_pic):'',
'country_id' => $user->country_id,
'country_txt' => Country::where('id',$user->country_id)->value('cn_addr'),
'avatar' => $user->avatar,
'avatar_full' => (!empty($user->avatar))?$ossservice->generatePrivateUrl($user->avatar):'',
'phone' => $user->phone,
'corp_name' => $corp_name,
'email' => $user->email,
'status' => $user->status,
'roles' => $user->roles->pluck('name')
];
}
/**
* 编辑用户信息
*/
public function edit(int $uid, array $data)
{
unset($data['password']);
unset($data['phone']);
unset($data['email']);
unset($data['sn']);
$user = $this->logic->edit($uid,$data);
return $user;
}
/**
* 修改用户密码
*/
public function savePassword(int $uid, array $data)
{
// 对密码进行哈希处理
$password = $data['password'];
$user = User::find($uid);
if (!password_verify($password, $user->password)) {
throw new BusinessException('密码错误');
}
$user = $this->logic->savePassword($uid,$data);
return $user;
}
/**
* 获取用户列表
*/
public function getList()
{
return $this->logic->getList();
}
}