关于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

@@ -1,5 +1,6 @@
import { app } from '@/app.ts';
import { ConfigModel } from './models/model.ts';
import { ShareConfigService } from './services/share.ts';
app
.route({
path: 'config',
@@ -12,6 +13,7 @@ app
where: {
uid: id,
},
order: [['updatedAt', 'DESC']],
});
ctx.body = {
list: config,
@@ -19,3 +21,123 @@ app
})
.addTo(app);
app
.route({
path: 'config',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokernUser = ctx.state.tokenUser;
const tuid = tokernUser.id;
const { id, key, data, ...rest } = ctx.query?.data || {};
if (id) {
const config = await ConfigModel.findByPk(id);
if (config && config.uid === tuid) {
const keyConfig = await ConfigModel.findOne({
where: {
key,
uid: tuid,
},
});
if (keyConfig && keyConfig.id !== id) {
ctx.throw(403, 'key is already exists');
}
await config.update({
key,
data: {
...config.data,
...data,
},
...rest,
});
if (config.data?.permission?.share === 'public') {
await ShareConfigService.expireShareConfig(config.key, tokernUser.username);
}
ctx.body = config;
} else {
ctx.throw(403, 'no permission');
}
} else {
const keyConfig = await ConfigModel.findOne({
where: {
key,
uid: tuid,
},
});
if (keyConfig) {
ctx.throw(403, 'key is already exists');
}
const config = await ConfigModel.create({
key,
...rest,
data: data,
uid: tuid,
});
ctx.body = config;
}
})
.addTo(app);
app
.route({
path: 'config',
key: 'get',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokernUser = ctx.state.tokenUser;
const tuid = tokernUser.id;
const { id, key } = ctx.query?.data || {};
if (!id && !key) {
ctx.throw(400, 'id or key is required');
}
let config: ConfigModel;
if (id) {
config = await ConfigModel.findByPk(id);
}
if (!config && key) {
config = await ConfigModel.findOne({
where: {
key,
uid: tuid,
},
});
}
if (!config) {
ctx.throw(404, 'config not found');
}
if (config && config.uid === tuid) {
ctx.body = config;
} else {
ctx.throw(403, 'no permission');
}
})
.addTo(app);
app
.route({
path: 'config',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokernUser = ctx.state.tokenUser;
const tuid = tokernUser.id;
const { id } = ctx.query?.data || {};
if (id) {
const config = await ConfigModel.findOne({
where: {
id,
},
});
if (config && config.uid === tuid) {
await config.destroy();
} else {
ctx.throw(403, 'no permission');
}
} else {
ctx.throw(400, 'id is required');
}
})
.addTo(app);