This commit is contained in:
2025-03-14 01:41:53 +08:00
parent efef48a1b0
commit d947043a16
12 changed files with 284 additions and 65 deletions

View File

@@ -0,0 +1,2 @@
import './list.ts';
import './upload-config.ts';

21
src/routes/config/list.ts Normal file
View File

@@ -0,0 +1,21 @@
import { app } from '@/app.ts';
import { ConfigModel } from './models/model.ts';
app
.route({
path: 'config',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const { id } = ctx.state.tokenUser;
const config = await ConfigModel.findAll({
where: {
uid: id,
},
});
ctx.body = {
list: config,
};
})
.addTo(app);

View File

@@ -0,0 +1,140 @@
import { useContextKey } from '@kevisual/use-config/context';
import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export interface ConfigData {
key?: string;
version?: string;
}
export type Config = Partial<InstanceType<typeof ConfigModel>>;
/**
* 用户配置
*/
export class ConfigModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare tags: string[];
declare key: string;
declare data: ConfigData; // files
declare uid: string;
/**
* 获取用户配置
* @param key 配置key
* @param opts 配置选项
* @param opts.uid 用户id
* @param opts.defaultData 默认数据
* @returns 配置
*/
static async getConfig(key: string, opts: { uid: string; defaultData?: any }) {
const [config, isNew] = await ConfigModel.findOrCreate({
where: { key, uid: opts.uid },
defaults: {
key,
title: key,
uid: opts.uid,
data: opts?.defaultData || {},
},
});
return {
config: config,
isNew,
};
}
static async setConfig(key: string, opts: { uid: string; data: any }) {
let config = await ConfigModel.findOne({
where: { key, uid: opts.uid },
});
if (config) {
config.data = { ...config.data, ...opts.data };
await config.save();
} else {
config = await ConfigModel.create({
title: key,
key,
uid: opts.uid,
data: opts.data,
});
}
return config;
}
/**
* 获取上传配置
* @param key 配置key
* @param opts 配置选项
* @param opts.uid 用户id
* @returns 配置
*/
static async getUploadConfig(opts: { uid: string }) {
const defaultConfig = {
key: 'upload',
type: 'upload',
version: '1.0.0',
};
const config = await ConfigModel.getConfig('upload', {
uid: opts.uid,
defaultData: defaultConfig,
});
const data = config.config.data;
const prefix = `/${data.key}/${data.version}`;
return {
config: config.config,
isNew: config.isNew,
prefix,
};
}
static async setUploadConfig(opts: { uid: string; data: any }) {
const config = await ConfigModel.setConfig('upload', {
uid: opts.uid,
data: opts.data,
});
return config;
}
}
ConfigModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.TEXT,
defaultValue: '',
},
key: {
type: DataTypes.TEXT,
defaultValue: '',
},
description: {
type: DataTypes.TEXT,
defaultValue: '',
},
tags: {
type: DataTypes.JSONB,
defaultValue: [],
},
data: {
type: DataTypes.JSONB,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'kv_config',
paranoid: true,
},
);
ConfigModel.sync({ alter: true, logging: false }).catch((e) => {
console.error('ConfigModel sync', e);
});
useContextKey('ConfigModel', () => ConfigModel);

View File

@@ -0,0 +1,34 @@
import { app } from '../../app.ts';
import { ConfigModel } from './models/model.ts';
app
.route({
path: 'config',
key: 'getUploadConfig',
middleware: ['auth'],
})
.define(async (ctx) => {
const { id } = ctx.state.tokenUser;
const config = await ConfigModel.getUploadConfig({
uid: id,
});
ctx.body = config;
})
.addTo(app);
app
.route({
path: 'config',
key: 'setUploadConfig',
middleware: ['auth'],
})
.define(async (ctx) => {
const { id } = ctx.state.tokenUser;
const data = ctx.query.data || {};
const config = await ConfigModel.setUploadConfig({
uid: id,
data,
});
ctx.body = config;
})
.addTo(app);

View File

@@ -15,3 +15,5 @@ import './file/index.ts';
// import './packages/index.ts';
import './micro-app/index.ts';
import './config/index.ts';

View File

@@ -1,6 +1,5 @@
import { ResourceData, ResourceModel } from './models/index.ts';
import { ResourceModel } from './models/index.ts';
import { app } from '../../app.ts';
import { CustomError } from '@kevisual/router';
app
.route({
@@ -29,11 +28,11 @@ app
.define(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
const rm = await ResourceModel.findByPk(id);
if (!rm) {
throw new CustomError('resource not found');
ctx.throw('resource not found');
}
ctx.body = rm;
return ctx;
@@ -61,15 +60,16 @@ app
.route({
path: 'resource',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
const resource = await ResourceModel.findByPk(id);
if (!resource) {
throw new CustomError('resource not found');
ctx.throw('resource not found');
}
await resource.destroy();
ctx.body = 'success';