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", "name": "@kevisual/code-center-module",
"version": "0.0.15", "version": "0.0.16",
"description": "", "description": "",
"main": "dist/system.mjs", "main": "dist/system.mjs",
"module": "dist/system.mjs", "module": "dist/system.mjs",
@ -40,7 +40,7 @@
"ioredis": "^5.6.0", "ioredis": "^5.6.0",
"nanoid": "^5.1.5", "nanoid": "^5.1.5",
"pg": "^8.14.1", "pg": "^8.14.1",
"sequelize": "^6.37.6", "sequelize": "^6.37.7",
"socket.io": "^4.8.1", "socket.io": "^4.8.1",
"zod": "^3.24.2" "zod": "^3.24.2"
}, },
@ -57,14 +57,14 @@
"@types/formidable": "^3.4.5", "@types/formidable": "^3.4.5",
"@types/jsonwebtoken": "^9.0.9", "@types/jsonwebtoken": "^9.0.9",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^22.13.11", "@types/node": "^22.13.14",
"@types/react": "^19.0.12", "@types/react": "^19.0.12",
"@types/uuid": "^10.0.0", "@types/uuid": "^10.0.0",
"concurrently": "^9.1.2", "concurrently": "^9.1.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"nodemon": "^3.1.9", "nodemon": "^3.1.9",
"rimraf": "^6.0.1", "rimraf": "^6.0.1",
"rollup": "^4.36.0", "rollup": "^4.38.0",
"rollup-plugin-copy": "^3.5.0", "rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.2.1", "rollup-plugin-dts": "^6.2.1",
"rollup-plugin-esbuild": "^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) { static async getOauthUser(token: string) {
return await oauth.verifyToken(token); return await oauth.verifyToken(token);
} }
/**
* , tokenUser
* @param token
* @returns
*/
static async getUserByToken(token: string) { static async getUserByToken(token: string) {
const oauthUser = await oauth.verifyToken(token); const oauthUser = await oauth.verifyToken(token);
if (!oauthUser) { if (!oauthUser) {
@ -89,6 +94,7 @@ export class User extends Model {
} }
const userId = oauthUser?.uid || oauthUser.id; const userId = oauthUser?.uid || oauthUser.id;
const user = await User.findByPk(userId); const user = await User.findByPk(userId);
user.setTokenUser(oauthUser);
return user; return user;
} }
/** /**
@ -148,9 +154,15 @@ export class User extends Model {
const cPassword = cryptPwd(password, salt); const cPassword = cryptPwd(password, salt);
return this.password === cPassword; return this.password === cPassword;
} }
async getInfo() { /**
* , tokenUser uid
* @param uid uid为真实用户
* @returns
*/
async getInfo(uid?: string) {
const orgs = await this.getOrgs(); const orgs = await this.getOrgs();
return {
const info: Record<string, any> = {
id: this.id, id: this.id,
username: this.username, username: this.username,
nickname: this.nickname, nickname: this.nickname,
@ -160,14 +172,25 @@ export class User extends Model {
avatar: this.avatar, avatar: this.avatar,
orgs, orgs,
}; };
const tokenUser = this.tokenUser;
if (uid) {
info.uid = uid;
} else if (tokenUser.uid) {
info.uid = tokenUser.uid;
} }
return info;
}
/**
*
* @returns
*/
async getOrgs() { async getOrgs() {
let id = this.id; let id = this.id;
if (this.type === 'org') { if (this.type === 'org') {
if (this.tokenUser && this.tokenUser.uid) { if (this.tokenUser && this.tokenUser.uid) {
id = this.tokenUser.uid; id = this.tokenUser.uid;
} else { } else {
throw new CustomError('Permission denied'); throw new CustomError(400, 'Permission denied');
} }
} }
const cache = await redis.get(`user:${id}:orgs`); const cache = await redis.get(`user:${id}:orgs`);