lrf 7 mesiacov pred
rodič
commit
c42e0c910f

+ 22 - 0
src/controller/home.controller.ts

@@ -5,6 +5,7 @@ import { SingleSignOnService } from '../service/singleSignOn.service';
 import { PermissionService } from '../service/permission.service';
 import { PemService } from '../service/pem.service';
 import SensitiveWordTool from 'sensitive-word-tool';
+import { EsDictService } from '../service/esDict.service';
 @Controller('/cxyy')
 export class HomeController {
   @Inject()
@@ -22,6 +23,9 @@ export class HomeController {
   @Inject()
   pemService: PemService;
 
+  @Inject()
+  esDict: EsDictService;
+
   @Get('/')
   async home(): Promise<string> {
     return 'proxy starting....';
@@ -43,6 +47,24 @@ export class HomeController {
     return 'makePairCrypto';
   }
 
+  /**
+   * es请求远程字典内容, 约1分钟来同步一次
+   * @returns 返回扩充字典内容
+   */
+  @Get('/es_dict')
+  async getEsDict() {
+    const result = await this.esDict.getDictContent();
+    const ranStr = this.esDict.randomStr();
+    try {
+      this.ctx.response.etag = ranStr;
+      this.ctx.response.lastModified = new Date();
+    } catch (error) {
+      console.log(error);
+    }
+
+    return result;
+  }
+
   @All('/**')
   async proxy() {
     // TODO:检查请求是否在白名单

+ 29 - 0
src/service/esDict.service.ts

@@ -0,0 +1,29 @@
+import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
+import { Provide, InjectClient, Config } from '@midwayjs/core';
+import { ServiceError, ErrorCode } from '../error/service.error';
+import { get } from 'lodash';
+
+@Provide()
+export class EsDictService {
+  @Config('axios.clients./cxyy/es.baseURL')
+  baseUrl: object;
+  @InjectClient(HttpServiceFactory, 'default')
+  serviceAxios: HttpService;
+  async getDictContent() {
+    const reqConfig: any = {
+      url: `${this.baseUrl}/cxyy/es/dict/remote`,
+      method: 'Get',
+    };
+    try {
+      const res = await this.serviceAxios.request(reqConfig);
+      if (res.status !== 200) throw new ServiceError(ErrorCode.REQUSET_ERROR);
+      const data = get(res, 'data.data');
+      return data;
+    } catch (error) {
+      console.log(error);
+    }
+  }
+  randomStr(len = 6) {
+    return Math.random().toString(36).slice(-len);
+  }
+}