- 新增 user_app / user_identity 模型、应用注册表与成员关系服务,启动时校验配置覆盖 - 新增微信开放平台扫码登录:二维码地址、回调与 ticket 换取 token - 管理端接口改为路由层声明 appIds + 角色鉴权,新增批量用户信息、锁定、授权与个人配置 - 删除账号同步清理成员关系与外部身份,避免关系被新账号复用 - 补充多应用、应用配置、注册表测试与迁移/建号脚本
84 lines
2.0 KiB
JavaScript
84 lines
2.0 KiB
JavaScript
import Koa from 'koa';
|
|
import bodyParser from 'koa-bodyparser';
|
|
import cors from '@koa/cors';
|
|
import http from 'http';
|
|
import { DBModel } from './models/index.js';
|
|
import { assertRegistryCoverage } from './models/app_registry.js';
|
|
import registerRoutes from './routes/index.js';
|
|
import ResponseUtil from './utils/api_response.js';
|
|
|
|
class APP {
|
|
constructor() {
|
|
// 配置缺失在启动阶段暴露,而不是等某个应用首次登录才报错
|
|
assertRegistryCoverage();
|
|
this.app = new Koa();
|
|
this.setupDB();
|
|
this.setupMiddleware();
|
|
this.setupRoutes();
|
|
this.setupFallback();
|
|
}
|
|
|
|
setupDB() {
|
|
DBModel.init();
|
|
}
|
|
|
|
setupMiddleware() {
|
|
// 服务在 nginx 之后,需信任代理头才能拿到真实客户端 IP
|
|
// (否则 ctx.ip 恒为 127.0.0.1,限流会变成全站共享一个桶)
|
|
this.app.proxy = true;
|
|
this.app.proxyIpHeader = "X-Real-IP";
|
|
|
|
this.app.use(cors());
|
|
this.app.use(bodyParser());
|
|
|
|
this.app.use(async (ctx, next) => {
|
|
const start = Date.now();
|
|
await next();
|
|
const ms = Date.now() - start;
|
|
console.log(`${ctx.method} ${ctx.url} - ${ctx.status} - ${ms}ms`);
|
|
});
|
|
}
|
|
|
|
setupRoutes() {
|
|
this.app.use(async (ctx, next) => {
|
|
if (ctx.path === '/') {
|
|
ResponseUtil.success(ctx, { name: 'user-api', version: '1.0.0' });
|
|
return;
|
|
}
|
|
await next();
|
|
});
|
|
|
|
this.app.use(registerRoutes.getRoutes());
|
|
this.app._router = registerRoutes.router;
|
|
}
|
|
|
|
setupFallback() {
|
|
this.app.use(async (ctx) => {
|
|
ctx.status = 404;
|
|
ctx.body = { code: 404, msg: 'Not Found' };
|
|
});
|
|
}
|
|
|
|
start(port) {
|
|
this.server = http.createServer(this.app.callback());
|
|
this.server.listen(port, () => {
|
|
console.log(`Server: running on http://localhost:${port}`);
|
|
});
|
|
}
|
|
|
|
stop() {
|
|
if (this.server) {
|
|
this.server.close();
|
|
this.server = null;
|
|
console.log('Server stopped');
|
|
}
|
|
}
|
|
|
|
static createKoaApp() {
|
|
const instance = new APP();
|
|
return instance.app;
|
|
}
|
|
}
|
|
|
|
export { APP };
|