checkOnePointLogin.middleware.ts 937 B

12345678910111213141516171819202122232425262728
  1. import { IMiddleware, Inject, MidwayWebRouterService } from '@midwayjs/core';
  2. import { Middleware } from '@midwayjs/decorator';
  3. import { NextFunction, Context } from '@midwayjs/koa';
  4. import { LoginService } from '../service/login.service';
  5. @Middleware()
  6. export class CheckOnePointLoginMiddleware implements IMiddleware<Context, NextFunction> {
  7. @Inject()
  8. webRouterService: MidwayWebRouterService;
  9. resolve() {
  10. return async (ctx: Context, next: NextFunction) => {
  11. const routeInfo = await this.webRouterService.getMatchedRouterInfo(ctx.path, ctx.method);
  12. if(!routeInfo) {
  13. await next()
  14. return
  15. }
  16. const desc = routeInfo.description;
  17. if (desc === 'ignore') {
  18. const loginService = await ctx.requestContext.getAsync(LoginService);
  19. await loginService.onePointCheck();
  20. }
  21. await next();
  22. };
  23. }
  24. static getName(): string {
  25. return 'checkOnePonitLogin';
  26. }
  27. }