|
@@ -5,6 +5,10 @@ import { BaseService, PageOptions, SearchBase } from 'free-midway-component';
|
|
|
import { Chat } from '../entity/chat.entity';
|
|
|
import { cloneDeep } from 'lodash';
|
|
|
import { ChatMqService } from './chatMq.service';
|
|
|
+import { Types } from 'mongoose';
|
|
|
+import { JwtService } from '@midwayjs/jwt';
|
|
|
+const assert = require('assert');
|
|
|
+const ObjectId = Types.ObjectId;
|
|
|
type modelType = ReturnModelType<typeof Chat>;
|
|
|
@Provide()
|
|
|
export class ChatService extends BaseService<modelType> {
|
|
@@ -13,6 +17,22 @@ export class ChatService extends BaseService<modelType> {
|
|
|
@Inject()
|
|
|
chatMqService: ChatMqService;
|
|
|
|
|
|
+ @Inject()
|
|
|
+ jwtService: JwtService;
|
|
|
+
|
|
|
+ // 处理已读问题
|
|
|
+ async allRead(data) {
|
|
|
+ const token = get(this.ctx, 'request.header.token');
|
|
|
+ assert(token, '缺少token信息');
|
|
|
+ const tokenInfo = await this.jwtService.decodeSync(token);
|
|
|
+ const info = { ...data, not_read: 1, speaker: { $ne: get(tokenInfo, '_id') } };
|
|
|
+ let result;
|
|
|
+ if (get(tokenInfo, 'role') === 'Patient') result = await this.model.find({ ...info }).lean();
|
|
|
+ else result = await this.model.find({ ...info, speaker_type: 'Patient' }).lean();
|
|
|
+ const ids = result.map(i => new ObjectId(i._id).toString());
|
|
|
+ await this.model.updateMany({ _id: ids }, { $set: { not_read: 0 } });
|
|
|
+ }
|
|
|
+
|
|
|
async sendMq(data) {
|
|
|
// 队列: /${群组id}/${患者id}
|
|
|
const { group, patient, _id } = data;
|