123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import { Configuration, App, Inject } from '@midwayjs/decorator';
- import * as koa from '@midwayjs/koa';
- import * as validate from '@midwayjs/validate';
- import * as info from '@midwayjs/info';
- import { join } from 'path';
- import * as FreeFrame from 'free-midway-component';
- // api文档
- import * as swagger from '@midwayjs/swagger';
- import * as redis from '@midwayjs/redis';
- import * as jwt from '@midwayjs/jwt';
- import { CheckTokenMiddleware } from './middleware/checkToken.middleware';
- import * as axios from '@midwayjs/axios';
- import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
- import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
- import * as rabbitmq from '@midwayjs/rabbitmq';
- import { Context } from '@midwayjs/koa';
- const axiosResponse = response => {
- if (response.status === 200) return response.data;
- else {
- console.log(JSON.stringify(response));
- throw new ServiceError('请求失败', FrameworkErrorEnum.SERVICE_FAULT);
- }
- };
- const axiosError = error => {
- return Promise.reject(error);
- };
- @Configuration({
- imports: [
- FreeFrame,
- validate,
- jwt,
- axios,
- // swagger,
- rabbitmq,
- {
- component: swagger,
- enabledEnvironment: ['local'],
- },
- {
- component: info,
- enabledEnvironment: ['local'],
- },
- redis,
- ],
- importConfigs: [join(__dirname, './config')],
- })
- export class ContainerLifeCycle implements ILifeCycle {
- @App()
- app: koa.Application;
- @Inject()
- ctx: Context;
- async onReady(container: IMidwayContainer) {
- this.app.getMiddleware().insertFirst(CheckTokenMiddleware);
- // base 添加头设置
- const httpServiceFactory = await container.getAsync(axios.HttpServiceFactory);
- const baseAxios = httpServiceFactory.get('base');
- baseAxios.defaults.headers.common['project'] = 'group-service';
- baseAxios.interceptors.response.use(axiosResponse, axiosError);
- const wechatAxios = httpServiceFactory.get('wechat');
- wechatAxios.interceptors.response.use(axiosResponse, axiosError);
- }
- }
|