浏览代码

修改聊天记录表

zs 1 年之前
父节点
当前提交
9a10913a10

+ 69 - 0
src/controller/platform/chat.controller.ts

@@ -0,0 +1,69 @@
+import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';
+import { BaseController } from 'free-midway-component';
+import { ChatService } from '../../service/platform/chat.service';
+import { CDTO_chat, CVO_chat, FVO_chat, QDTO_chat, QVO_chat, UDTO_chat, UVAO_chat } from '../../interface/platform/chat.interface';
+import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+@ApiTags(['聊天记录'])
+@Controller('/chat')
+export class ChatController extends BaseController {
+  @Inject()
+  service: ChatService;
+
+  @Post('/')
+  @Validate()
+  @ApiResponse({ type: CVO_chat })
+  async create(@Body() data: CDTO_chat) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_chat(dbData);
+    return result;
+  }
+  @Get('/')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_chat })
+  async query(@Query() filter) {
+    const list = await this.service.query(filter);
+    return list;
+  }
+
+  @Get('/chat')
+  @ApiQuery({ name: 'query' })
+  async chat(@Query() filter) {
+    const list = await this.service.chat(filter);
+    return list;
+  }
+
+  @Get('/:id')
+  @ApiResponse({ type: FVO_chat })
+  async fetch(@Param('id') id: string) {
+    const data = await this.service.fetch(id);
+    const result = new FVO_chat(data);
+    return result;
+  }
+
+  @Post('/:id')
+  @Validate()
+  @ApiResponse({ type: UVAO_chat })
+  async update(@Param('id') id: string, @Body() body: UDTO_chat) {
+    const result = await this.service.updateOne(id, body);
+    return result;
+  }
+
+  @Del('/:id')
+  @Validate()
+  async delete(@Param('id') id: string) {
+    await this.service.delete(id);
+    return 'ok';
+  }
+  async createMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async updateMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async deleteMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+}

+ 19 - 0
src/entity/platform/chat.entity.ts

@@ -0,0 +1,19 @@
+import { modelOptions, prop } from '@typegoose/typegoose';
+import { BaseModel } from 'free-midway-component';
+@modelOptions({
+  schemaOptions: { collection: 'chat' },
+})
+export class Chat extends BaseModel {
+  @prop({ required: false, index: true, zh: '发送者id' })
+  sender_id: string;
+  @prop({ required: false, index: false, zh: '接收者id' })
+  receiver_id: string;
+  @prop({ required: false, index: false, zh: '内容类型' })
+  type: string;
+  @prop({ required: false, index: true, zh: '内容' })
+  content: string;
+  @prop({ required: false, index: false, zh: '发送时间' })
+  send_time: string;
+  @prop({ required: false, index: false, zh: '是否已读', default: '0' })
+  is_read: string;
+}

+ 90 - 0
src/interface/platform/chat.interface.ts

@@ -0,0 +1,90 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import { SearchBase } from 'free-midway-component';
+import get = require('lodash/get');
+const dealVO = (cla, data) => {
+  for (const key in cla) {
+    const val = get(data, key);
+    if (val || val === 0) cla[key] = val;
+  }
+};
+export class FVO_chat {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: '发送者id' })
+  'sender_id': string = undefined;
+  @ApiProperty({ description: '接收者id' })
+  'receiver_id': string = undefined;
+  @ApiProperty({ description: '内容类型' })
+  'type': string = undefined;
+  @ApiProperty({ description: '内容' })
+  'content': string = undefined;
+  @ApiProperty({ description: '发送时间' })
+  'send_time': string = undefined;
+  @ApiProperty({ description: '是否已读' })
+  'is_read': string = undefined;
+}
+
+export class QDTO_chat extends SearchBase {
+  constructor() {
+    const like_prop = [];
+    const props = ['sender_id', 'content'];
+    const mapping = [];
+    super({ like_prop, props, mapping });
+  }
+  @ApiProperty({ description: '发送者id' })
+  'sender_id': string = undefined;
+  @ApiProperty({ description: '内容' })
+  'content': string = undefined;
+}
+
+export class QVO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_chat {
+  @ApiProperty({ description: '发送者id' })
+  @Rule(RuleType['string']().empty(''))
+  'sender_id': string = undefined;
+  @ApiProperty({ description: '接收者id' })
+  @Rule(RuleType['string']().empty(''))
+  'receiver_id': string = undefined;
+  @ApiProperty({ description: '内容类型' })
+  @Rule(RuleType['string']().empty(''))
+  'type': string = undefined;
+  @ApiProperty({ description: '内容' })
+  @Rule(RuleType['string']().empty(''))
+  'content': string = undefined;
+  @ApiProperty({ description: '发送时间' })
+  @Rule(RuleType['string']().empty(''))
+  'send_time': string = undefined;
+  @ApiProperty({ description: '是否已读' })
+  @Rule(RuleType['string']().empty(''))
+  'is_read': string = undefined;
+}
+
+export class CVO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_chat extends CDTO_chat {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['string']().empty(''))
+  _id: string = undefined;
+}
+
+export class UVAO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}

+ 76 - 0
src/service/platform/chat.service.ts

@@ -0,0 +1,76 @@
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { BaseService } from 'free-midway-component';
+import { Chat } from '../../entity/platform/chat.entity';
+import { get } from 'lodash';
+import { User } from '../../entity/system/user.entity';
+type modelType = ReturnModelType<typeof Chat>;
+@Provide()
+export class ChatService extends BaseService<modelType> {
+  @InjectEntityModel(Chat)
+  model: modelType;
+
+  @InjectEntityModel(User)
+  uModel: ReturnModelType<typeof User>;
+
+  // 聊天
+  async chat(filter) {
+    const user = this.ctx.user;
+    const { skip = 0, limit = 0, ...condition } = filter;
+    const list = await this.model.aggregate([
+      {
+        $match: {
+          ...condition,
+          $or: [{ sender_id: get(user, '_id') }, { receiver_id: get(user, '_id') }],
+        },
+      },
+      { $group: { _id: '$sender_id', other: { $first: '$$ROOT' } } },
+      {
+        $replaceRoot: {
+          newRoot: '$other',
+        },
+      },
+      { $skip: parseInt(skip) },
+      { $limit: parseInt(limit) },
+    ]);
+    const data = [];
+    for (const val of list) {
+      if (val.receiver_id === get(user, '_id')) {
+        const userData = await this.uModel.findById(val.sender_id).lean();
+        data.push({
+          user: val.sender_id,
+          name: get(userData, 'nick_name'),
+          send_time: val.send_time,
+          content: val.content,
+          type: val.type,
+        });
+      }
+    }
+    return data;
+  }
+  // 聊天记录
+  async query(filter) {
+    const userInfo = this.ctx.user;
+    const { skip = 0, limit = 0, user, ...condition } = filter;
+    const list = await this.model
+      .find({
+        ...condition,
+        ...{ $or: [{ $and: [{ sender_id: user }, { receiver_id: get(userInfo, '_id') }] }, { $and: [{ sender_id: get(userInfo, '_id') }, { receiver_id: user }] }] },
+      })
+      .skip(skip)
+      .limit(limit)
+      .lean();
+    const data = [];
+    for (const val of list) {
+      const sender = await this.uModel.findById(val.sender_id).lean();
+      const receiver = await this.uModel.findById(val.receiver_id).lean();
+      data.push({
+        sender_name: get(sender, 'nick_name'),
+        receiver_name: get(receiver, 'nick_name'),
+        ...val,
+      });
+    }
+    return data;
+  }
+}