fix: 修改config和推出登陆

This commit is contained in:
2025-03-22 19:49:35 +08:00
parent d739bd12b3
commit 5f63c4cf4b
6 changed files with 92 additions and 42 deletions

View File

@@ -30,21 +30,27 @@ app
.define(async (ctx) => {
const tokernUser = ctx.state.tokenUser;
const tuid = tokernUser.id;
const { id, key, data, ...rest } = ctx.query?.data || {};
const { id, data, ...rest } = ctx.query?.data || {};
if (id) {
const config = await ConfigModel.findByPk(id);
let keyIsChange = false;
if (rest?.key) {
keyIsChange = rest.key !== config?.key;
}
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');
if (keyIsChange) {
const key = rest.key;
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,
@@ -58,7 +64,9 @@ app
} else {
ctx.throw(403, 'no permission');
}
} else {
} else if (rest?.key) {
// id 不存在key存在则属于更新key不能重复
const key = rest.key;
const keyConfig = await ConfigModel.findOne({
where: {
key,
@@ -66,16 +74,30 @@ app
},
});
if (keyConfig) {
ctx.throw(403, 'key is already exists');
await keyConfig.update({
data: { ...keyConfig.data, ...data },
...rest,
});
ctx.body = keyConfig;
} else {
const config = await ConfigModel.create({
key,
...rest,
data: data,
uid: tuid,
});
ctx.body = config;
}
const config = await ConfigModel.create({
key,
...rest,
data: data,
uid: tuid,
});
ctx.body = config;
}
if (id || rest?.key) return;
// id和key不存在。创建一个新的配置
const config = await ConfigModel.create({
...rest,
data: data,
uid: tuid,
});
ctx.body = config;
})
.addTo(app);