import Router from 'koa-router'; import { HandlerUser } from '../handler/users.js'; import { rateLimit } from '../middleware/ratelimit.js'; import { auth } from '../middleware/auth.js'; import { ADMIN_APP_IDS } from '../models/app_registry.js'; const adminAuth = auth({ appIds: ADMIN_APP_IDS, roles: ['admin'] }); class ApiRouter { constructor() { this.router = new Router(); this.handler = new HandlerUser(); this.setupRoutes(); } setupRoutes() { const userRouter = new Router({ prefix: '/user' }); // 限流:注册/登录接口 5次/分钟,微信登录 10次/分钟 userRouter.post('/register', rateLimit(5, 60_000), this.handler.register.bind(this.handler)); userRouter.post('/signin', rateLimit(10, 60_000), this.handler.signin.bind(this.handler)); userRouter.post('/wxsignin', rateLimit(10, 60_000), this.handler.wxSignin.bind(this.handler)); // 微信扫码登录(开放平台网站应用):生成二维码地址 / 微信回调 / ticket 换 token userRouter.get('/wechat/qr/url', auth({ required: false }), rateLimit(20, 60_000), this.handler.wechatQrUrl.bind(this.handler)); userRouter.get('/wechat/qr/callback', rateLimit(20, 60_000), this.handler.wechatQrCallback.bind(this.handler)); userRouter.post('/wechat/qr/exchange', rateLimit(20, 60_000), this.handler.wechatQrExchange.bind(this.handler)); userRouter.post('/signout', auth({ required: false }), this.handler.signout.bind(this.handler)); userRouter.post('/userInfo', auth({ required: false }), this.handler.userInfo.bind(this.handler)); userRouter.post('/update', auth(), this.handler.updateUser.bind(this.handler)); // 管理端接口统一在路由层声明应用与角色,handler 内不再重复鉴权 userRouter.post('/list', adminAuth, this.handler.userList.bind(this.handler)); // 管理端:批量取用户信息(就诊人/订单展示归属用户)、锁定/解锁 userRouter.post('/info/batch', adminAuth, this.handler.batchUserInfo.bind(this.handler)); userRouter.post('/lock', adminAuth, this.handler.lockUser.bind(this.handler)); // 应用内个人配置:登录用户只能改自己的配置,成员关系在 handler 内按 appId 校验 userRouter.post('/app-config', auth(), this.handler.appConfig.bind(this.handler)); // 授权入口:管理员授予/撤销应用角色 userRouter.post('/grant', adminAuth, this.handler.grant.bind(this.handler)); userRouter.post('/wxgetphonenumber', rateLimit(10, 60_000), this.handler.wxGetPhoneNumber.bind(this.handler)); this.router.use(userRouter.routes()); this.printRoutes(this.router.stack); } getRoutes() { return this.router.routes(); } printRoutes(stack) { for (const layer of stack) { if (layer.path) { const methods = layer.methods.filter(m => m !== '_all'); methods.forEach(m => console.log(` [${m.toUpperCase()}] ${layer.path}`)); } } } } export default new ApiRouter();