feat: add tips for user info

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

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/code-center-module",
"version": "0.0.15",
"version": "0.0.16",
"description": "",
"main": "dist/system.mjs",
"module": "dist/system.mjs",
@ -40,7 +40,7 @@
"ioredis": "^5.6.0",
"nanoid": "^5.1.5",
"pg": "^8.14.1",
"sequelize": "^6.37.6",
"sequelize": "^6.37.7",
"socket.io": "^4.8.1",
"zod": "^3.24.2"
},
@ -57,14 +57,14 @@
"@types/formidable": "^3.4.5",
"@types/jsonwebtoken": "^9.0.9",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.13.11",
"@types/node": "^22.13.14",
"@types/react": "^19.0.12",
"@types/uuid": "^10.0.0",
"concurrently": "^9.1.2",
"cross-env": "^7.0.3",
"nodemon": "^3.1.9",
"rimraf": "^6.0.1",
"rollup": "^4.36.0",
"rollup": "^4.38.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^6.2.1",

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`);