wangzhibo
6 天以前 3f249e399659dcd09d3fe58071b146e50e45c17f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { App, Inject, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { CsMsgEntity } from '../entity/msg';
import { CsSessionEntity } from '../entity/session';
import { CsConnService } from './conn';
import { Application as SocketApplication } from '@midwayjs/socketio';
import * as _ from 'lodash';
import { UserInfoEntity } from '../../user/entity/info';
import { BaseSysUserEntity } from '../../base/entity/sys/user';
 
/**
 * 消息
 */
@Provide()
export class CsMsgService extends BaseService {
  @InjectEntityModel(CsMsgEntity)
  csMsgEntity: Repository<CsMsgEntity>;
 
  @InjectEntityModel(CsSessionEntity)
  csSessionEntity: Repository<CsSessionEntity>;
 
  @InjectEntityModel(UserInfoEntity)
  userInfoEntity: Repository<UserInfoEntity>;
 
  @InjectEntityModel(BaseSysUserEntity)
  baseSysUserEntity: Repository<BaseSysUserEntity>;
 
  @Inject()
  csConnService: CsConnService;
 
  @App('socketIO')
  socketApp: SocketApplication;
 
  @Inject()
  ctx;
 
  /**
   * 消息标记为已读
   * @param msgIds
   */
  async read(msgIds: number[]) {
    await this.csMsgEntity.update({ id: In(msgIds) }, { status: 1 });
  }
 
  /**
   * 未读消息数
   * @param userId
   * @param type
   */
  async unreadCount(userId: number, type: number) {
    if (userId) {
      return this.csMsgEntity.countBy({ userId, type, status: 0 });
    }
    return this.csMsgEntity.countBy({ type, status: 0 });
  }
 
  /**
   * 更新客户未读消息数
   * @param sessionId
   */
  async updateAdminUnreadCount(sessionId: number) {
    const count = await this.csMsgEntity.countBy({
      sessionId,
      type: 0,
      status: 0,
    });
    await this.csSessionEntity.update(sessionId, { adminUnreadCount: count });
  }
 
  /**
   * 分页查询
   * @param query
   * @param option
   * @param connectionName
   */
  async page(query: any, option: any, connectionName?: any) {
    const { sessionId } = query;
    const result = await super.page(query, option, connectionName);
    // 消息置为已读
    const type = this.ctx.user?.id ? 1 : 0;
    await this.csMsgEntity.update({ sessionId, type }, { status: 1 });
    // 更新未读消息数
    await this.updateAdminUnreadCount(sessionId);
 
    return result;
  }
 
  /**
   * 消息发送
   * @param msg
   */
  async send(msg: any, isAdmin: boolean) {
    let session = await this.csSessionEntity.findOneBy({ id: msg.sessionId });
    if (isAdmin) {
      msg.type = 1;
    } else {
      msg.type = 0;
      if (!session) {
        session = new CsSessionEntity();
        session.userId = msg.userId;
        await this.csSessionEntity.insert(session);
      }
    }
    await this.csMsgEntity.insert(msg);
    // 更新未读消息数
    await this.updateAdminUnreadCount(msg.sessionId);
    // 更新最后一条消息
    await this.csSessionEntity.update(session.id, { lastMsg: msg });
 
    // 完善消息内容
    if (msg.type == 0) {
      const user = await this.userInfoEntity.findOneBy({ id: msg.userId });
      msg.user = {
        userId: user.id,
        nickName: user.nickName,
        avatarUrl: user.avatarUrl,
      };
    }
    if (msg.type == 1) {
      const user = await this.baseSysUserEntity.findOneBy({ id: msg.userId });
      msg.user = {
        userId: user.id,
        nickName: user.name,
        avatarUrl: user.headImg,
      };
    }
    // 发送给客户端
    if (isAdmin) {
      // 获得连接ID
      const connId = await this.csConnService.getConnId(false, msg.sessionId);
      // 发送消息
      this.socketApp.of('/cs').to(connId).emit('msg', msg);
    } else {
      // 广播给所有的后端客服
      this.socketApp.of('/cs').emit('msg', msg);
    }
  }
}