feat: add tips for user info

This commit is contained in:
2025-03-29 20:52:49 +08:00
parent 1cce7e1193
commit 0c36328ac3
2 changed files with 30 additions and 7 deletions

View File

@@ -82,6 +82,11 @@ export class User extends Model {
static async getOauthUser(token: string) {
return await oauth.verifyToken(token);
}
/**
* 获取用户信息, 并设置tokenUser
* @param token
* @returns
*/
static async getUserByToken(token: string) {
const oauthUser = await oauth.verifyToken(token);
if (!oauthUser) {
@@ -89,6 +94,7 @@ export class User extends Model {
}
const userId = oauthUser?.uid || oauthUser.id;
const user = await User.findByPk(userId);
user.setTokenUser(oauthUser);
return user;
}
/**
@@ -148,9 +154,15 @@ export class User extends Model {
const cPassword = cryptPwd(password, salt);
return this.password === cPassword;
}
async getInfo() {
/**
* 获取用户信息, 需要先设置 tokenUser 或者设置 uid
* @param uid 如果存在则表示是组织其中uid为真实用户
* @returns
*/
async getInfo(uid?: string) {
const orgs = await this.getOrgs();
return {
const info: Record<string, any> = {
id: this.id,
username: this.username,
nickname: this.nickname,
@@ -160,14 +172,25 @@ export class User extends Model {
avatar: this.avatar,
orgs,
};
const tokenUser = this.tokenUser;
if (uid) {
info.uid = uid;
} else if (tokenUser.uid) {
info.uid = tokenUser.uid;
}
return info;
}
/**
* 获取用户组织
* @returns
*/
async getOrgs() {
let id = this.id;
if (this.type === 'org') {
if (this.tokenUser && this.tokenUser.uid) {
id = this.tokenUser.uid;
} else {
throw new CustomError('Permission denied');
throw new CustomError(400, 'Permission denied');
}
}
const cache = await redis.get(`user:${id}:orgs`);