lrf hai 9 meses
pai
achega
a0aef937f2

+ 62 - 0
src/controller/contactSearch.controller.ts

@@ -0,0 +1,62 @@
+import { Controller, Get, Inject, Query } from '@midwayjs/core';
+import { NeedLoginMiddleware } from '../middleware/needLogin.middleware';
+import { ContactSearchService } from '../service/contactSearch.service';
+
+/**
+ * 预约对接查询匹配度
+ */
+@Controller('/cs', { middleware: [NeedLoginMiddleware] })
+export class ContactSearchController {
+  @Inject()
+  csService: ContactSearchService;
+  /**
+   * 来源是需求的匹配查询
+   * 需要查询 当前用户 通过审核的 供给和成果数据
+   * @param keyword 关键词
+   */
+  @Get('/demand')
+  async demand(@Query('keyword') keyword: string) {
+    if (!keyword) return;
+    const supplyConfig = { index: 'supply', fields: ['name', 'tags', 'brief'] };
+    const sres = await this.csService.search(keyword, supplyConfig);
+    const achievementConfig = { index: 'achievement', fields: ['name', 'tags', 'brief'] };
+    const ares = await this.csService.search(keyword, achievementConfig);
+    return sres || ares;
+  }
+
+  /**
+   * 来源是供给的匹配查询
+   * 需要查询 当前用户 通过审核的 需求数据
+   * @param keyword 关键词
+   */
+  @Get('/supply')
+  async supply(@Query('keyword') keyword: string) {
+    const config = { index: 'demand', fields: ['name', 'tags', 'brief'] };
+    const res = await this.csService.search(keyword, config);
+    return res;
+  }
+
+  /**
+   * 来源是项目的匹配查询
+   * 需要查询 当前用户 通过审核的 需求数据
+   * @param keyword 关键词
+   */
+  @Get('/project')
+  async projectr(@Query('keyword') keyword: string) {
+    const config = { index: 'demand', fields: ['name', 'tags', 'brief'] };
+    const res = await this.csService.search(keyword, config);
+    return res;
+  }
+
+  /**
+   * 来源是成果的匹配查询
+   * 需要查询 当前用户 通过审核的 需求数据
+   * @param keyword 关键词
+   */
+  @Get('/achievement')
+  async achievement(@Query('keyword') keyword: string) {
+    const config = { index: 'demand', fields: ['name', 'tags', 'brief'] };
+    const res = await this.csService.search(keyword, config);
+    return res;
+  }
+}

+ 20 - 0
src/middleware/needLogin.middleware.ts

@@ -0,0 +1,20 @@
+import { Context, IMiddleware, Middleware, NextFunction } from '@midwayjs/core';
+import { get } from 'lodash';
+
+@Middleware()
+export class NeedLoginMiddleware implements IMiddleware<Context, NextFunction> {
+  resolve() {
+    return async (ctx: Context, next: NextFunction) => {
+      const user = get(ctx, 'user');
+      if (!user) return;
+      const role = get(user, 'role', []);
+      const has_user = role.find(f => f === 'User');
+      if (!has_user) return;
+      await next();
+    };
+  }
+
+  static getName(): string {
+    return 'needLogin';
+  }
+}

+ 75 - 0
src/service/contactSearch.service.ts

@@ -0,0 +1,75 @@
+import { Client } from '@elastic/elasticsearch';
+import { Provide, Config, Init, Inject } from '@midwayjs/core';
+import { Context } from '@midwayjs/koa';
+import { floor, get, head } from 'lodash';
+
+@Provide()
+export class ContactSearchService {
+  @Config('elasticsearch')
+  esConfig: object;
+  @Inject()
+  ctx: Context;
+  /**es连接实例 */
+  esClient: Client;
+  @Init()
+  async initClient() {
+    const esClient = new Client(this.esConfig);
+    this.esClient = esClient;
+  }
+
+  async search(keyword: string, config: object) {
+    const index = get(config, 'index');
+    const fields = get(config, 'fields', []);
+    const user_id = get(this.ctx, 'user.id');
+    // 添加条件: 当前用户, status:'1'(已审核)
+    const searchObject = {
+      index,
+      query: {
+        bool: {
+          must: [
+            {
+              multi_match: {
+                query: keyword,
+                fields,
+              },
+            },
+            {
+              match_phrase: { status: '1' },
+            },
+            {
+              match_phrase: { user: user_id },
+            },
+          ],
+        },
+      },
+      from: 0,
+      size: 1,
+    };
+    const result = await this.esClient.search(searchObject);
+    const returnData = this.dealResponses(result);
+    // 查找,是否有推荐度为4或4以上的数据.
+    const data = head(get(returnData, 'data', []));
+    if (!data) return false;
+    else {
+      const recommend = get(data, '_recommend', 0);
+      return recommend >= 4;
+    }
+  }
+
+  dealResponses(result) {
+    const total = get(result, 'hits.total.value', 0);
+    const hits = get(result, 'hits.hits', []);
+    const list = [];
+    for (const ol of hits) {
+      const _score = get(ol, '_score', 0);
+      const data = get(ol, '_source', {});
+      let recommend = 0;
+      /**推荐星数计算:超过5,就都是5颗星.未超过5的都向下取整 */
+      if (_score >= 5) recommend = 5;
+      else recommend = floor(_score);
+      data._recommend = recommend;
+      list.push(data);
+    }
+    return { total, data: list };
+  }
+}