重构应用列表处理逻辑,优化变量命名并移除冗余路由

This commit is contained in:
2026-02-06 02:46:58 +08:00
parent 266b7b33de
commit cb742807b4

View File

@@ -48,11 +48,11 @@ app
if (!id && (!key || !version)) { if (!id && (!key || !version)) {
throw new CustomError('id is required'); throw new CustomError('id is required');
} }
let am: AppListModel; let appListModel: AppListModel;
if (id) { if (id) {
am = await AppListModel.findByPk(id); appListModel = await AppListModel.findByPk(id);
} else if (key && version) { } else if (key && version) {
am = await AppListModel.findOne({ appListModel = await AppListModel.findOne({
where: { where: {
key, key,
version, version,
@@ -60,18 +60,35 @@ app
}, },
}); });
} }
if (!am && create) { if (!appListModel && create) {
am = await AppListModel.create({ appListModel = await AppListModel.create({
key, key,
version, version,
uid: tokenUser.id, uid: tokenUser.id,
data: {}, data: {},
}); });
const res = await app.run({ path: 'app', key: "detectVersionList", payload: { data: { appKey: key, version, username: tokenUser.username }, token: ctx.query.token } }); const appModel = await AppModel.findOne({
where: {
key,
uid: tokenUser.id,
},
});
if (!appModel) {
await AppModel.create({
key,
uid: tokenUser.id,
user: tokenUser.username,
version,
title: key,
description: '',
data: {},
});
}
const res = await callDetectAppVersion({ appKey: key, version, username: tokenUser.username }, ctx.query.token);
if (res.code !== 200) { if (res.code !== 200) {
ctx.throw(res.message || 'detect version list error'); ctx.throw(res.message || 'detect version list error');
} }
am = await AppListModel.findOne({ appListModel = await AppListModel.findOne({
where: { where: {
key, key,
version, version,
@@ -79,11 +96,11 @@ app
}, },
}); });
} }
if (!am) { if (!appListModel) {
ctx.throw('app not found'); ctx.throw('app not found');
} }
console.log('get app', am.id, am.key, am.version); console.log('get app', appListModel.id, appListModel.key, appListModel.version);
ctx.body = prefixFix(am, tokenUser.username); ctx.body = prefixFix(appListModel, tokenUser.username);
}) })
.addTo(app); .addTo(app);
@@ -154,26 +171,7 @@ app
return ctx; return ctx;
}) })
.addTo(app); .addTo(app);
app
.route({
path: 'app',
key: 'canUploadFiles',
middleware: ['auth'],
description: '检查是否可以上传文件,如果当前版本已存在,则不允许上传',
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { appKey, version } = ctx.query.data;
if (!appKey) {
throw new CustomError('appKey is required');
}
const app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
if (!app) {
throw new CustomError('app not found');
}
ctx.body = app;
})
.addTo(app);
app app
.route({ .route({
path: 'app', path: 'app',
@@ -291,6 +289,9 @@ app
} }
appList = await AppListModel.findByPk(appList.id); appList = await AppListModel.findByPk(appList.id);
} }
if (!appList) {
ctx.throw('app 未发现');
}
const files = appList.data.files || []; const files = appList.data.files || [];
const am = await AppModel.findOne({ where: { key: appList.key, uid: uid } }); const am = await AppModel.findOne({ where: { key: appList.key, uid: uid } });