This commit is contained in:
2026-02-14 19:22:15 +08:00
parent 84e7a4158d
commit 8c6d57d228

View File

@@ -1,6 +1,5 @@
import { App, CustomError } from '@kevisual/router';
import { App as AppType, AppList, AppData, AppHelper } from './module/app-drizzle.ts';
import { app, redis, db, schema } from '@/app.ts';
import { App as AppType, AppList, AppData } from './module/app-drizzle.ts';
import { app, db, schema } from '@/app.ts';
import { uniqBy } from 'es-toolkit';
import { getUidByUsername, prefixFix } from './util.ts';
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
@@ -9,18 +8,26 @@ import { User } from '@/models/user.ts';
import { callDetectAppVersion } from './export.ts';
import { eq, and, desc } from 'drizzle-orm';
import { randomUUID } from 'crypto';
import { z } from 'zod';
app
.route({
path: 'app',
key: 'list',
middleware: ['auth'],
description: '获取应用列表根据key进行过滤',
metadata: {
args: {
data: z.object({
key: z.string().describe('应用的唯一标识')
})
}
}
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const data = ctx.query.data || {};
if (!data.key) {
throw new CustomError('key is required');
ctx.throw('key is required');
}
const list = await db.select()
.from(schema.kvAppList)
@@ -47,7 +54,7 @@ app
const id = ctx.query.id;
const { key, version, create = false } = ctx.query?.data || {};
if (!id && (!key || !version)) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
let appListModel: AppList | undefined;
if (id) {
@@ -130,13 +137,13 @@ app
ctx.body = newApp;
setExpire(newApp.id, 'test');
} else {
throw new CustomError('app not found');
ctx.throw('app not found');
}
return;
}
if (!rest.key) {
throw new CustomError('key is required');
ctx.throw('key is required');
}
const newApps = await db.insert(schema.kvAppList).values({ id: randomUUID(), data, ...rest, uid: tokenUser.id }).returning();
ctx.body = newApps[0];
@@ -155,12 +162,12 @@ app
const id = ctx.query.id;
const deleteFile = !!ctx.query.deleteFile; // 是否删除文件, 默认不删除
if (!id) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
const apps = await db.select().from(schema.kvAppList).where(eq(schema.kvAppList.id, id)).limit(1);
const app = apps[0];
if (!app) {
throw new CustomError('app not found');
ctx.throw('app not found');
}
const ams = await db.select().from(schema.kvApp).where(and(
eq(schema.kvApp.key, app.key),
@@ -168,10 +175,10 @@ app
)).limit(1);
const am = ams[0];
if (!am) {
throw new CustomError('app not found');
ctx.throw('app not found');
}
if (am.version === app.version) {
throw new CustomError('app is published');
ctx.throw('app is published');
}
const appData = app.data as AppData;
const files = appData.files || [];
@@ -197,10 +204,10 @@ app
const tokenUser = ctx.state.tokenUser;
const { appKey, files, version, username, description } = ctx.query.data;
if (!appKey) {
throw new CustomError('appKey is required');
ctx.throw('appKey is required');
}
if (!files || !files.length) {
throw new CustomError('files is required');
ctx.throw('files is required');
}
let uid = tokenUser.id;
let userPrefix = tokenUser.username;
@@ -214,7 +221,7 @@ app
}
} catch (e) {
console.log('getUserByToken error', e);
throw new CustomError('user not found');
ctx.throw('user not found');
}
}
const ams = await db.select().from(schema.kvApp).where(and(
@@ -276,7 +283,7 @@ app
ctx.body = prefixFix(res, userPrefix);
} catch (e) {
console.log('update error', e);
throw new CustomError(e.message);
ctx.throw(e.message);
}
})
.addTo(app);
@@ -292,7 +299,7 @@ app
const tokenUser = ctx.state.tokenUser;
const { id, username, appKey, version, detect } = ctx.query.data;
if (!id && !appKey) {
throw new CustomError('id or appKey is required');
ctx.throw('id or appKey is required');
}
const uid = await getUidByUsername(app, ctx, username);
@@ -380,10 +387,10 @@ app
)).limit(1);
app = apps[0];
} else {
throw new CustomError('user or key is required');
ctx.throw('user or key is required');
}
if (!app) {
throw new CustomError('app not found');
ctx.throw('app not found');
}
ctx.body = app;
})
@@ -400,7 +407,7 @@ app
const tokenUser = ctx.state.tokenUser;
const { key, version } = ctx.query?.data || {};
if (!key || !version) {
throw new CustomError('key and version are required');
ctx.throw('key and version are required');
}
const files = await getMinioListAndSetToAppList({ username: tokenUser.username, appKey: key, version });
ctx.body = files;
@@ -418,7 +425,7 @@ app
const tokenUser = ctx.state.tokenUser;
let { appKey, version, username } = ctx.query?.data || {};
if (!appKey || !version) {
throw new CustomError('appKey and version are required');
ctx.throw('appKey and version are required');
}
const uid = await getUidByUsername(app, ctx, username);
const appLists = await db.select().from(schema.kvAppList).where(and(