lrf 2 years ago
parent
commit
11a63181df
3 changed files with 42 additions and 11 deletions
  1. 9 6
      package.json
  2. 26 4
      src/configuration.ts
  3. 7 1
      src/error/service.error.ts

+ 9 - 6
package.json

@@ -25,17 +25,20 @@
     "@midwayjs/decorator": "^3.0.0",
     "@midwayjs/koa": "^3.0.0",
     "@midwayjs/mock": "^3.0.0",
+    "@midwayjs/typegoose": "^3.0.0",
+    "@typegoose/typegoose": "^9.0.0",
     "@types/jest": "^29.2.3",
+    "@types/lodash": "^4.14.190",
     "@types/node": "^16.11.22",
     "cross-env": "^6.0.0",
     "jest": "^29.2.2",
+    "lodash": "^4.17.21",
+    "mongoose": "^6.0.7",
     "mwts": "^1.0.5",
     "ts-jest": "^29.0.3",
-    "typescript": "~4.8.0",
-    "@types/lodash": "^4.14.190",
-    "@midwayjs/typegoose": "^3.0.0",
-    "@typegoose/typegoose": "^9.0.0",
-    "lodash": "^4.17.21",
-    "mongoose": "^6.0.7"
+    "typescript": "~4.8.0"
+  },
+  "dependencies": {
+    "@midwayjs/axios": "^3.9.0"
   }
 }

+ 26 - 4
src/configuration.ts

@@ -6,9 +6,22 @@ import * as typegoose from '@midwayjs/typegoose';
 import * as Typegoose from '@typegoose/typegoose';
 import { ResponseMiddleware } from './middleware/response.middleware';
 import { DefaultErrorFilter } from './filter/default.filter';
+import * as axios from '@midwayjs/axios';
+import { ServiceError, FrameworkErrorEnum } from './error/service.error';
+import { IMidwayContainer } from '@midwayjs/core';
+
+const axiosResponse = response => {
+  if (response.status === 200) return response.data;
+  else {
+    throw new ServiceError('请求失败', FrameworkErrorEnum.REQUEST_FAULT);
+  }
+};
+const axiosError = error => {
+  return Promise.reject(error);
+};
 @Configuration({
   namespace: 'free',
-  imports: [koa, typegoose],
+  imports: [koa, typegoose, axios],
   importConfigs: [
     {
       default: DefaultConfig,
@@ -21,13 +34,13 @@ export class FreeConfiguration {
   getApiUrl() {
     const path = this.app.getConfig()?.swagger?.swaggerPath;
     const port = this.app.getConfig()?.koa?.port;
-    if (path)
-      console.log(`api文档: http://127.0.0.1:${port}${path}/index.html`);
+    if (path) console.log(`api文档: http://127.0.0.1:${port}${path}/index.html`);
   }
-  async onReady() {
+  async onReady(container: IMidwayContainer) {
     // TODO something
     this.app.getMiddleware().insertLast(ResponseMiddleware);
     this.app.useFilter([DefaultErrorFilter]);
+    // typegoose设置
     Typegoose.setGlobalOptions({
       schemaOptions: {
         id: true,
@@ -37,6 +50,15 @@ export class FreeConfiguration {
       options: { allowMixed: Typegoose.Severity.ALLOW },
     });
     this.getApiUrl();
+    // axios设置
+    const httpServiceFactory = await container.getAsync(axios.HttpServiceFactory);
+    const aos: object = this.app.getConfig('axios.clients');
+    const keys = Object.keys(aos);
+    for (const key of keys) {
+      const a = httpServiceFactory.get(key);
+      a.interceptors.response.use(axiosResponse, axiosError);
+    }
+
     console.log('free frame is ready');
   }
 }

+ 7 - 1
src/error/service.error.ts

@@ -29,6 +29,8 @@ export enum ErrorCode {
   NOT_LOGIN = '-101',
   /** 密码错误 */
   BAD_PASSWORD = '-102',
+  /**请求错误 */
+  REQUEST_FAULT = '500',
 }
 
 /** 自定义错误枚举数据 */
@@ -40,7 +42,11 @@ export const FrameworkErrorEnum = registerErrorCode('ServiceError', ErrorCode);
  */
 export class ServiceError extends MidwayError {
   constructor(str: string, errcode: string = ErrorCode.UNKNOWN, detail?: any) {
-    if (detail) console.log(detail);
     super(str, errcode);
+    if (detail) {
+      console.log(detail);
+      this.detail = detail;
+    }
   }
+  detail: any;
 }