144 lines
3.2 KiB
TypeScript
144 lines
3.2 KiB
TypeScript
import { app, assistantConfig } from '../app.ts';
|
|
import './config/index.ts';
|
|
import './shop-install/index.ts';
|
|
import './ai/index.ts';
|
|
// TODO:
|
|
// import './light-code/index.ts';
|
|
import './user/index.ts';
|
|
|
|
// TODO: 移除
|
|
import './hot-api/key-sender/index.ts';
|
|
|
|
import os from 'node:os';
|
|
import { authCache } from '@/module/cache/auth.ts';
|
|
const getTokenUser = async (token: string) => {
|
|
const query = assistantConfig.query
|
|
const res = await query.post({
|
|
path: 'user',
|
|
key: 'me',
|
|
token: token,
|
|
});
|
|
return res;
|
|
}
|
|
export const getTokenUserCache = async (token: string) => {
|
|
const tokenUser = await authCache.get(token);
|
|
if (tokenUser) {
|
|
return {
|
|
code: 200,
|
|
data: tokenUser,
|
|
};
|
|
}
|
|
const res = await getTokenUser(token);
|
|
if (res.code === 200) {
|
|
authCache.set(token, res.data);
|
|
}
|
|
return res;
|
|
}
|
|
const checkAuth = async (ctx: any, isAdmin = false) => {
|
|
const config = assistantConfig.getConfig();
|
|
const { auth = {} } = config;
|
|
const token = ctx.query.token;
|
|
console.log('checkAuth', ctx.query, { token });
|
|
if (!token) {
|
|
return ctx.throw(401, 'not login');
|
|
}
|
|
// 鉴权代理
|
|
let tokenUser = await authCache.get(token);
|
|
if (!tokenUser) {
|
|
const tokenUserRes = await getTokenUser(token);
|
|
if (tokenUserRes.code !== 200) {
|
|
return ctx.throw(tokenUserRes.code, 'not login');
|
|
} else {
|
|
tokenUser = tokenUserRes.data;
|
|
}
|
|
authCache.set(token, tokenUser);
|
|
}
|
|
ctx.state = {
|
|
...ctx.state,
|
|
token,
|
|
tokenUser,
|
|
};
|
|
const { username } = tokenUser;
|
|
if (!auth.username) {
|
|
// 初始管理员账号
|
|
auth.username = username;
|
|
assistantConfig.setConfig({ auth });
|
|
}
|
|
if (isAdmin && auth.username) {
|
|
const admins = config.auth?.admin || [];
|
|
let isCheckAdmin = false;
|
|
const admin = auth.username;
|
|
if (admin === username) {
|
|
isCheckAdmin = true;
|
|
}
|
|
if (!isCheckAdmin && admins.length > 0 && admins.includes(username)) {
|
|
isCheckAdmin = true;
|
|
}
|
|
if (!isCheckAdmin) {
|
|
return ctx.throw(403, 'not admin user');
|
|
}
|
|
}
|
|
};
|
|
app
|
|
.route({
|
|
path: 'auth',
|
|
id: 'auth',
|
|
description: '获取当前登录用户信息, 第一个登录的用户为管理员用户',
|
|
})
|
|
.define(async (ctx) => {
|
|
await checkAuth(ctx);
|
|
})
|
|
.addTo(app);
|
|
app
|
|
.route({
|
|
path: 'admin-auth',
|
|
id: 'admin-auth',
|
|
description: '管理员鉴权, 获取用户信息,并验证是否为管理员。',
|
|
})
|
|
.define(async (ctx) => {
|
|
console.log('query', ctx.query);
|
|
await checkAuth(ctx, true);
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'client',
|
|
key: 'version',
|
|
description: '获取客户端版本号',
|
|
})
|
|
.define(async (ctx) => {
|
|
ctx.body = 'v1.0.0';
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'client',
|
|
key: 'time',
|
|
description: '获取当前时间',
|
|
})
|
|
.define(async (ctx) => {
|
|
ctx.body = {
|
|
time: new Date().getTime(),
|
|
date: new Date().toLocaleDateString(),
|
|
};
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'client',
|
|
key: 'system',
|
|
description: '获取系统信息',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { platform, arch, release } = os;
|
|
ctx.body = {
|
|
platform: platform(),
|
|
arch: arch(),
|
|
release: release(),
|
|
};
|
|
})
|
|
.addTo(app);
|