Explorar el Código

Merge branch 'main' of http://git.cc-lotus.info/Information/cxyy-service into main

zs hace 8 meses
padre
commit
fd0a346e2c

+ 9 - 0
src/controller/system/message.controller.ts

@@ -44,6 +44,15 @@ export class RegionController implements BaseController {
   @ApiTags('创建数据')
   @Validate()
   async create(@Body() data: object) {
+    let oto = get(data, 'to', []);
+    oto = oto.map(i => {
+      const user_type = get(i, 'user_type');
+      if (!user_type) return i;
+      else if (user_type === 'admin') i.user_type = MessageUserType.ADMIN;
+      else if (user_type === 'user') i.user_type = MessageUserType.USER;
+      return i;
+    });
+    data['to'] = oto;
     const dbData = await this.service.create(data);
     // 需要让mq发送消息,触发前端接收消息
     const to = get(dbData, 'to', []);

+ 69 - 0
src/controller/users/contactApply.controller.ts

@@ -0,0 +1,69 @@
+import { Body, Controller, Get, Inject, Param, Post, Query } from '@midwayjs/core';
+import { ApiQuery, ApiTags } from '@midwayjs/swagger';
+import { ContactApplyService } from '../../service/users/contactApply.service';
+import { get, omit, pick } from 'lodash';
+import { Validate } from '@midwayjs/validate';
+import { ServiceError, ErrorCode } from '../../error/service.error';
+import { MessageService } from '../../service/system/message.service';
+
+const namePrefix = '联系方式';
+@ApiTags(['联系方式'])
+@Controller('/contactApply', { tagName: namePrefix })
+export class ContactApplyController {
+  @Inject()
+  service: ContactApplyService;
+  @Inject()
+  msgService: MessageService;
+
+  @Get('/')
+  @ApiTags('列表查询')
+  @ApiQuery({ name: 'query' })
+  async index(@Query() query: object) {
+    const qobj = omit(query, ['skip', 'limit']);
+    const others = pick(query, ['skip', 'limit']);
+    const result = await this.service.query(qobj, others);
+    return result;
+  }
+
+  @Post('/', { routerName: `申请${namePrefix}` })
+  @ApiTags('创建数据')
+  @Validate()
+  async create(@Body() data: object) {
+    /**
+     * 数据应该只有:
+     *  apply_user, source,source_id
+     * 根据source和source_id获取到数据,再根据数据的user获取普通用户信息,先将普通用户信息中的
+     * 用户id(user/user表的id),电话(phone)和邮箱(email)拿来给申请填充个数据
+     */
+    const otherData = await this.service.searchCompleteData(get(data, 'source'), get(data, 'source_id'));
+    const userData = get(otherData, 'userData');
+    if (!userData) throw new ServiceError(ErrorCode.CONTACTAPPLY_TARGET_USER_NOT_FOUND);
+    const target_user = get(userData, 'id');
+    data['target_user'] = target_user;
+    const res = await this.service.checkHasApply(data);
+    if (res) {
+      const status = get(res, 'status');
+      // 已有申请并通过审核,直接获取联系方式
+      if (status === '1') return { data: get(res, 'content') };
+    }
+    const dbData = await this.service.create(data);
+    const msgObj = await this.service.sendMqMsg(get(dbData, 'id'));
+    // 发送mq消息
+    await this.msgService.create(msgObj);
+    const to = get(msgObj, 'to', []);
+    for (const i of to) {
+      await this.msgService.toSendMq(get(i, 'user'), get(i, 'user_type'), get(dbData, 'id'));
+    }
+    return dbData;
+  }
+
+  @Post('/status/:id', { routerName: `审核${namePrefix}申请` })
+  @ApiTags('审核申请')
+  @Validate()
+  async update(@Param('id') id: number, @Body('status') status: string) {
+    if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
+    const result = await this.service.update({ id }, { status });
+    this.service.sendMqMsg(id);
+    return result;
+  }
+}

+ 1 - 1
src/entity/system/message.entity.ts

@@ -15,5 +15,5 @@ export class Message extends BaseModel {
   @Column({ type: 'jsonb', default: [], comment: '发送对象' })
   to: Array<toModel>;
   @Column({ type: 'character varying', nullable: true, comment: '消息类型' })
-  type: string; // 审核消息, 通知消息, 业务消息
+  type: string; // 通知消息:0,审核消息:1,业务消息:2
 }

+ 21 - 0
src/entity/users/contactApply.entity.ts

@@ -0,0 +1,21 @@
+import { Entity, Column } from "typeorm";
+import { BaseModel } from "../../frame/BaseModel";
+
+/**联系方式申请表 */
+@Entity('contactApply')
+export class ContactApply extends BaseModel {
+  @Column({ type: 'integer', nullable: true, comment: '申请用户id' })
+  apply_user: number;
+  @Column({ type: 'integer', nullable: true, comment: '被申请信息的用户id' })
+  target_user: number;
+  // 电话,联系人姓名,邮箱都动态获取
+  @Column({ type: 'character varying', nullable: true, comment: '申请来源' })
+  source: string; // 哪个表:需求,供给;专家
+  @Column({ type: 'character varying', nullable: true, comment: '申请来源id' })
+  source_id: string; // 哪个数据
+
+
+
+  @Column({ type: 'character varying', nullable: true, default: '1', comment: '审核状态: 0-未审核;1通过;-1拒绝' })
+  status: string;
+}

+ 5 - 1
src/error/service.error.ts

@@ -17,7 +17,7 @@ export enum ErrorCode {
   DUPLICATE_USERS_ACCOUNT = 'DUPLICATE_USERS_ACCOUNT',
   DUPLICATE_USERS_OPENID = 'DUPLICATE_USERS_OPENID',
 
-  PHONE_IS_EXISTS = "PHONE_IS_EXISTS",
+  PHONE_IS_EXISTS = 'PHONE_IS_EXISTS',
 
   // 参数
   ID_NOT_FOUND = 'ID_NOT_FOUND',
@@ -26,6 +26,10 @@ export enum ErrorCode {
   ADMIN_BODY_NICK_NAME = 'ADMIN_BODY_NICK_NAME',
   ADMIN_BODY_ACCOUNT = 'ADMIN_BODY_ACCOUNT',
   ADMIN_BODY_PASSWORD = 'ADMIN_BODY_PASSWORD',
+
+  // contactApply
+  CONTACTAPPLY_TARGET_USER_NOT_FOUND = 'TARGET_USER_NOT_FOUND',
+  CONTACTAPPLY_HAS_APPLY_NOT_STATUS = 'CONTACTAPPLY_HAS_APPLY_NOT_STATUS',
 }
 export class ServiceError extends Error {
   constructor(errcode: string) {

+ 3 - 2
src/event/dbSubscriber.ts

@@ -5,7 +5,6 @@ import { Config } from '@midwayjs/core';
 import { get, toLower } from 'lodash';
 import { Methods } from '../public/var';
 
-
 /**
  * 数据库监听类
  * 在这里通过对指定表的过滤,将数据变化通过http请求发送至es服务
@@ -88,7 +87,9 @@ export class DbSubscriber implements EntitySubscriberInterface {
     try {
       await Axios.post(url, body, { responseType: 'json' });
     } catch (error) {
-      console.log(error)
+      // console.log(error);
+    } finally {
+      return;
     }
   }
 }

+ 181 - 0
src/service/users/contactApply.service.ts

@@ -0,0 +1,181 @@
+import { Provide } from '@midwayjs/core';
+import { InjectEntityModel } from '@midwayjs/typeorm';
+import { Repository } from 'typeorm';
+import { ContactApply } from '../../entity/users/contactApply.entity';
+import { BaseServiceV2 } from '../../frame/BaseServiceV2';
+import { get } from 'lodash';
+import { Demand } from '../../entity/platform/demand.entity';
+import { Supply } from '../../entity/platform/supply.entity';
+import { Expert } from '../../entity/users/expert.entity';
+import { User } from '../../entity/system/user.entity';
+import { ErrorCode, ServiceError } from '../../error/service.error';
+import { MessageUserType } from '../../public/var';
+
+@Provide()
+export class ContactApplyService extends BaseServiceV2 {
+  getQueryColumnsOpera(): object {
+    return {};
+  }
+  @InjectEntityModel(ContactApply)
+  model: Repository<ContactApply>;
+
+  @InjectEntityModel(Demand)
+  demand: Repository<Demand>;
+
+  @InjectEntityModel(Supply)
+  supply: Repository<Supply>;
+
+  @InjectEntityModel(Expert)
+  expert: Repository<Expert>;
+
+  @InjectEntityModel(User)
+  user: Repository<User>;
+
+  /**
+   * 发送mq消息
+   * 如果status是0,那就给被申请人发送;如果是1,那就给申请人发送联系方式,如果是-1:那就给申请人发送拒绝申请
+   * 需求是先从需求中找联系方式,没有再从用户中招,其余的都直接从用户中找
+   * @param id 数据id
+   */
+  async sendMqMsg(id) {
+    const data = await this.model.createQueryBuilder().where(`"id" = :id`, { id }).getOne();
+    if (!data) return;
+    const status = get(data, 'status');
+    const source = get(data, 'source');
+    const source_id = get(data, 'source_id');
+    const otherData = await this.searchCompleteData(source, source_id);
+    const targetUserData = await this.getUserData(get(data, 'target_user'));
+    const applyUserData = await this.getUserData(get(data, 'apply_user'));
+    let sourceStr;
+    let sourceProp;
+    let sourcePropValue;
+    let mqTarget;
+    let msgStr;
+    let mqUserType = MessageUserType.USER;
+    let mqUserNickName;
+    switch (source) {
+      case 'demand':
+        sourceStr = '需求';
+        sourceProp = 'name';
+        break;
+      case 'supply':
+        sourceStr = '供给';
+        sourceProp = 'name';
+        break;
+      case 'expert':
+        sourceStr = '专家';
+        break;
+      default:
+        break;
+    }
+    const sourceData = get(otherData, 'sourceData');
+    if (sourceProp) sourcePropValue = get(sourceData, sourceProp);
+    switch (status) {
+      case '0':
+        // 给被申请人发送和mq消息
+        mqTarget = get(data, 'target_user');
+        mqUserNickName = get(targetUserData, 'nick_name');
+        const apply_userName = get(applyUserData, 'nick_name');
+        msgStr = `用户${apply_userName} 通过 ${sourceStr}数据 ${sourcePropValue ? `- ${sourcePropValue}` : ''} 申请获取您的联系方式,尽快审核避免错失良机!`;
+        break;
+      case '1':
+        // 给申请人发送消息和mq消息
+        mqTarget = get(data, 'apply_user');
+        mqUserNickName = get(applyUserData, 'nick_name');
+        const contacts = this.getContactObject(otherData, source);
+        msgStr = `您通过 ${sourceStr}数据 ${sourcePropValue ? `- ${sourcePropValue}` : ''} 获取对方联系方式的申请已通过.`;
+        // 拼接联系方式
+        const person = get(contacts, 'person');
+        if (person) msgStr = `${msgStr} 联系人: ${person};`;
+        const phone = get(contacts, 'phone');
+        if (phone) msgStr = `${msgStr} 联系电话: ${phone};`;
+        const email = get(contacts, 'email');
+        if (email) msgStr = `${msgStr} 邮箱: ${email}`;
+        break;
+      case '-1':
+        mqTarget = get(data, 'apply_user');
+        mqUserNickName = get(applyUserData, 'nick_name');
+        msgStr = `您通过 ${sourceStr}数据 ${sourcePropValue ? `- ${sourcePropValue}` : ''} 获取对方联系方式的申请已被拒绝.`;
+        break;
+      default:
+        break;
+    }
+    // 创建消息,发送
+    const to = [{ user: mqTarget, user_type: mqUserType, user_nick_name: mqUserNickName }];
+    const msgObj = { content: msgStr, to, type: '0' };
+    return msgObj;
+  }
+  /**
+   * 获取用户数据
+   * @param id 用户id
+   * @returns
+   */
+  async getUserData(id) {
+    const user = await this.user.createQueryBuilder().where(`"id" = :id`, { id }).getOne();
+    return user;
+  }
+  /**
+   * 根据 apply_user ,target_user检查是否有过申请且已通过审核的数据
+   * 如果有:直接返回信息;
+   * 如果没有,返回继续执行
+   * @param data
+   */
+  async checkHasApply(data) {
+    const apply_user = get(data, 'apply_user');
+    const target_user = get(data, 'target_user');
+    const builder = this.model.createQueryBuilder().where(`"apply_user" = :apply_user`, { apply_user }).andWhere(`"target_user" = :target_user`, { target_user });
+    const apply = await builder.andWhere(`"status" = :status`, { status: '1' }).getOne();
+    if (apply) {
+      const msgObj = await this.sendMqMsg(get(apply, 'id'));
+      return { status: '1', content: msgObj.content };
+    }
+    // 查询是不是有其他状态的申请: 未审核,已拒绝,如果有,就根据状态返回提示语
+    const othersApply = await builder.getOne();
+    // 没有找到其他的申请,直接返回
+    if (!othersApply) return;
+    const status = get(othersApply, 'status');
+    // 有没审核的申请,抛出异常
+    if (status === '0') throw new ServiceError(ErrorCode.CONTACTAPPLY_HAS_APPLY_NOT_STATUS);
+    // 被拒绝了也允许重新申请
+    else if (status === '-1') return;
+  }
+  /**
+   * 组织联系信息
+   * @param data searchCompleteData的返回
+   * @param source 来源
+   */
+  getContactObject(data, source) {
+    const contacts: any = {};
+    if (source === 'demand') {
+      const sourceData = get(data, 'sourceData');
+      contacts.phone = get(sourceData, 'tel');
+      contacts.person = get(sourceData, 'contacts');
+    }
+    if (Object.keys(contacts).length <= 0) {
+      const user = get(data, 'userData');
+      contacts.phone = get(user, 'phone');
+      contacts.person = get(user, 'nick_name');
+      contacts.email = get(user, 'email');
+    }
+    return contacts;
+  }
+
+  /**
+   * 根据source和source_id,查找数据,再查找数据拥有人的信息,返回数据
+   * @param source 数据来源
+   * @param source_id 数据来源的id
+   */
+  async searchCompleteData(source, source_id) {
+    let model = this[source];
+    if (!model) return;
+    const sourceData = await model.createQueryBuilder().where(`"id" = :id`, { id: source_id }).getOne();
+    if (!sourceData) return;
+    const result = { sourceData };
+    const userId = get(sourceData, 'user');
+    if (!userId) return result;
+    const user = await this.user.createQueryBuilder().where(`"id" = :id`, { id: userId }).getOne();
+    if (!user) return result;
+    result['userData'] = user;
+    return result;
+  }
+}