瀏覽代碼

系统消息创建方法重写

guhongwei 2 年之前
父節點
當前提交
ff9ba2ec7b
共有 3 個文件被更改,包括 53 次插入2 次删除
  1. 1 0
      README.md
  2. 5 1
      src/config/config.default.ts
  3. 47 1
      src/service/message.service.ts

+ 1 - 0
README.md

@@ -27,3 +27,4 @@ $ npm start
 
 
 
 
 [midway]: https://midwayjs.org
 [midway]: https://midwayjs.org
+1

+ 5 - 1
src/config/config.default.ts

@@ -11,7 +11,11 @@ export default {
   },
   },
   redis_timeout: 300, //s
   redis_timeout: 300, //s
   axios: {
   axios: {
-    clients: {},
+    clients: {
+      admin: {
+        baseURL: 'http://127.0.0.1:13010/jcyjdtglpt/v1/api',
+      },
+    },
   },
   },
 
 
 } as MidwayConfig;
 } as MidwayConfig;

+ 47 - 1
src/service/message.service.ts

@@ -1,11 +1,57 @@
-import { Provide } from '@midwayjs/decorator';
+import { Provide, InjectClient } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { BaseService } from 'free-midway-component';
 import { Message } from '../entity/message.entity';
 import { Message } from '../entity/message.entity';
+import { HttpService, HttpServiceFactory } from '@midwayjs/axios';
+import _ = require('lodash');
 type modelType = ReturnModelType<typeof Message>;
 type modelType = ReturnModelType<typeof Message>;
 @Provide()
 @Provide()
 export class MessageService extends BaseService<modelType> {
 export class MessageService extends BaseService<modelType> {
   @InjectEntityModel(Message)
   @InjectEntityModel(Message)
   model: modelType;
   model: modelType;
+
+  @InjectClient(HttpServiceFactory, 'admin')
+  adminAxios: HttpService;
+  async create(body) {
+    if (body.type !== '3') {
+      // 企业用户
+      let cList = [];
+      const curi = '/unitStudioApply';
+      const company: any = await this.adminAxios.get(curi, { params: { status: '1' } });
+      if (company && company.total > 0) cList = this.searchC(company.data, '1')
+      // 科学家用户
+      let sList = [];
+      const suri = '/userStudioApply';
+      const users: any = await this.adminAxios.get(suri, { params: { status: '1' } });
+      if (users && users.total > 0) sList = this.searchC(users.data, '2')
+      // 组装接收人数据
+      if (body.type === '0') body.user = [...cList, ...sList];
+      else if (body.type === '1') body.user = [...cList];
+      else if (body.type === '2') body.user = [...sList];
+    }
+    const res = await this.model.create(body);
+    return res;
+  }
+  // 整理user数组数据
+  searchC(e, type) {
+    const list = [];
+    for (const val of e) {
+      const data = { id: String, company: String, name: String, phone: String };
+      if (type === '1') {
+        const p1 = _.pick(val, ['id', 'company', 'phone']);
+        data.id = p1.id;
+        data.company = p1.company;
+        data.phone = p1.phone;
+      }
+      else if (type === '2') {
+        const p2 = _.pick(val, ['id', 'name', 'phone']);
+        data.id = p2.id;
+        data.name = p2.name;
+        data.phone = p2.phone.phone;
+      }
+      list.push(data);
+    }
+    return list;
+  }
 }
 }