diff --git a/package.json b/package.json index 29bbc58..f781e68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-center-module", - "version": "0.0.12", + "version": "0.0.13", "description": "", "main": "dist/system.mjs", "module": "dist/system.mjs", diff --git a/src/core-models.ts b/src/core-models.ts index 7519cbe..becaea6 100644 --- a/src/core-models.ts +++ b/src/core-models.ts @@ -1,4 +1,4 @@ -import { UserServices, User, UserInit } from './models/user.ts'; -import { Org, OrgInit } from './models/org.ts'; +import { UserServices, User, UserInit, UserModel } from './models/user.ts'; +import { Org, OrgInit, OrgModel } from './models/org.ts'; -export { User, Org, UserServices, UserInit, OrgInit }; +export { User, Org, UserServices, UserInit, OrgInit, UserModel, OrgModel }; diff --git a/src/lib.ts b/src/lib.ts index cc9572d..db46b4e 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -2,18 +2,16 @@ * 自己初始化redis和sequelize,的模块,放到useContextKey当中 */ import { app } from './app.ts'; -import { UserServices, UserInit } from './models/user.ts'; -import { Org, OrgInit } from './models/org.ts'; +import { UserServices, UserInit, UserModel, User } from './models/user.ts'; +import { Org, OrgInit, OrgModel } from './models/org.ts'; import { useContextKey } from '@kevisual/use-config/context'; import { Sequelize } from 'sequelize'; import { Redis } from 'ioredis'; -export const User = UserServices; -export { Org, OrgInit, UserInit }; +export { User, UserServices, UserInit, UserModel }; +export { Org, OrgInit, OrgModel }; export const redis = useContextKey('redis'); export const sequelize = useContextKey('sequelize'); -export const UserModel = useContextKey('UserModel', () => UserServices); -export const OrgModel = useContextKey('OrgModel', () => Org); export { app }; export const init = () => { OrgInit(); diff --git a/src/models/org.ts b/src/models/org.ts index bc346fa..3f49820 100644 --- a/src/models/org.ts +++ b/src/models/org.ts @@ -192,4 +192,4 @@ export const OrgInit = async (newSequelize?: any, tableName?: string, sync?: Syn } return Org; }; -useContextKey('OrgModel', () => Org); +export const OrgModel = useContextKey('OrgModel', () => Org); diff --git a/src/models/user.ts b/src/models/user.ts index f0d0bf3..cadb173 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -73,6 +73,32 @@ export class User extends Model { const tokenUser = ct.payload; return tokenUser; } + static async getUserByToken(token: string) { + const ct = await checkToken(token, config.tokenSecret); + const tokenUser = ct.payload; + let userId = tokenUser.id; + if (tokenUser.uid) { + // 如果tokenUser.uid 存在,则表示是token是o用户的user,需要获取o的真实用户 + userId = tokenUser.uid; + } + const user = await User.findByPk(userId); + return user; + } + /** + * 判断是否在用户列表中 + * @param username + * @param includeMe + * @returns + */ + async hasUser(username: string, includeMe = true) { + const orgs = await this.getOrgs(); + const me = this.username; + const allUsers = [...orgs]; + if (includeMe) { + allUsers.push(me); + } + return allUsers.includes(username); + } static async createUser(username: string, password?: string, description?: string) { const user = await User.findOne({ where: { username } }); if (user) { @@ -301,4 +327,4 @@ export class UserServices extends User { static createDemoUser = createDemoUser; } -useContextKey('UserModel', () => UserServices); +export const UserModel = useContextKey('UserModel', () => UserServices); diff --git a/src/modules/redis.ts b/src/modules/redis.ts index 826294d..5d04b4b 100644 --- a/src/modules/redis.ts +++ b/src/modules/redis.ts @@ -1,6 +1,6 @@ import { Redis } from 'ioredis'; import { useConfig } from '@kevisual/use-config'; - +import { useContextKey } from '@kevisual/use-config/context'; const config = useConfig<{ redis: ConstructorParameters; }>(); @@ -18,7 +18,7 @@ export const redis = new Redis({ maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试) ...config.redis, }); - +useContextKey('redis', () => redis); // 监听连接事件 redis.on('connect', () => { console.log('Redis 连接成功'); diff --git a/src/scripts/user-token.ts b/src/scripts/user-token.ts new file mode 100644 index 0000000..c838d2d --- /dev/null +++ b/src/scripts/user-token.ts @@ -0,0 +1,34 @@ +import * as redisLib from '../modules/redis.ts'; +import { useContextKey } from '@kevisual/use-config/context'; +const redis = useContextKey('redis', () => redisLib.redis); +import '../modules/init.ts'; +import { User, UserInit } from '../models/user.ts'; +import { Org, OrgInit } from '../models/org.ts'; +const sequelize = useContextKey('sequelize'); +let rootToken = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjE0MjA2MzA1LThiNWMtNDRjYy1iMTc3LTc2NmNmZTJlNDUyZiIsInVzZXJuYW1lIjoicm9vdCIsInR5cGUiOiJ1c2VyIiwiaWF0IjoxNzQxMjU3ODAyLCJleHAiOjE3NDE4NjI2MDJ9.JfqiABYBqkMDuFJQ5LcluQR7-EpfoBObFEOau_jKvKk'; +export const main = async () => { + // await UserInit(null ,null, { + // alter: true, + // }); + await UserInit(); + await OrgInit(); + + // const root = await User.findOne({ + // where: { + // username: 'root', + // }, + // }); + // if (root) { + // console.log('root', root.id, root.username); + // // const token = await root.createToken(); + // // console.log('token', token); + // const orgs = await root.getOrgs(); + // console.log('orgs', orgs.length, orgs); + // } + const k = await User.getUserByToken(rootToken); + const has = await k.hasUser('admin'); + console.log('has', has); + process.exit(0); +}; +main(); diff --git a/src/system.ts b/src/system.ts index ab466a4..bb864b6 100644 --- a/src/system.ts +++ b/src/system.ts @@ -6,8 +6,8 @@ import { app } from './app.ts'; import * as sequelizeLib from './modules/sequelize.ts'; export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize); -import { UserServices, UserInit } from './models/user.ts'; -import { Org, OrgInit } from './models/org.ts'; +import { UserServices, User, UserInit, UserModel } from './models/user.ts'; +import { Org, OrgInit, OrgModel } from './models/org.ts'; import * as redisLib from './modules/redis.ts'; import { useContextKey } from '@kevisual/use-config/context'; @@ -17,10 +17,7 @@ export const redis = useContextKey('redis', () => redisLib.redis); export const redisPublisher = useContextKey('redisPublisher', () => redisLib.redisPublisher); export const redisSubscriber = useContextKey('redisSubscriber', () => redisLib.redisSubscriber); -export const UserModel = useContextKey('UserModel', () => UserServices); -export const OrgModel = useContextKey('OrgModel', () => Org); -export { app }; -export const User = UserServices; +export { UserModel, OrgModel, User, UserServices }; export { Org, OrgInit, UserInit }; export const init = () => {