fix: 更新功能
This commit is contained in:
parent
2f166817df
commit
16722f1d1d
@ -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",
|
||||
|
@ -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 };
|
||||
|
10
src/lib.ts
10
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>('redis');
|
||||
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 const init = () => {
|
||||
OrgInit();
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<typeof Redis>;
|
||||
}>();
|
||||
@ -18,7 +18,7 @@ export const redis = new Redis({
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
...config.redis,
|
||||
});
|
||||
|
||||
useContextKey('redis', () => redis);
|
||||
// 监听连接事件
|
||||
redis.on('connect', () => {
|
||||
console.log('Redis 连接成功');
|
||||
|
34
src/scripts/user-token.ts
Normal file
34
src/scripts/user-token.ts
Normal 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();
|
@ -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<typeof UserServices>('UserModel', () => UserServices);
|
||||
export const OrgModel = useContextKey<typeof Org>('OrgModel', () => Org);
|
||||
export { app };
|
||||
export const User = UserServices;
|
||||
export { UserModel, OrgModel, User, UserServices };
|
||||
export { Org, OrgInit, UserInit };
|
||||
|
||||
export const init = () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user