diff --git a/package.json b/package.json index 90d7d2b..81e5f87 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/models/user.ts b/src/models/user.ts index b46a200..9425c13 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -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 = { 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`);