fix: 更新功能

This commit is contained in:
xion 2025-03-06 19:02:24 +08:00
parent 2f166817df
commit 16722f1d1d
8 changed files with 75 additions and 20 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/code-center-module", "name": "@kevisual/code-center-module",
"version": "0.0.12", "version": "0.0.13",
"description": "", "description": "",
"main": "dist/system.mjs", "main": "dist/system.mjs",
"module": "dist/system.mjs", "module": "dist/system.mjs",

View File

@ -1,4 +1,4 @@
import { UserServices, User, UserInit } from './models/user.ts'; import { UserServices, User, UserInit, UserModel } from './models/user.ts';
import { Org, OrgInit } from './models/org.ts'; import { Org, OrgInit, OrgModel } from './models/org.ts';
export { User, Org, UserServices, UserInit, OrgInit }; export { User, Org, UserServices, UserInit, OrgInit, UserModel, OrgModel };

View File

@ -2,18 +2,16 @@
* redis和sequelizeuseContextKey当中 * redis和sequelizeuseContextKey当中
*/ */
import { app } from './app.ts'; import { app } from './app.ts';
import { UserServices, UserInit } from './models/user.ts'; import { UserServices, UserInit, UserModel, User } from './models/user.ts';
import { Org, OrgInit } from './models/org.ts'; import { Org, OrgInit, OrgModel } from './models/org.ts';
import { useContextKey } from '@kevisual/use-config/context'; import { useContextKey } from '@kevisual/use-config/context';
import { Sequelize } from 'sequelize'; import { Sequelize } from 'sequelize';
import { Redis } from 'ioredis'; import { Redis } from 'ioredis';
export const User = UserServices; export { User, UserServices, UserInit, UserModel };
export { Org, OrgInit, UserInit }; export { Org, OrgInit, OrgModel };
export const redis = useContextKey<Redis>('redis'); export const redis = useContextKey<Redis>('redis');
export const sequelize = useContextKey<Sequelize>('sequelize'); export const sequelize = useContextKey<Sequelize>('sequelize');
export const UserModel = useContextKey<typeof UserServices>('UserModel', () => UserServices);
export const OrgModel = useContextKey<typeof Org>('OrgModel', () => Org);
export { app }; export { app };
export const init = () => { export const init = () => {
OrgInit(); OrgInit();

View File

@ -192,4 +192,4 @@ export const OrgInit = async (newSequelize?: any, tableName?: string, sync?: Syn
} }
return Org; return Org;
}; };
useContextKey('OrgModel', () => Org); export const OrgModel = useContextKey('OrgModel', () => Org);

View File

@ -73,6 +73,32 @@ export class User extends Model {
const tokenUser = ct.payload; const tokenUser = ct.payload;
return tokenUser; 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) { static async createUser(username: string, password?: string, description?: string) {
const user = await User.findOne({ where: { username } }); const user = await User.findOne({ where: { username } });
if (user) { if (user) {
@ -301,4 +327,4 @@ export class UserServices extends User {
static createDemoUser = createDemoUser; static createDemoUser = createDemoUser;
} }
useContextKey('UserModel', () => UserServices); export const UserModel = useContextKey('UserModel', () => UserServices);

View File

@ -1,6 +1,6 @@
import { Redis } from 'ioredis'; import { Redis } from 'ioredis';
import { useConfig } from '@kevisual/use-config'; import { useConfig } from '@kevisual/use-config';
import { useContextKey } from '@kevisual/use-config/context';
const config = useConfig<{ const config = useConfig<{
redis: ConstructorParameters<typeof Redis>; redis: ConstructorParameters<typeof Redis>;
}>(); }>();
@ -18,7 +18,7 @@ export const redis = new Redis({
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试) maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
...config.redis, ...config.redis,
}); });
useContextKey('redis', () => redis);
// 监听连接事件 // 监听连接事件
redis.on('connect', () => { redis.on('connect', () => {
console.log('Redis 连接成功'); console.log('Redis 连接成功');

34
src/scripts/user-token.ts Normal file
View File

@ -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();

View File

@ -6,8 +6,8 @@ import { app } from './app.ts';
import * as sequelizeLib from './modules/sequelize.ts'; import * as sequelizeLib from './modules/sequelize.ts';
export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize); export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
import { UserServices, UserInit } from './models/user.ts'; import { UserServices, User, UserInit, UserModel } from './models/user.ts';
import { Org, OrgInit } from './models/org.ts'; import { Org, OrgInit, OrgModel } from './models/org.ts';
import * as redisLib from './modules/redis.ts'; import * as redisLib from './modules/redis.ts';
import { useContextKey } from '@kevisual/use-config/context'; 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 redisPublisher = useContextKey('redisPublisher', () => redisLib.redisPublisher);
export const redisSubscriber = useContextKey('redisSubscriber', () => redisLib.redisSubscriber); export const redisSubscriber = useContextKey('redisSubscriber', () => redisLib.redisSubscriber);
export const UserModel = useContextKey<typeof UserServices>('UserModel', () => UserServices); export { UserModel, OrgModel, User, UserServices };
export const OrgModel = useContextKey<typeof Org>('OrgModel', () => Org);
export { app };
export const User = UserServices;
export { Org, OrgInit, UserInit }; export { Org, OrgInit, UserInit };
export const init = () => { export const init = () => {