lrf402788946 4 yıl önce
ebeveyn
işleme
5782df0fab
3 değiştirilmiş dosya ile 66 ekleme ve 0 silme
  1. 5 0
      app/controller/home.js
  2. 1 0
      app/router.js
  3. 60 0
      app/service/util/spm.js

+ 5 - 0
app/controller/home.js

@@ -15,6 +15,11 @@ class HomeController extends Controller {
     const data = await this.ctx.service.users.product.indexQuery();
     this.ctx.ok({ data });
   }
+
+  async spm() {
+    const data = await this.ctx.service.util.spm.index(this.ctx.query, this.ctx.request.body);
+    this.ctx.ok({ data });
+  }
 }
 
 module.exports = HomeController;

+ 1 - 0
app/router.js

@@ -9,6 +9,7 @@ module.exports = app => {
   const profix = '/api/live/';
   const vision = 'v0';
   router.post(`${profix}${vision}/util`, controller.home.utilMethod);
+  router.post(`${profix}${vision}/spm`, controller.home.spm);
   router.get(`${profix}${vision}/index`, controller.home.indexQuery);
   require('./router/users/admin')(app); // 管理员
   require('./router/users/personal')(app); // 个人用户

+ 60 - 0
app/service/util/spm.js

@@ -0,0 +1,60 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 只属于这个项目的工具service
+class SpmService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'spm');
+    this.org = this.ctx.model.Organization;
+    this.personal = this.ctx.model.Personal;
+  }
+
+  /**
+   * 入口,根据method,分配到函数中进行处理,返回结果
+   * @param {Object} {method} method:在这个service的函数
+   * @param {*} body 条件
+   */
+  async index({ method }, body) {
+    return await this[method](body);
+  }
+
+  /**
+   * 获取用户信息,个人(专家)/企业都有可能
+   * @param {Object} condition 条件
+   */
+  async getAllUser(condition) {
+    const { ids } = condition;
+    const projection = 'name phone';
+    // 直接先用ids在企业表中查询
+    let arr = [];
+    const org = await this.org.find({ _id: ids }, projection);
+    arr = arr.concat(org);
+    if (org.length !== ids.length) {
+      // 还有个人用户,再查
+      const personal = await this.personal.find({ _id: ids }, projection);
+      arr = arr.concat(personal);
+    }
+    return arr;
+  }
+
+  /**
+   * 根据条件查找用户
+   * @param {Object} condition 查询条件
+   */
+  async getUser(condition) {
+    let res;
+    // 先查是不是企业
+    const org = await this.org.findOne(condition);
+    if (org) res = org;
+    else {
+      const personal = await this.personal.findOne(condition);
+      if (personal) res = personal;
+    }
+    return res;
+  }
+}
+
+module.exports = SpmService;