关于login重构

This commit is contained in:
2025-03-21 20:41:01 +08:00
parent 0179fe73a3
commit 8053a3db64
28 changed files with 889 additions and 596 deletions

View File

@@ -0,0 +1,41 @@
import { ConfigModel } from '../models/model.ts';
import { CustomError } from '@kevisual/router';
import { redis } from '@/app.ts';
import { User } from '@/models/user.ts';
export class ShareConfigService extends ConfigModel {
/**
* 获取分享的配置
* @param key 配置的key
* @param username 分享者的username
* @returns 配置
*/
static async getShareConfig(key: string, username: string) {
const shareCacheConfig = await redis.get(`config:share:${username}:${key}`);
if (shareCacheConfig) {
return JSON.parse(shareCacheConfig);
}
const user = await User.findOne({
where: { username },
});
if (!user) {
throw new CustomError(404, 'user not found');
}
const config = await ConfigModel.findOne({
where: { key, uid: user.id },
});
if (!config) {
throw new CustomError(404, 'config not found');
}
const configData = config?.data?.permission;
if (configData?.share !== 'public') {
throw new CustomError(403, 'no permission');
}
await redis.set(`config:share:${username}:${key}`, JSON.stringify(config), 'EX', 60 * 60 * 24 * 7); // 7天
return config;
}
static async expireShareConfig(key: string, username: string) {
if (key && username) {
await redis.del(`config:share:${username}:${key}`);
}
}
}