修改了bug
This commit is contained in:
+19
-44
@@ -39,61 +39,36 @@ async function lookup(body) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息(带 5 分钟进程内缓存,token→user)
|
||||
* 仅按 token 获取用户信息(带 5 分钟进程内缓存,token→user)
|
||||
*
|
||||
* 注意:缓存期内登出/账户锁定/用户信息变更最多延迟 5 分钟生效
|
||||
* (user 服务自身接口即时生效)
|
||||
*
|
||||
* 只保留 token 路径:user 服务已不再接受按 userId 明文查询,
|
||||
* 故不存在 userId 兜底,避免凭 userId 冒充他人身份。
|
||||
*
|
||||
* @param {string} token 登录 token
|
||||
* @param {string} [userId] 备用用户ID(token 缺失或失效时的兜底路径,保持原 WS 行为)
|
||||
* @returns {Promise<Object|null>} 用户信息或 null
|
||||
*/
|
||||
export async function getUserInfo(token, userId) {
|
||||
if (!token && !userId) return null;
|
||||
export async function getUserByToken(token) {
|
||||
if (!token) return null;
|
||||
|
||||
// 1. token 路径(带缓存)
|
||||
if (token) {
|
||||
const key = `tk:${token}`;
|
||||
const cached = cacheGet(key);
|
||||
if (cached !== undefined) return cached;
|
||||
const key = `tk:${token}`;
|
||||
const cached = cacheGet(key);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
try {
|
||||
const user = await lookup({ token });
|
||||
if (user) {
|
||||
cacheSet(key, user, CACHE_TTL);
|
||||
return user;
|
||||
}
|
||||
// 无效 token 短缓存,保护 user 服务
|
||||
cacheSet(key, null, NEGATIVE_TTL);
|
||||
} catch (err) {
|
||||
// 网络异常不缓存,走 userId 兜底
|
||||
console.error("getUserByToken error:", err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. userId 兜底路径
|
||||
if (userId) {
|
||||
const key = `uid:${userId}`;
|
||||
const cached = cacheGet(key);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
try {
|
||||
const user = await lookup({ userId });
|
||||
if (user) {
|
||||
cacheSet(key, user, CACHE_TTL);
|
||||
return user;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("getUserById error:", err.message);
|
||||
try {
|
||||
const user = await lookup({ token });
|
||||
if (user) {
|
||||
cacheSet(key, user, CACHE_TTL);
|
||||
return user;
|
||||
}
|
||||
// 无效 token 短缓存,保护 user 服务
|
||||
cacheSet(key, null, NEGATIVE_TTL);
|
||||
} catch (err) {
|
||||
// 网络异常不缓存,避免把故障期内的问题固化 5 分钟
|
||||
console.error("getUserByToken error:", err.message);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅按 token 获取用户信息(REST 鉴权用,无 userId 兜底)
|
||||
*/
|
||||
export async function getUserByToken(token) {
|
||||
return getUserInfo(token, null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user