feat: add tips for user info
This commit is contained in:
@@ -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`);
|
||||
|
||||
Reference in New Issue
Block a user