This commit is contained in:
2025-05-30 18:16:36 +08:00
parent 84d9fb2455
commit 239ba3ad10
7 changed files with 115 additions and 37 deletions

View File

@@ -1,12 +1,11 @@
import { app, assistantConfig } from '@/app.ts';
// import { getCacheAssistantConfig, setConfig } from '@/modules/config/index.ts';
// import { reload } from '@/modules/parent-msg.ts';
import { reload } from '../../module/reload-server.ts';
app
.route({
path: 'config',
description: '获取配置',
middleware: ['auth'],
middleware: ['admin-auth'],
})
.define(async (ctx) => {
ctx.body = assistantConfig.getCacheAssistantConfig();
@@ -18,12 +17,11 @@ app
path: 'config',
key: 'set',
description: '设置配置',
middleware: ['auth'],
middleware: ['admin-auth'],
})
.define(async (ctx) => {
const { data } = ctx.query;
// reload();
ctx.body = assistantConfig.setConfig(data);
reload();
})
.addTo(app);

View File

@@ -1,4 +1,4 @@
import { Query } from '@kevisual/query';
import { Query } from '@kevisual/query/query';
import { app, assistantConfig } from '../app.ts';
import './config/index.ts';
import './shop-install/index.ts';
@@ -6,40 +6,58 @@ import './ai/index.ts';
import os from 'node:os';
const checkAuth = async (ctx: any, isAdmin = false) => {
const config = assistantConfig.getConfig();
const { auth } = config;
const host = config.pageApi || config.registry || 'https://kevisual.cn';
const url = new URL('/api/router', host);
const token = ctx.query.token;
if (auth && auth.type !== 'public') {
if (!token) {
return ctx.throw(401, 'not login');
}
url.searchParams.set('token', token);
// 鉴权代理
// TODO:
const query = new Query({ url: url.toString() });
const res = await query.post({
path: 'user',
key: 'me',
});
if (res.code !== 200) {
return ctx.throw(401, 'not login');
}
const tokenUser = res.data || {};
ctx.state = {
...ctx.state,
tokenUser,
};
const { username } = tokenUser;
if (isAdmin) {
if (auth.username && auth.username !== username) {
return ctx.throw(403, 'not admin user');
}
}
}
};
app
.route({
path: 'auth',
id: 'auth',
isDebug: true,
description: '获取当前登录用户信息',
})
.define(async (ctx) => {
const config = assistantConfig.getConfig();
const { auth } = config;
const host = config.pageApi || config.registry || 'https://kevisual.cn';
const url = new URL('/api/router', host);
const token = ctx.token;
if (auth && auth.type !== 'public') {
if (!token) {
return ctx.throw(4001, 'not login');
}
url.searchParams.set('token', token);
// 鉴权代理
// TODO:
const query = new Query({ baseURL: url.toString() });
const res = await query.post({
path: 'user',
key: 'me',
});
console.log('res', res);
if (res.code !== 200) {
return ctx.throw(4001, 'not login');
}
const tokenUser = res.data || {};
ctx.state = {
...ctx.state,
tokenUser,
};
const { username } = tokenUser;
}
await checkAuth(ctx);
})
.addTo(app);
app
.route({
path: 'admin-auth',
id: 'admin-auth',
})
.define(async (ctx) => {
await checkAuth(ctx, true);
})
.addTo(app);