|
@@ -1,11 +1,57 @@
|
|
|
-import { Provide } from '@midwayjs/decorator';
|
|
|
+import { Provide, InjectClient } from '@midwayjs/decorator';
|
|
|
import { InjectEntityModel } from '@midwayjs/typegoose';
|
|
|
import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
import { BaseService } from 'free-midway-component';
|
|
|
import { Message } from '../entity/message.entity';
|
|
|
+import { HttpService, HttpServiceFactory } from '@midwayjs/axios';
|
|
|
+import _ = require('lodash');
|
|
|
type modelType = ReturnModelType<typeof Message>;
|
|
|
@Provide()
|
|
|
export class MessageService extends BaseService<modelType> {
|
|
|
@InjectEntityModel(Message)
|
|
|
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;
|
|
|
+ }
|
|
|
}
|