forked from nsjcy/frontEnd/nsjcy

liyj
2020-02-03 7a4d973140ee0168f509098b52745c8a7947ca74
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const webim = require('../../../utils/webim_wx');
 
module.exports = {
  initData(userData, groupData = {}) {
    this.userData = userData || {};
    this.userData['accountType'] = 1;
 
    this.groupData = groupData || {};
    this.groupData['sessionType'] = webim.SESSION_TYPE.GROUP;
    this.selSess = null; // 当前会话
    this.selSessHeadUrl = null; // 当前会话头像
  },
 
  /**
   * 初始化登录IM的监听函数
   * @param {Object} loginListeners 
   */
  initLoginListeners(loginListeners) {
    this.loginListeners = loginListeners;
  },
 
  /**
   * 登录IM
   * @param {Function} success 
   * @param {Function} fail 
   */
  loginIm(success, fail) {
    webim.login(this.userData, this.loginListeners, {
      isAccessFormalEnv: true,
      isLogOn: false
    }, success, fail);
  },
 
  /**
   * 注销IM
   */
  logout() {
    webim.logout();
  },
 
  /**
   * 创建群组
   * @param {*} groupId 群组ID
   * @param {*} userID 用户ID
   * @param {*} succ 成功回调
   * @param {*} fail 失败回调
   */
  createGroup(groupId, userID, succ, fail) {
    var options = {
      'GroupId': String(groupId),
      'Owner_Account': String(userID),
      'Type': "AVChatRoom", //Private/Public/ChatRoom/AVChatRoom
      'ApplyJoinOption': 'FreeAccess',
      'Name': String(groupId),
      'Notification': "",
      'Introduction': "",
      'MemberList': [],
    };
 
    webim.createGroup(
      options,
      function (resp) {
        if (succ) succ();
      },
      function (err) {
        if (err.ErrorCode == 10025 || err.ErrorCode == 10021) {
          if (succ) succ();
        } else {
          if (fail) fail(err);
        }
      }
    );
  },
 
  /**
   * 加入群组
   * @param {*} groupId 群组ID
   * @param {*} succ 成功回调
   * @param {*} fail 失败回调
   */
  joinGroup(groupId, succ, fail) {
    var self = this;
    this.selSess = null;
    // 先创建群,成功后加入群
    this.createGroup(groupId, this.userData.identifier, () => {
      webim.applyJoinBigGroup({
          GroupId: String(groupId)
        },
        function (resp) {
          //JoinedSuccess:加入成功; WaitAdminApproval:等待管理员审批
          if (resp.JoinedStatus && resp.JoinedStatus == 'JoinedSuccess') {
            self.groupData['groupId'] = groupId;
            succ && succ(resp);
          } else {
            fail && fail(resp);
          }
        },
        function (err) {
          if (err.ErrorCode == 10013) { // 被邀请加入的用户已经是群成员,也表示成功
            self.groupData['groupId'] = groupId;
            console.warn('applyJoinGroupSucc', groupId)
            return;
          }
          if (fail) {
            fail(err);
          }
        }
      );
    }, fail);
  },
 
  /**
   * 发送C2C文本消息
   * @param {string} msg 
   * @param {function} succ 
   * @param {function} fail
   */
  sendC2CTextMsg(receiveUser, msg, succ, fail) {
    this.sendTextMessage(webim.SESSION_TYPE.C2C, receiveUser, msg, succ, fail);
  },
 
  /**
   * 发送C2C自定义消息
   * @param {object} msgObj {data: 'xxx', desc: 'xxxx', ext: 'xxxx'}
   * @param {function} succ
   * @param {function} fail
   */
  sendC2CCustomMsg(toUser, msgObj, succ, fail) {
    this.sendCustomMsg(webim.SESSION_TYPE.C2C, toUser, msgObj, succ, fail);
  },
 
 
  /**
   * 发送群组文本消息
   * @param {string} msg 
   * @param {function} succ 
   * @param {function} fail
   */
  sendGroupTextMsg(msg, succ, fail) {
    this.sendTextMessage(webim.SESSION_TYPE.GROUP, null, msg, succ, fail);
  },
 
  /**
   * 发送群组自定义消息
   * @param {object} msgObj {data: 'xxx', desc: 'xxxx', ext: 'xxxx'}
   * @param {function} succ
   * @param {function} fail
   */
  sendGroupCustomMsg(msgObj, succ, fail) {
    this.sendCustomMsg(webim.SESSION_TYPE.GROUP, null, msgObj, succ, fail);
  },
 
  /**
   * 发送普通文本消息
   * @param {*} selType 接收方类型(个人/群组)
   * @param {*} msgText 消息内容
   * @param {*} toUser 接收方ID
   */
  sendTextMessage(selType, toUser, msgText, succ, fail) {
    var maxLen, errInfo;
    if (selType == webim.SESSION_TYPE.C2C) {
      if (!toUser) {
        fail && fail(-1, '没有接收人');
        return;
      }
      maxLen = webim.MSG_MAX_LENGTH.C2C;
      errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
    } else {
      maxLen = webim.MSG_MAX_LENGTH.GROUP;
      errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
    }
 
    if (msgText.length < 1) {
      fail && fail(-2, '不能发送空消息');
      return;
    }
    var msgLen = webim.Tool.getStrBytes(msgText);
 
    if (msgLen > maxLen) {
      fail && fail(-3, errInfo);
      return;
    }
 
    var selSess = null;
    var subType; //消息子类型
 
    // 如果是发给群组
    if (selType == webim.SESSION_TYPE.GROUP) {
      var groupId = this.groupData['groupId'];
      selSess = new webim.Session(webim.SESSION_TYPE.GROUP, groupId, groupId);
      subType = webim.GROUP_MSG_SUB_TYPE.COMMON;
    } else {
      subType = webim.C2C_MSG_SUB_TYPE.COMMON;
      selSess = new webim.Session(selType, toUser, toUser, '', this.getUnixTimestamp());
    }
 
    var isSend = true; //是否为自己发送
    var seq = -1; //消息序列,-1表示 SDK 自动生成,用于去重
    var random = Math.round(Math.random() * 4294967296); //消息随机数,用于去重
    var msgTime = this.getUnixTimestamp(); //消息时间戳
    var msg = new webim.Msg(selSess, isSend, seq, random, msgTime, this.userData.identifier, subType, this.userData.identifierNick);
    var text_obj, face_obj, tmsg, emotionIndex, emotion, restMsgIndex;
 
    //解析文本和表情
    var expr = /\[[^[\]]{1,3}\]/mg;
    var emotions = msgText.match(expr);
    if (!emotions || emotions.length < 1) {
      text_obj = new webim.Msg.Elem.Text(msgText);
      msg.addText(text_obj);
    } else {
      for (var i = 0; i < emotions.length; i++) {
        tmsg = msgText.substring(0, msgText.indexOf(emotions[i]));
        if (tmsg) {
          text_obj = new webim.Msg.Elem.Text(tmsg);
          msg.addText(text_obj);
        }
        emotionIndex = webim.EmotionDataIndexs[emotions[i]];
        emotion = webim.Emotions[emotionIndex];
        if (emotion) {
          face_obj = new webim.Msg.Elem.Face(emotionIndex, emotions[i]);
          msg.addFace(face_obj);
        } else {
          text_obj = new webim.Msg.Elem.Text(emotions[i]);
          msg.addText(text_obj);
        }
        restMsgIndex = msgText.indexOf(emotions[i]) + emotions[i].length;
        msgText = msgText.substring(restMsgIndex);
      }
      if (msgText) {
        text_obj = new webim.Msg.Elem.Text(msgText);
        msg.addText(text_obj);
      }
    }
 
    webim.sendMsg(msg, function (resp) {
      succ && succ(msg);
    }, function (err) {
      fail && fail(-4, err);
    });
  },
 
  /**
   * 发送自定义消息
   * @param {*} selType 接收方类型(个人/群组)
   * @param {*} msgObj 消息内容
   * @param {*} toUser 接收方ID
   */
  sendCustomMsg(selType, toUser, msgObj, succ, fail) {
    var maxLen, errInfo;
    if (selType == webim.SESSION_TYPE.C2C) {
      if (!toUser) {
        event.fire(this, Constant.EVENT.IM.SEND_CHAT_MSG_EMPTY_RECEIVE_ERROR, JSON.stringify(msgObj));
        fail && fail(-1, '没有接收人');
        return;
      }
      maxLen = webim.MSG_MAX_LENGTH.C2C;
      errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
    } else {
      maxLen = webim.MSG_MAX_LENGTH.GROUP;
      errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
    }
 
    var data = msgObj.data + '';
    var desc = msgObj.desc;
    var ext = msgObj.ext;
 
    var msgLen = webim.Tool.getStrBytes(data);
    if (data.length < 1) {
      fail && fail(-2, '不能发送空消息');
      return;
    }
 
    if (msgLen > maxLen) {
      fail && fail(-3, errInfo);
      return;
    }
 
    var selSess = null;
    var subType; //消息子类型
 
    // 如果是发给群组
    if (selType == webim.SESSION_TYPE.GROUP) {
      var groupId = this.groupData['groupId'];
      selSess = new webim.Session(webim.SESSION_TYPE.GROUP, groupId, groupId);
      subType = webim.GROUP_MSG_SUB_TYPE.COMMON;
    } else {
      selSess = new webim.Session(selType, toUser, toUser, '', this.getUnixTimestamp());
      subType = webim.C2C_MSG_SUB_TYPE.COMMON;
    }
 
    var isSend = true; //是否为自己发送
    var seq = -1; //消息序列,-1表示 SDK 自动生成,用于去重
    var random = Math.round(Math.random() * 4294967296); //消息随机数,用于去重
    var msgTime = this.getUnixTimestamp(); //消息时间戳
 
    var msg = new webim.Msg(selSess, isSend, seq, random, msgTime, this.userData.identifier, subType, this.userData.identifierNick);
 
    var custom_obj = new webim.Msg.Elem.Custom(data, desc, ext);
    msg.addCustom(custom_obj);
    //调用发送消息接口
    webim.sendMsg(msg, function (resp) {
      // if (selType == webim.SESSION_TYPE.C2C) { //私聊时,在聊天窗口手动添加一条发的消息,群聊时,长轮询接口会返回自己发的消息
      //   succ && succ(msg);
      // }
      succ && succ(msg);
    }, function (err) {
      fail && fail(-4, err);
    });
  },
 
  /**
   * 获取unixTimestamp时间戳
   */
  getUnixTimestamp() {
    return Math.round(new Date().getTime() / 1000);
  },
 
  /**
   * 组织自定义消息体
   * @param {*} msg 要发送的消息
   * @param {*} succ 
   */
  formatCustomMsg(msg) {
    // custom消息
    var data = msg.data || '';
    var desc = msg.desc || '';
    var ext = msg.ext || '';
 
    if (!this.selSess) {
      this.selSess = new webim.Session(this.groupData.sessionType, this.groupData.groupId, this.groupData.groupId, this.selSessHeadUrl, Math.round(new Date().getTime() / 1000));
    }
 
    var isSend = true; //是否为自己发送
    var seq = -1; //消息序列,-1表示sdk自动生成,用于去重
    var random = Math.round(Math.random() * 4294967296); //消息随机数,用于去重
    var msgTime = Math.round(new Date().getTime() / 1000); //消息时间戳
    var subType; //消息子类型
    if (this.groupData.sessionType == webim.SESSION_TYPE.GROUP) {
      //群消息子类型如下:
      //webim.GROUP_MSG_SUB_TYPE.COMMON-普通消息,
      //webim.GROUP_MSG_SUB_TYPE.LOVEMSG-点赞消息,优先级最低
      //webim.GROUP_MSG_SUB_TYPE.TIP-提示消息(不支持发送,用于区分群消息子类型),
      //webim.GROUP_MSG_SUB_TYPE.REDPACKET-红包消息,优先级最高
      subType = webim.GROUP_MSG_SUB_TYPE.COMMON;
    } else {
      //C2C消息子类型如下:
      //webim.C2C_MSG_SUB_TYPE.COMMON-普通消息,
      subType = webim.C2C_MSG_SUB_TYPE.COMMON;
    }
    var msg = new webim.Msg(this.selSess, isSend, seq, random, msgTime, this.userData.identifier, subType, this.userData.identifierNick);
 
    var custom_obj = new webim.Msg.Elem.Custom(data, desc, ext);
    msg.addCustom(custom_obj);
    return msg;
  },
 
  /**
   * G
   * @param {*} toUserID 
   * @param {*} msg 
   */
  formatC2CCustomMsg(toUserID, msg) {
    // custom消息
    var data = msg.data || '';
    var desc = msg.desc || '';
    var ext = msg.ext || '';
 
    var msgLen = webim.Tool.getStrBytes(data);
 
    var session = new webim.Session(webim.SESSION_TYPE.C2C, toUserID, toUserID, '', Math.round(new Date().getTime() / 1000));
    var isSend = true; //是否为自己发送
    var seq = -1; //消息序列,-1表示sdk自动生成,用于去重
    var random = Math.round(Math.random() * 4294967296); //消息随机数,用于去重
    var msgTime = Math.round(new Date().getTime() / 1000); //消息时间戳
    var subType = webim.C2C_MSG_SUB_TYPE.COMMON; //消息子类型 
    var msg = new webim.Msg(session, isSend, seq, random, msgTime, this.userData.identifier, subType, this.userData.identifierNick);
 
    var custom_obj = new webim.Msg.Elem.Custom(data, desc, ext);
    msg.addCustom(custom_obj);
    return msg;
  }
}