fix: 修改config和推出登陆
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@ export class ConfigModel extends Model {
|
||||
declare title: string;
|
||||
declare description: string;
|
||||
declare tags: string[];
|
||||
/**
|
||||
* 配置key, 默认可以为空,如何设置了,必须要唯一。
|
||||
*/
|
||||
declare key: string;
|
||||
declare data: ConfigData; // files
|
||||
declare uid: string;
|
||||
@@ -74,7 +77,7 @@ export class ConfigModel extends Model {
|
||||
key: 'upload',
|
||||
version: '1.0.0',
|
||||
};
|
||||
const config = await ConfigModel.getConfig('upload', {
|
||||
const config = await ConfigModel.getConfig('upload.json', {
|
||||
uid: opts.uid,
|
||||
defaultData: defaultConfig,
|
||||
});
|
||||
@@ -87,7 +90,7 @@ export class ConfigModel extends Model {
|
||||
};
|
||||
}
|
||||
static async setUploadConfig(opts: { uid: string; data: { key?: string; version?: string } }) {
|
||||
const config = await ConfigModel.setConfig('upload', {
|
||||
const config = await ConfigModel.setConfig('upload.json', {
|
||||
uid: opts.uid,
|
||||
data: opts.data,
|
||||
});
|
||||
|
||||
@@ -51,7 +51,7 @@ export class ShareConfigService extends ConfigModel {
|
||||
}
|
||||
static async expireShareConfig(key: string, username: string) {
|
||||
if (key && username) {
|
||||
await redis.set(`config:share:${username}:${key}`, '', 'EX', 0);
|
||||
await redis.set(`config:share:${username}:${key}`, '', 'EX', 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,15 @@ app
|
||||
key: 'logout',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { tokens = [] } = ctx.query?.data || {};
|
||||
clearCookie(ctx);
|
||||
for (const token of tokens) {
|
||||
await User.oauth.delToken(token);
|
||||
}
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
message: 'Logout Success',
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
app
|
||||
|
||||
Reference in New Issue
Block a user