temp
This commit is contained in:
@@ -11,7 +11,10 @@ app
|
||||
path: 'ai',
|
||||
key: 'chat',
|
||||
description: '与 AI 进行对话, 调用 GPT 的AI 服务,生成结果,并返回。',
|
||||
middleware: ['auth'],
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
admin: true,
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { messages = [], username, group, question, chatOpts = {} } = ctx.query;
|
||||
|
||||
@@ -1,43 +1,53 @@
|
||||
import { Query } from '@kevisual/query/query';
|
||||
import { app, assistantConfig } from '../app.ts';
|
||||
import './config/index.ts';
|
||||
import './shop-install/index.ts';
|
||||
import './ai/index.ts';
|
||||
import './light-code/index.ts';
|
||||
import './user/index.ts';
|
||||
|
||||
import os from 'node:os';
|
||||
|
||||
import { authCache } from '@/module/cache/auth.ts';
|
||||
export const getTokenUser = async (ctx: any) => {
|
||||
const query = assistantConfig.query
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
token: ctx.state.token,
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
const tokenUser = res.data || {};
|
||||
return tokenUser;
|
||||
}
|
||||
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 { auth = {} } = config;
|
||||
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');
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
// 鉴权代理
|
||||
let tokenUser = await authCache.get(token);
|
||||
if (!tokenUser) {
|
||||
tokenUser = await getTokenUser(ctx);
|
||||
authCache.set(token, tokenUser);
|
||||
}
|
||||
ctx.state = {
|
||||
...ctx.state,
|
||||
token,
|
||||
tokenUser,
|
||||
};
|
||||
const { username } = tokenUser;
|
||||
if (!auth.username) {
|
||||
// 初始管理员账号
|
||||
auth.username = username;
|
||||
assistantConfig.setConfig({ auth });
|
||||
}
|
||||
if (isAdmin) {
|
||||
if (auth.username && auth.username !== username) {
|
||||
return ctx.throw(403, 'not admin user');
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -45,8 +55,7 @@ app
|
||||
.route({
|
||||
path: 'auth',
|
||||
id: 'auth',
|
||||
isDebug: true,
|
||||
description: '获取当前登录用户信息',
|
||||
description: '获取当前登录用户信息, 第一个登录的用户为管理员用户',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
await checkAuth(ctx);
|
||||
@@ -56,6 +65,7 @@ app
|
||||
.route({
|
||||
path: 'admin-auth',
|
||||
id: 'admin-auth',
|
||||
description: '管理员鉴权, 获取用户信息,并验证是否为管理员。',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
await checkAuth(ctx, true);
|
||||
@@ -66,6 +76,7 @@ app
|
||||
.route({
|
||||
path: 'client',
|
||||
key: 'version',
|
||||
description: '获取客户端版本号',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'v1.0.0';
|
||||
@@ -76,6 +87,7 @@ app
|
||||
.route({
|
||||
path: 'client',
|
||||
key: 'time',
|
||||
description: '获取当前时间',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = {
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
import './call.ts'
|
||||
import './call.ts'
|
||||
|
||||
import './upload.ts'
|
||||
10
assistant/src/routes/light-code/upload.ts
Normal file
10
assistant/src/routes/light-code/upload.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { app } from '../../app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'light-code',
|
||||
key: 'upload',
|
||||
middleware: ['auth'],
|
||||
description: '上传轻代码应用代码',
|
||||
}).define(async (ctx) => {
|
||||
const files = ctx.query.files;
|
||||
}).addTo(app);
|
||||
@@ -5,7 +5,10 @@ import { shopDefine } from './define.ts';
|
||||
app
|
||||
.route({
|
||||
...shopDefine.get('getRegistry'),
|
||||
middleware: ['auth'],
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
admin: true,
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const registry = assistantConfig.getRegistry();
|
||||
@@ -17,7 +20,10 @@ app
|
||||
app
|
||||
.route({
|
||||
...shopDefine.get('listInstalled'),
|
||||
middleware: ['auth'],
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
admin: true,
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const manager = new AssistantApp(assistantConfig);
|
||||
@@ -30,7 +36,10 @@ app
|
||||
app
|
||||
.route({
|
||||
...shopDefine.get('install'),
|
||||
middleware: ['auth'],
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
admin: true,
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
// https://localhost:51015/client/router?path=shop&key=install
|
||||
@@ -52,7 +61,10 @@ app
|
||||
app
|
||||
.route({
|
||||
...shopDefine.get('uninstall'),
|
||||
middleware: ['auth'],
|
||||
middleware: ['admin-auth'],
|
||||
metadata: {
|
||||
admin: true,
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
// https://localhost:51015/client/router?path=shop&key=uninstall
|
||||
|
||||
34
assistant/src/routes/user/index.ts
Normal file
34
assistant/src/routes/user/index.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { app, assistantConfig } from '../../app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'admin',
|
||||
key: 'login',
|
||||
description: '管理员用户登录',
|
||||
}).define(async (ctx) => {
|
||||
const { username, password } = ctx.query;
|
||||
const query = assistantConfig.query;
|
||||
const auth = assistantConfig.getConfig().auth;
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'login',
|
||||
data: {
|
||||
username,
|
||||
password,
|
||||
},
|
||||
})
|
||||
if (res.code !== 200) {
|
||||
return ctx.throw(401, 'login failed');
|
||||
}
|
||||
const loginUser = res.data.username;
|
||||
if (auth.username && loginUser !== auth.username) {
|
||||
return ctx.throw(403, 'login user is not admin user');
|
||||
}
|
||||
if (!auth.username) {
|
||||
// 初始管理员账号
|
||||
auth.username = 'admin';
|
||||
assistantConfig.setConfig({ auth });
|
||||
}
|
||||
// 保存配置
|
||||
|
||||
ctx.body = res.data;
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user