xs-common-plugins 1.2.1 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +298 -294
- package/common.js +110 -110
- package/index.js +1 -1
- package/package.json +16 -16
- package/src/common/common.js +548 -548
- package/src/components/CheckBox_Cmp/index.vue +62 -62
- package/src/components/FormItem/index.vue +92 -92
- package/src/components/ReportCmp/index.vue +76 -76
- package/src/components/Search/index.scss +219 -219
- package/src/components/Search/index.vue +410 -410
- package/src/components/Search/product_option/index.scss +1 -1
- package/src/components/Search_filter/index.scss +104 -104
- package/src/components/TableItem/TableItem.vue +55 -55
- package/src/components/TextOVer/index.vue +55 -55
- package/src/components/UploadImg/index.vue +177 -177
- package/src/components/im/index.vue +155 -155
- package/src/components/im/pages/chatList/index.vue +45 -45
- package/src/components/im/pages/chatRoom/index.vue +159 -159
- package/src/components/xsSelect/index.vue +125 -125
- package/src/mixin/keepAlive.js +53 -0
- package/src/plugins/im/components/chat/index.scss +163 -163
- package/src/plugins/im/components/chat/index.vue +144 -144
- package/src/plugins/im/components/chat/methods.js +149 -149
- package/src/plugins/im/components/msg-image/index.vue +40 -40
- package/src/plugins/im/components/send-msg/index.scss +164 -164
- package/src/plugins/im/components/send-msg/index.vue +107 -107
- package/src/plugins/im/components/send-msg/methods.js +125 -125
- package/src/plugins/im/components/template-message/index.vue +76 -76
- package/src/plugins/im/components/without.vue +19 -19
- package/src/plugins/im/index.js +31 -31
- package/src/plugins/im/utils/services.js +625 -625
- package/src/plugins/index.js +60 -60
- package/src/plugins/row-col-cmp/index.js +20 -20
- package/src/router/permission.js +126 -126
- package/src/store/modules/dic.js +74 -74
- package/src/store/modules/oss.js +40 -40
- package/src/styles/index.scss +91 -91
- package/src/styles/table.scss +90 -90
- package/src/utils/api.js +54 -54
- package/src/utils/auth.js +38 -38
- package/src/utils/concat_batch_btns.js +88 -88
- package/src/utils/enum.js +150 -150
- package/src/utils/filter.js +5 -5
- package/src/utils/filterRules.js +55 -55
- package/src/utils/getMenu.js +82 -82
- package/src/utils/global_directive.js +10 -0
- package/src/utils/ossService.js +55 -55
- package/src/utils/prototype.js +46 -46
- package/src/utils/search.js +33 -33
- package/src/utils/signalR.js +24 -24
- package/src/views/callback/index.vue +35 -35
- package/src/views/home/index.vue +25 -25
- package/src/views/layout/components/AppMain.vue +21 -5
- package/src/views/layout/components/Navbar.vue +20 -13
- package/src/views/layout/components/TagsView/index.vue +130 -91
- package/src/views/slienceAuth/index.vue +42 -42
|
@@ -1,625 +1,625 @@
|
|
|
1
|
-
// const RongIMLib = require('@/common/RongIMLib-3.0.7-dev.js');
|
|
2
|
-
const RongIMLib = require('../lib/RongIMLib-3.0.7.2-dev.js');
|
|
3
|
-
const utils = require('./utils.js');
|
|
4
|
-
// UserList, GroupList, MusicList
|
|
5
|
-
const UserList = [];
|
|
6
|
-
const GroupList = [];
|
|
7
|
-
const MusicList = [];
|
|
8
|
-
|
|
9
|
-
let imInstance = null;
|
|
10
|
-
let currentUser = null;
|
|
11
|
-
|
|
12
|
-
let conversationList = [];
|
|
13
|
-
|
|
14
|
-
let config = {
|
|
15
|
-
appkey: '',
|
|
16
|
-
url: '',
|
|
17
|
-
wsScheme: 'wss://',
|
|
18
|
-
protocol: 'https://'
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// let registerMessages = () => {
|
|
23
|
-
// let messageName = "MusicMessage";
|
|
24
|
-
// let objectName = "seal:music";
|
|
25
|
-
// let mesasgeTag = new RongIMLib.MessageTag(true, true);
|
|
26
|
-
// let prototypes = ["url", "name", "author", "poster"];
|
|
27
|
-
// RongIMClient.registerMessageType(messageName, objectName, mesasgeTag, prototypes);
|
|
28
|
-
// };
|
|
29
|
-
|
|
30
|
-
let ErrorInfo = {
|
|
31
|
-
4: {
|
|
32
|
-
code: 4,
|
|
33
|
-
msg: 'Appkey、Token 不匹配'
|
|
34
|
-
},
|
|
35
|
-
3: {
|
|
36
|
-
code: 3,
|
|
37
|
-
msg: '网络不可用'
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
function ObserverList() {
|
|
41
|
-
var checkIndexOutBound = function (index, bound) {
|
|
42
|
-
return index > -1 && index < bound;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
this.observerList = [];
|
|
46
|
-
|
|
47
|
-
this.add = function (observer, force) {
|
|
48
|
-
if (force) {
|
|
49
|
-
this.observerList.length = 0;
|
|
50
|
-
}
|
|
51
|
-
this.observerList.push(observer);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
this.get = function (index) {
|
|
55
|
-
if (checkIndexOutBound(index, this.observerList.length)) {
|
|
56
|
-
return this.observerList[index];
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
this.count = function () {
|
|
61
|
-
return this.observerList.length;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
this.removeAt = function (index) {
|
|
65
|
-
checkIndexOutBound(index, this.observerList.length) && this.observerList.splice(index, 1);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
this.remove = function (observer) {
|
|
69
|
-
if (!observer) {
|
|
70
|
-
this.observerList.length = 0;
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
var observerList = Object.prototype.toString.call(observer) === '[object Function]' ? [observer] : observer;
|
|
74
|
-
for (var i = 0, len = this.observerList.length; i < len; i++) {
|
|
75
|
-
for (var j = 0; j < observerList.length; j++) {
|
|
76
|
-
if (this.observerList[i] === observerList[j]) {
|
|
77
|
-
this.removeAt(i);
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
// if (this.observerList[i] === observer[i]) {
|
|
82
|
-
// this.removeAt(i);
|
|
83
|
-
// break;
|
|
84
|
-
// }
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
this.notify = function (val) {
|
|
89
|
-
for (var i = 0, len = this.observerList.length; i < len; i++) {
|
|
90
|
-
this.observerList[i](val);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
this.indexOf = function (observer, startIndex) {
|
|
95
|
-
var i = startIndex || 0,
|
|
96
|
-
len = this.observerList.length;
|
|
97
|
-
while (i < len) {
|
|
98
|
-
if (this.observerList[i] === observer) {
|
|
99
|
-
return i;
|
|
100
|
-
}
|
|
101
|
-
i++;
|
|
102
|
-
}
|
|
103
|
-
return -1;
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
let Friend = {};
|
|
108
|
-
|
|
109
|
-
Friend.getList = () => {
|
|
110
|
-
return Promise.resolve(utils.filter(UserList, (friend) => {
|
|
111
|
-
return (friend.id != currentUser.id);
|
|
112
|
-
}));
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
let User = {};
|
|
116
|
-
|
|
117
|
-
let getUserIndex = (name, max) => {
|
|
118
|
-
var index = utils.toUnicode(name).slice(-1);
|
|
119
|
-
//转 unicode 后最后一个字符不是数字,给固定🈯值
|
|
120
|
-
if (isNaN(index)) {
|
|
121
|
-
index = max;
|
|
122
|
-
}
|
|
123
|
-
if (index > max) {
|
|
124
|
-
index = max;
|
|
125
|
-
}
|
|
126
|
-
return index;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
let getUser = (user) => {
|
|
130
|
-
user = utils.rename(user, { avatarUrl: 'avatar', nickName: 'name' });
|
|
131
|
-
let maxIndex = UserList.length - 1;
|
|
132
|
-
let index = getUserIndex(user.name, maxIndex);
|
|
133
|
-
let _user = utils.extend(UserList[index], {
|
|
134
|
-
token: user.token
|
|
135
|
-
});
|
|
136
|
-
// let _user = UserList[index]
|
|
137
|
-
return _user
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
User.getToken = (user) => {
|
|
141
|
-
currentUser = getUser(user);
|
|
142
|
-
return Promise.resolve(currentUser);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
User.joinChatroom = (id) => {
|
|
146
|
-
const chatRoom = imInstance.ChatRoom.get({ id });
|
|
147
|
-
return chatRoom.join({ count: -1 });
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
let bindSender = (message, position) => {
|
|
151
|
-
if (!utils.isArray(message)) {
|
|
152
|
-
message = [message];
|
|
153
|
-
}
|
|
154
|
-
let getSender = {
|
|
155
|
-
1: (msg) => {
|
|
156
|
-
msg.sender = {
|
|
157
|
-
name: msg.senderUserId,
|
|
158
|
-
type: msg.type,
|
|
159
|
-
avatar1: require('../../../../static/im/icon1.png'),
|
|
160
|
-
avatar2: require('../../../../static/im/icon2.png'),
|
|
161
|
-
avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-user-unkown.png'
|
|
162
|
-
};
|
|
163
|
-
},
|
|
164
|
-
3: (msg) => {
|
|
165
|
-
msg.sender = {
|
|
166
|
-
name: msg.senderUserId,
|
|
167
|
-
type: msg.type,
|
|
168
|
-
// avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-group-unkown.png'
|
|
169
|
-
avatar: require('../../../../static/im/icon1.png')
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
utils.map(message, (msg) => {
|
|
174
|
-
const { type } = msg;
|
|
175
|
-
msg.pos = position;
|
|
176
|
-
const event = getSender[type] || getSender[1];
|
|
177
|
-
event(msg);
|
|
178
|
-
utils.formatMessage(msg);
|
|
179
|
-
return msg;
|
|
180
|
-
});
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
let Message = {
|
|
184
|
-
watcher: new ObserverList(),
|
|
185
|
-
_push: (message) => {
|
|
186
|
-
// console.log('received msg', message);
|
|
187
|
-
//不处理离线消息
|
|
188
|
-
if (message.isOffLineMessage) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
bindSender(message);
|
|
192
|
-
Message.watcher.notify(message);
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
let sendMessage = (type, targetId, message) => {
|
|
198
|
-
let bindUser = (_msg, next) => {
|
|
199
|
-
bindSender(_msg);
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
// let { name, avatar } = currentUser;
|
|
203
|
-
let { name, avatar } = { name: 'aaa', avatar: 'bbb' };
|
|
204
|
-
let user = {
|
|
205
|
-
name,
|
|
206
|
-
avatar
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
let messageMap = {
|
|
210
|
-
text: (params) => {
|
|
211
|
-
params.messageType = 'RC:TxtMsg';
|
|
212
|
-
return params;
|
|
213
|
-
},
|
|
214
|
-
image: (params) => {
|
|
215
|
-
params.messageType = 'RC:ImgMsg';
|
|
216
|
-
return params;
|
|
217
|
-
},
|
|
218
|
-
voice: (params) => {
|
|
219
|
-
params.messageType = 'RC:HQVCMsg';
|
|
220
|
-
return params;
|
|
221
|
-
},
|
|
222
|
-
music: (params) => {
|
|
223
|
-
params.messageType = 'seal:music';
|
|
224
|
-
return params;
|
|
225
|
-
},
|
|
226
|
-
file: (params) => {
|
|
227
|
-
params.messageType = 'RC:FileMsg';
|
|
228
|
-
return params;
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
message.user = user;
|
|
233
|
-
let params = {
|
|
234
|
-
content: message
|
|
235
|
-
};
|
|
236
|
-
params = messageMap[message.type](params);
|
|
237
|
-
let conversation = imInstance.Conversation.get({
|
|
238
|
-
type: +type,
|
|
239
|
-
targetId
|
|
240
|
-
});
|
|
241
|
-
return conversation.send(params).then((message) => {
|
|
242
|
-
bindUser(message);
|
|
243
|
-
return message;
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
//params.type
|
|
248
|
-
//params.targetId
|
|
249
|
-
//params.content
|
|
250
|
-
Message.sendText = (params) => {
|
|
251
|
-
let { type, targetId, content, userInfo } = params;
|
|
252
|
-
return sendMessage(type, targetId, {
|
|
253
|
-
type: 'text',
|
|
254
|
-
userInfo,
|
|
255
|
-
content
|
|
256
|
-
})
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
//params.type
|
|
260
|
-
//params.targetId
|
|
261
|
-
//params.content
|
|
262
|
-
//params.iamgeUri
|
|
263
|
-
Message.sendImage = (params) => {
|
|
264
|
-
let { type, targetId, content, imageUri, extra, userInfo } = params;
|
|
265
|
-
return sendMessage(type, targetId, {
|
|
266
|
-
type: 'image',
|
|
267
|
-
content,
|
|
268
|
-
imageUri,
|
|
269
|
-
extra,
|
|
270
|
-
userInfo
|
|
271
|
-
})
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
//params.type
|
|
275
|
-
//params.targetId
|
|
276
|
-
//params.name
|
|
277
|
-
//params.size
|
|
278
|
-
//params.type
|
|
279
|
-
//params.fileUrl
|
|
280
|
-
Message.sendFile = (params) => {
|
|
281
|
-
let { type, targetId, name, size, fileUrl } = params;
|
|
282
|
-
return sendMessage(type, targetId, {
|
|
283
|
-
type: 'file',
|
|
284
|
-
name,
|
|
285
|
-
size,
|
|
286
|
-
fileUrl,
|
|
287
|
-
})
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
//params.type
|
|
291
|
-
//params.targetId
|
|
292
|
-
Message.sendVoice = (params) => {
|
|
293
|
-
let { type, targetId, content } = params;
|
|
294
|
-
let data = utils.extend({ type: 'voice' }, content);
|
|
295
|
-
return sendMessage(type, targetId, data)
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
let getMusic = () => {
|
|
299
|
-
let len = MusicList.length;
|
|
300
|
-
let index = Math.floor(Math.random() * len);
|
|
301
|
-
return MusicList[index];
|
|
302
|
-
};
|
|
303
|
-
Message.sendMusic = (params) => {
|
|
304
|
-
let { type, targetId } = params;
|
|
305
|
-
let content = utils.extend({ type: 'music' }, getMusic());
|
|
306
|
-
return sendMessage(type, targetId, content);
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
// params.type
|
|
310
|
-
// params.targetId
|
|
311
|
-
// params.position 0/1
|
|
312
|
-
Message.getList = (params) => {
|
|
313
|
-
let { type, targetId, position, count } = params;
|
|
314
|
-
count = count || 5;
|
|
315
|
-
let timestamp = position;
|
|
316
|
-
let conversation = imInstance.Conversation.get({
|
|
317
|
-
type: +type,
|
|
318
|
-
targetId
|
|
319
|
-
});
|
|
320
|
-
return conversation.getMessages({
|
|
321
|
-
timestrap: timestamp,
|
|
322
|
-
count
|
|
323
|
-
}).then(({ list, hasMore }) => {
|
|
324
|
-
let messageList = list;
|
|
325
|
-
bindSender(messageList, position);
|
|
326
|
-
hasMore = !!hasMore;
|
|
327
|
-
return {
|
|
328
|
-
messageList,
|
|
329
|
-
hasMore
|
|
330
|
-
};
|
|
331
|
-
})
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
Message.getChatRoomMessageList = (params) => {
|
|
335
|
-
let { type, targetId, position, count } = params;
|
|
336
|
-
count = count || 5;
|
|
337
|
-
let timestamp = position;
|
|
338
|
-
let chatRoom = imInstance.ChatRoom.get({
|
|
339
|
-
id: targetId
|
|
340
|
-
});
|
|
341
|
-
return chatRoom.getMessages({
|
|
342
|
-
timestrap: timestamp,
|
|
343
|
-
count
|
|
344
|
-
}).then(({ list, hasMore }) => {
|
|
345
|
-
let messageList = list;
|
|
346
|
-
// let messageList = list.filter((message) => {
|
|
347
|
-
// return message.messageType != 'RC:RcCmd';
|
|
348
|
-
// });
|
|
349
|
-
bindSender(messageList, position);
|
|
350
|
-
hasMore = !!hasMore;
|
|
351
|
-
return {
|
|
352
|
-
messageList,
|
|
353
|
-
hasMore
|
|
354
|
-
};
|
|
355
|
-
});
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
Message.create = (params) => {
|
|
359
|
-
let { name, type, targetId, content } = params;
|
|
360
|
-
let message = {
|
|
361
|
-
messageType: name,
|
|
362
|
-
type,
|
|
363
|
-
targetId: targetId,
|
|
364
|
-
senderUserId: currentUser.id,
|
|
365
|
-
content: content,
|
|
366
|
-
messageDirection: 1,
|
|
367
|
-
messageUId: 'M' + Date.now(),
|
|
368
|
-
sentTime: Date.now()
|
|
369
|
-
};
|
|
370
|
-
bindSender(message);
|
|
371
|
-
return message;
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
Message.watch = (watch) => {
|
|
375
|
-
Message.watcher.add(watch);
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
Message.unwatch = () => {
|
|
379
|
-
Message.watcher.remove();
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
let Conversation = {
|
|
383
|
-
watcher: new ObserverList()
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
Conversation.getList = () => {
|
|
387
|
-
return imInstance.Conversation.getList().then((list) => {
|
|
388
|
-
conversationList = imInstance.Conversation.merge({
|
|
389
|
-
conversationList,
|
|
390
|
-
updatedConversationList: list
|
|
391
|
-
});
|
|
392
|
-
bindUserInfo(conversationList);
|
|
393
|
-
return conversationList;
|
|
394
|
-
});
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
let bindUserInfo = (list) => {
|
|
398
|
-
let unknowUser = {
|
|
399
|
-
name: '火星人',
|
|
400
|
-
// avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-user-unkown.png'
|
|
401
|
-
avatar: require('../../../../static/im/icon2.png'),
|
|
402
|
-
};
|
|
403
|
-
let unknowGroup = {
|
|
404
|
-
name: '火星群组',
|
|
405
|
-
avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-group-unkown.png'
|
|
406
|
-
};
|
|
407
|
-
if (!utils.isArray(list)) {
|
|
408
|
-
list = [list];
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
let infoMap = {
|
|
412
|
-
1: (conversation) => {
|
|
413
|
-
conversation.target = utils.find(UserList, (user) => {
|
|
414
|
-
return user.id == conversation.targetId
|
|
415
|
-
}) || utils.copy(unknowUser);
|
|
416
|
-
},
|
|
417
|
-
2: (conversation) => {
|
|
418
|
-
conversation.target = utils.copy(unknowUser);
|
|
419
|
-
},
|
|
420
|
-
3: (conversation) => {
|
|
421
|
-
conversation.target = utils.find(GroupList, (group) => {
|
|
422
|
-
return group.id == conversation.targetId
|
|
423
|
-
}) || utils.copy(unknowGroup);;
|
|
424
|
-
},
|
|
425
|
-
10: (conversation) => {
|
|
426
|
-
conversation.target = utils.copy(unknowUser);
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
let formatMsg = (msg) => {
|
|
430
|
-
let { messageType } = msg;
|
|
431
|
-
let content = '[此消息类型未解析]';
|
|
432
|
-
if (messageType == 'RC:TxtMsg') {
|
|
433
|
-
if (msg.content) {
|
|
434
|
-
content = msg.content.content;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
if (messageType == 'RC:VcMsg' || messageType == 'RC:HQVCMsg') {
|
|
438
|
-
content = '[语音]';
|
|
439
|
-
}
|
|
440
|
-
if (messageType == 'RC:ImgMsg') {
|
|
441
|
-
content = '[图片]';
|
|
442
|
-
}
|
|
443
|
-
if (messageType == 'RC:FileMsg') {
|
|
444
|
-
content = '[文件]';
|
|
445
|
-
}
|
|
446
|
-
if (messageType == 'seal:music') {
|
|
447
|
-
content = '[音乐]';
|
|
448
|
-
}
|
|
449
|
-
return content;
|
|
450
|
-
};
|
|
451
|
-
list.forEach((conversation, index) => {
|
|
452
|
-
const { latestMessage, unreadMessageCount, type, targetId } = conversation;
|
|
453
|
-
const { sentTime } = latestMessage;
|
|
454
|
-
conversation._sentTime = utils.getTime(sentTime);
|
|
455
|
-
conversation.unReadCount = unreadMessageCount;
|
|
456
|
-
conversation.content = formatMsg(latestMessage);
|
|
457
|
-
let _type = type > 3 ? 10 : type;
|
|
458
|
-
infoMap[_type](conversation);
|
|
459
|
-
// conversation.target.name = targetId + ' (' + type + ')';
|
|
460
|
-
conversation.target.name = targetId;
|
|
461
|
-
list[index] = conversation;
|
|
462
|
-
});
|
|
463
|
-
};
|
|
464
|
-
|
|
465
|
-
Conversation.clearUnreadCount = (conversation) => {
|
|
466
|
-
let { type, targetId } = conversation;
|
|
467
|
-
conversation.type = +conversation.type;
|
|
468
|
-
imInstance.Conversation.get(conversation).read();
|
|
469
|
-
};
|
|
470
|
-
Conversation.watch = (watcher) => {
|
|
471
|
-
imInstance.watch({
|
|
472
|
-
conversation: function (event) {
|
|
473
|
-
const { updatedConversationList } = event;
|
|
474
|
-
conversationList = imInstance.Conversation.merge({
|
|
475
|
-
conversationList,
|
|
476
|
-
updatedConversationList
|
|
477
|
-
});
|
|
478
|
-
bindUserInfo(conversationList);
|
|
479
|
-
watcher(conversationList);
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
};
|
|
483
|
-
Conversation.setStatus = (conversation, options) => {
|
|
484
|
-
const params = {
|
|
485
|
-
targetId: conversation.targetId,
|
|
486
|
-
type: conversation.type
|
|
487
|
-
};
|
|
488
|
-
return imInstance.Conversation.get(params).setStatus(options);
|
|
489
|
-
}
|
|
490
|
-
let Status = {
|
|
491
|
-
watcher: new ObserverList()
|
|
492
|
-
};
|
|
493
|
-
Status.disconnect = () => {
|
|
494
|
-
imInstance.disconnect();
|
|
495
|
-
};
|
|
496
|
-
Status.connect = (user) => {
|
|
497
|
-
currentUser = user;
|
|
498
|
-
imInstance.watch({
|
|
499
|
-
status: function ({ status }) {
|
|
500
|
-
Status.watcher.notify(status);
|
|
501
|
-
},
|
|
502
|
-
message: function ({ message }) {
|
|
503
|
-
let { messageType } = message;
|
|
504
|
-
let messageCtrol = {
|
|
505
|
-
otherMessage: () => {
|
|
506
|
-
Message._push(message);
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
let messageHandler = messageCtrol[messageType] || messageCtrol.otherMessage;
|
|
510
|
-
messageHandler();
|
|
511
|
-
}
|
|
512
|
-
});
|
|
513
|
-
user.token = config.token;
|
|
514
|
-
return imInstance.connect(user).then((result) => {
|
|
515
|
-
currentUser.id = result.id;
|
|
516
|
-
return result;
|
|
517
|
-
}).catch((error) => {
|
|
518
|
-
console.log(error);
|
|
519
|
-
});
|
|
520
|
-
};
|
|
521
|
-
|
|
522
|
-
Status.watch = (watch) => {
|
|
523
|
-
var force = true;
|
|
524
|
-
Status.watcher.add(watch, force);
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
let File = {};
|
|
528
|
-
|
|
529
|
-
const zfbUpload = (fileInfo, token, bosHeaders, bosUrl) => {
|
|
530
|
-
return new Promise((resolve, reject) => {
|
|
531
|
-
const uploadTask = my.uploadFile({
|
|
532
|
-
url: 'https://upload.qiniup.com',
|
|
533
|
-
filePath: fileInfo.path,
|
|
534
|
-
fileType: 'image',
|
|
535
|
-
fileName: 'file',
|
|
536
|
-
header: {
|
|
537
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
538
|
-
},
|
|
539
|
-
formData: {
|
|
540
|
-
token: token
|
|
541
|
-
},
|
|
542
|
-
success: function (res) {
|
|
543
|
-
resolve(res);
|
|
544
|
-
},
|
|
545
|
-
fail: function (err) {
|
|
546
|
-
console.log('upload qiniu failed', err);
|
|
547
|
-
}
|
|
548
|
-
})
|
|
549
|
-
|
|
550
|
-
uploadTask.onProgressUpdate(res => {
|
|
551
|
-
console.log('上传进度', res.progress)
|
|
552
|
-
console.log('已经上传的数据长度', res.totalBytesSent)
|
|
553
|
-
console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
|
|
554
|
-
})
|
|
555
|
-
});
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
File.upload = (fileInfo, uploadType) => {
|
|
561
|
-
let fileType = uploadType || RongIMLib.FILE_TYPE.FILE;
|
|
562
|
-
let fileName = fileInfo.name || 'base64.png';
|
|
563
|
-
console.log('fileType, fileName', fileType, fileName)
|
|
564
|
-
return imInstance.getFileToken(fileType, fileName).then(result => {
|
|
565
|
-
console.log('result', result)
|
|
566
|
-
let { token, bosToken, bosDate, bos, path } = result;
|
|
567
|
-
let bosHeaders = {
|
|
568
|
-
'authorization': bosToken,
|
|
569
|
-
'x-bce-date': bosDate,
|
|
570
|
-
'Content-Type': 'multipart/form-data',
|
|
571
|
-
}
|
|
572
|
-
let bosUrl = bos + path;
|
|
573
|
-
return { fileInfo, token, bosHeaders, bosUrl }
|
|
574
|
-
// return zfbUpload(fileInfo, token, bosHeaders, bosUrl)
|
|
575
|
-
})
|
|
576
|
-
// .then(res => {
|
|
577
|
-
// let qiniuHash, qiniuName;
|
|
578
|
-
// if (!res.isBosRes) {
|
|
579
|
-
// const { data } = res;
|
|
580
|
-
// const { hash, name } = JSON.parse(data);
|
|
581
|
-
// qiniuHash = hash, qiniuName = name;
|
|
582
|
-
// }
|
|
583
|
-
// return imInstance.getFileUrl(fileType, qiniuHash, qiniuName, res);
|
|
584
|
-
// })
|
|
585
|
-
}
|
|
586
|
-
File.FileToken = (fileType) => {
|
|
587
|
-
return imInstance.getFileToken(fileType).then(result => {
|
|
588
|
-
return result
|
|
589
|
-
})
|
|
590
|
-
}
|
|
591
|
-
File.getFileUrl = (fileType, data) => {
|
|
592
|
-
return new Promise((resolve, reject) => {
|
|
593
|
-
imInstance.getFileUrl(fileType, data.hash, data.name, data).then(res => {
|
|
594
|
-
resolve(res)
|
|
595
|
-
})
|
|
596
|
-
.catch((error) => {
|
|
597
|
-
console.log('获取文件 url 失败', error);
|
|
598
|
-
reject();
|
|
599
|
-
});
|
|
600
|
-
})
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
const Init = (_config) => {
|
|
604
|
-
utils.extend(config, _config);
|
|
605
|
-
config.connectType = 'comet';
|
|
606
|
-
config.debug = true;
|
|
607
|
-
imInstance = RongIMLib.init(config);
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
let modules = {
|
|
611
|
-
User,
|
|
612
|
-
Message,
|
|
613
|
-
Conversation,
|
|
614
|
-
Friend,
|
|
615
|
-
Status,
|
|
616
|
-
File,
|
|
617
|
-
ConnectionStatus: RongIMLib.CONNECTION_STATUS,
|
|
618
|
-
CONNECTION_STATUS: RongIMLib.CONNECTION_STATUS,
|
|
619
|
-
Init,
|
|
620
|
-
};
|
|
621
|
-
module.exports = (_config) => {
|
|
622
|
-
// config.customCMP = ['cometqa.cn.ronghub.com:8201'];
|
|
623
|
-
// config.customCMP = ['wsqa0.cn.ronghub.com'];
|
|
624
|
-
return modules;
|
|
625
|
-
};
|
|
1
|
+
// const RongIMLib = require('@/common/RongIMLib-3.0.7-dev.js');
|
|
2
|
+
const RongIMLib = require('../lib/RongIMLib-3.0.7.2-dev.js');
|
|
3
|
+
const utils = require('./utils.js');
|
|
4
|
+
// UserList, GroupList, MusicList
|
|
5
|
+
const UserList = [];
|
|
6
|
+
const GroupList = [];
|
|
7
|
+
const MusicList = [];
|
|
8
|
+
|
|
9
|
+
let imInstance = null;
|
|
10
|
+
let currentUser = null;
|
|
11
|
+
|
|
12
|
+
let conversationList = [];
|
|
13
|
+
|
|
14
|
+
let config = {
|
|
15
|
+
appkey: '',
|
|
16
|
+
url: '',
|
|
17
|
+
wsScheme: 'wss://',
|
|
18
|
+
protocol: 'https://'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
// let registerMessages = () => {
|
|
23
|
+
// let messageName = "MusicMessage";
|
|
24
|
+
// let objectName = "seal:music";
|
|
25
|
+
// let mesasgeTag = new RongIMLib.MessageTag(true, true);
|
|
26
|
+
// let prototypes = ["url", "name", "author", "poster"];
|
|
27
|
+
// RongIMClient.registerMessageType(messageName, objectName, mesasgeTag, prototypes);
|
|
28
|
+
// };
|
|
29
|
+
|
|
30
|
+
let ErrorInfo = {
|
|
31
|
+
4: {
|
|
32
|
+
code: 4,
|
|
33
|
+
msg: 'Appkey、Token 不匹配'
|
|
34
|
+
},
|
|
35
|
+
3: {
|
|
36
|
+
code: 3,
|
|
37
|
+
msg: '网络不可用'
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
function ObserverList() {
|
|
41
|
+
var checkIndexOutBound = function (index, bound) {
|
|
42
|
+
return index > -1 && index < bound;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
this.observerList = [];
|
|
46
|
+
|
|
47
|
+
this.add = function (observer, force) {
|
|
48
|
+
if (force) {
|
|
49
|
+
this.observerList.length = 0;
|
|
50
|
+
}
|
|
51
|
+
this.observerList.push(observer);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.get = function (index) {
|
|
55
|
+
if (checkIndexOutBound(index, this.observerList.length)) {
|
|
56
|
+
return this.observerList[index];
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
this.count = function () {
|
|
61
|
+
return this.observerList.length;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
this.removeAt = function (index) {
|
|
65
|
+
checkIndexOutBound(index, this.observerList.length) && this.observerList.splice(index, 1);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
this.remove = function (observer) {
|
|
69
|
+
if (!observer) {
|
|
70
|
+
this.observerList.length = 0;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
var observerList = Object.prototype.toString.call(observer) === '[object Function]' ? [observer] : observer;
|
|
74
|
+
for (var i = 0, len = this.observerList.length; i < len; i++) {
|
|
75
|
+
for (var j = 0; j < observerList.length; j++) {
|
|
76
|
+
if (this.observerList[i] === observerList[j]) {
|
|
77
|
+
this.removeAt(i);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// if (this.observerList[i] === observer[i]) {
|
|
82
|
+
// this.removeAt(i);
|
|
83
|
+
// break;
|
|
84
|
+
// }
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
this.notify = function (val) {
|
|
89
|
+
for (var i = 0, len = this.observerList.length; i < len; i++) {
|
|
90
|
+
this.observerList[i](val);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
this.indexOf = function (observer, startIndex) {
|
|
95
|
+
var i = startIndex || 0,
|
|
96
|
+
len = this.observerList.length;
|
|
97
|
+
while (i < len) {
|
|
98
|
+
if (this.observerList[i] === observer) {
|
|
99
|
+
return i;
|
|
100
|
+
}
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
return -1;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let Friend = {};
|
|
108
|
+
|
|
109
|
+
Friend.getList = () => {
|
|
110
|
+
return Promise.resolve(utils.filter(UserList, (friend) => {
|
|
111
|
+
return (friend.id != currentUser.id);
|
|
112
|
+
}));
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
let User = {};
|
|
116
|
+
|
|
117
|
+
let getUserIndex = (name, max) => {
|
|
118
|
+
var index = utils.toUnicode(name).slice(-1);
|
|
119
|
+
//转 unicode 后最后一个字符不是数字,给固定🈯值
|
|
120
|
+
if (isNaN(index)) {
|
|
121
|
+
index = max;
|
|
122
|
+
}
|
|
123
|
+
if (index > max) {
|
|
124
|
+
index = max;
|
|
125
|
+
}
|
|
126
|
+
return index;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
let getUser = (user) => {
|
|
130
|
+
user = utils.rename(user, { avatarUrl: 'avatar', nickName: 'name' });
|
|
131
|
+
let maxIndex = UserList.length - 1;
|
|
132
|
+
let index = getUserIndex(user.name, maxIndex);
|
|
133
|
+
let _user = utils.extend(UserList[index], {
|
|
134
|
+
token: user.token
|
|
135
|
+
});
|
|
136
|
+
// let _user = UserList[index]
|
|
137
|
+
return _user
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
User.getToken = (user) => {
|
|
141
|
+
currentUser = getUser(user);
|
|
142
|
+
return Promise.resolve(currentUser);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
User.joinChatroom = (id) => {
|
|
146
|
+
const chatRoom = imInstance.ChatRoom.get({ id });
|
|
147
|
+
return chatRoom.join({ count: -1 });
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
let bindSender = (message, position) => {
|
|
151
|
+
if (!utils.isArray(message)) {
|
|
152
|
+
message = [message];
|
|
153
|
+
}
|
|
154
|
+
let getSender = {
|
|
155
|
+
1: (msg) => {
|
|
156
|
+
msg.sender = {
|
|
157
|
+
name: msg.senderUserId,
|
|
158
|
+
type: msg.type,
|
|
159
|
+
avatar1: require('../../../../static/im/icon1.png'),
|
|
160
|
+
avatar2: require('../../../../static/im/icon2.png'),
|
|
161
|
+
avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-user-unkown.png'
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
3: (msg) => {
|
|
165
|
+
msg.sender = {
|
|
166
|
+
name: msg.senderUserId,
|
|
167
|
+
type: msg.type,
|
|
168
|
+
// avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-group-unkown.png'
|
|
169
|
+
avatar: require('../../../../static/im/icon1.png')
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
utils.map(message, (msg) => {
|
|
174
|
+
const { type } = msg;
|
|
175
|
+
msg.pos = position;
|
|
176
|
+
const event = getSender[type] || getSender[1];
|
|
177
|
+
event(msg);
|
|
178
|
+
utils.formatMessage(msg);
|
|
179
|
+
return msg;
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
let Message = {
|
|
184
|
+
watcher: new ObserverList(),
|
|
185
|
+
_push: (message) => {
|
|
186
|
+
// console.log('received msg', message);
|
|
187
|
+
//不处理离线消息
|
|
188
|
+
if (message.isOffLineMessage) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
bindSender(message);
|
|
192
|
+
Message.watcher.notify(message);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
let sendMessage = (type, targetId, message) => {
|
|
198
|
+
let bindUser = (_msg, next) => {
|
|
199
|
+
bindSender(_msg);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// let { name, avatar } = currentUser;
|
|
203
|
+
let { name, avatar } = { name: 'aaa', avatar: 'bbb' };
|
|
204
|
+
let user = {
|
|
205
|
+
name,
|
|
206
|
+
avatar
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
let messageMap = {
|
|
210
|
+
text: (params) => {
|
|
211
|
+
params.messageType = 'RC:TxtMsg';
|
|
212
|
+
return params;
|
|
213
|
+
},
|
|
214
|
+
image: (params) => {
|
|
215
|
+
params.messageType = 'RC:ImgMsg';
|
|
216
|
+
return params;
|
|
217
|
+
},
|
|
218
|
+
voice: (params) => {
|
|
219
|
+
params.messageType = 'RC:HQVCMsg';
|
|
220
|
+
return params;
|
|
221
|
+
},
|
|
222
|
+
music: (params) => {
|
|
223
|
+
params.messageType = 'seal:music';
|
|
224
|
+
return params;
|
|
225
|
+
},
|
|
226
|
+
file: (params) => {
|
|
227
|
+
params.messageType = 'RC:FileMsg';
|
|
228
|
+
return params;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
message.user = user;
|
|
233
|
+
let params = {
|
|
234
|
+
content: message
|
|
235
|
+
};
|
|
236
|
+
params = messageMap[message.type](params);
|
|
237
|
+
let conversation = imInstance.Conversation.get({
|
|
238
|
+
type: +type,
|
|
239
|
+
targetId
|
|
240
|
+
});
|
|
241
|
+
return conversation.send(params).then((message) => {
|
|
242
|
+
bindUser(message);
|
|
243
|
+
return message;
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
//params.type
|
|
248
|
+
//params.targetId
|
|
249
|
+
//params.content
|
|
250
|
+
Message.sendText = (params) => {
|
|
251
|
+
let { type, targetId, content, userInfo } = params;
|
|
252
|
+
return sendMessage(type, targetId, {
|
|
253
|
+
type: 'text',
|
|
254
|
+
userInfo,
|
|
255
|
+
content
|
|
256
|
+
})
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
//params.type
|
|
260
|
+
//params.targetId
|
|
261
|
+
//params.content
|
|
262
|
+
//params.iamgeUri
|
|
263
|
+
Message.sendImage = (params) => {
|
|
264
|
+
let { type, targetId, content, imageUri, extra, userInfo } = params;
|
|
265
|
+
return sendMessage(type, targetId, {
|
|
266
|
+
type: 'image',
|
|
267
|
+
content,
|
|
268
|
+
imageUri,
|
|
269
|
+
extra,
|
|
270
|
+
userInfo
|
|
271
|
+
})
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
//params.type
|
|
275
|
+
//params.targetId
|
|
276
|
+
//params.name
|
|
277
|
+
//params.size
|
|
278
|
+
//params.type
|
|
279
|
+
//params.fileUrl
|
|
280
|
+
Message.sendFile = (params) => {
|
|
281
|
+
let { type, targetId, name, size, fileUrl } = params;
|
|
282
|
+
return sendMessage(type, targetId, {
|
|
283
|
+
type: 'file',
|
|
284
|
+
name,
|
|
285
|
+
size,
|
|
286
|
+
fileUrl,
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
//params.type
|
|
291
|
+
//params.targetId
|
|
292
|
+
Message.sendVoice = (params) => {
|
|
293
|
+
let { type, targetId, content } = params;
|
|
294
|
+
let data = utils.extend({ type: 'voice' }, content);
|
|
295
|
+
return sendMessage(type, targetId, data)
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
let getMusic = () => {
|
|
299
|
+
let len = MusicList.length;
|
|
300
|
+
let index = Math.floor(Math.random() * len);
|
|
301
|
+
return MusicList[index];
|
|
302
|
+
};
|
|
303
|
+
Message.sendMusic = (params) => {
|
|
304
|
+
let { type, targetId } = params;
|
|
305
|
+
let content = utils.extend({ type: 'music' }, getMusic());
|
|
306
|
+
return sendMessage(type, targetId, content);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// params.type
|
|
310
|
+
// params.targetId
|
|
311
|
+
// params.position 0/1
|
|
312
|
+
Message.getList = (params) => {
|
|
313
|
+
let { type, targetId, position, count } = params;
|
|
314
|
+
count = count || 5;
|
|
315
|
+
let timestamp = position;
|
|
316
|
+
let conversation = imInstance.Conversation.get({
|
|
317
|
+
type: +type,
|
|
318
|
+
targetId
|
|
319
|
+
});
|
|
320
|
+
return conversation.getMessages({
|
|
321
|
+
timestrap: timestamp,
|
|
322
|
+
count
|
|
323
|
+
}).then(({ list, hasMore }) => {
|
|
324
|
+
let messageList = list;
|
|
325
|
+
bindSender(messageList, position);
|
|
326
|
+
hasMore = !!hasMore;
|
|
327
|
+
return {
|
|
328
|
+
messageList,
|
|
329
|
+
hasMore
|
|
330
|
+
};
|
|
331
|
+
})
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
Message.getChatRoomMessageList = (params) => {
|
|
335
|
+
let { type, targetId, position, count } = params;
|
|
336
|
+
count = count || 5;
|
|
337
|
+
let timestamp = position;
|
|
338
|
+
let chatRoom = imInstance.ChatRoom.get({
|
|
339
|
+
id: targetId
|
|
340
|
+
});
|
|
341
|
+
return chatRoom.getMessages({
|
|
342
|
+
timestrap: timestamp,
|
|
343
|
+
count
|
|
344
|
+
}).then(({ list, hasMore }) => {
|
|
345
|
+
let messageList = list;
|
|
346
|
+
// let messageList = list.filter((message) => {
|
|
347
|
+
// return message.messageType != 'RC:RcCmd';
|
|
348
|
+
// });
|
|
349
|
+
bindSender(messageList, position);
|
|
350
|
+
hasMore = !!hasMore;
|
|
351
|
+
return {
|
|
352
|
+
messageList,
|
|
353
|
+
hasMore
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
Message.create = (params) => {
|
|
359
|
+
let { name, type, targetId, content } = params;
|
|
360
|
+
let message = {
|
|
361
|
+
messageType: name,
|
|
362
|
+
type,
|
|
363
|
+
targetId: targetId,
|
|
364
|
+
senderUserId: currentUser.id,
|
|
365
|
+
content: content,
|
|
366
|
+
messageDirection: 1,
|
|
367
|
+
messageUId: 'M' + Date.now(),
|
|
368
|
+
sentTime: Date.now()
|
|
369
|
+
};
|
|
370
|
+
bindSender(message);
|
|
371
|
+
return message;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
Message.watch = (watch) => {
|
|
375
|
+
Message.watcher.add(watch);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
Message.unwatch = () => {
|
|
379
|
+
Message.watcher.remove();
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
let Conversation = {
|
|
383
|
+
watcher: new ObserverList()
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
Conversation.getList = () => {
|
|
387
|
+
return imInstance.Conversation.getList().then((list) => {
|
|
388
|
+
conversationList = imInstance.Conversation.merge({
|
|
389
|
+
conversationList,
|
|
390
|
+
updatedConversationList: list
|
|
391
|
+
});
|
|
392
|
+
bindUserInfo(conversationList);
|
|
393
|
+
return conversationList;
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
let bindUserInfo = (list) => {
|
|
398
|
+
let unknowUser = {
|
|
399
|
+
name: '火星人',
|
|
400
|
+
// avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-user-unkown.png'
|
|
401
|
+
avatar: require('../../../../static/im/icon2.png'),
|
|
402
|
+
};
|
|
403
|
+
let unknowGroup = {
|
|
404
|
+
name: '火星群组',
|
|
405
|
+
avatar: 'http://7xogjk.com1.z0.glb.clouddn.com/rc-mini-group-unkown.png'
|
|
406
|
+
};
|
|
407
|
+
if (!utils.isArray(list)) {
|
|
408
|
+
list = [list];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
let infoMap = {
|
|
412
|
+
1: (conversation) => {
|
|
413
|
+
conversation.target = utils.find(UserList, (user) => {
|
|
414
|
+
return user.id == conversation.targetId
|
|
415
|
+
}) || utils.copy(unknowUser);
|
|
416
|
+
},
|
|
417
|
+
2: (conversation) => {
|
|
418
|
+
conversation.target = utils.copy(unknowUser);
|
|
419
|
+
},
|
|
420
|
+
3: (conversation) => {
|
|
421
|
+
conversation.target = utils.find(GroupList, (group) => {
|
|
422
|
+
return group.id == conversation.targetId
|
|
423
|
+
}) || utils.copy(unknowGroup);;
|
|
424
|
+
},
|
|
425
|
+
10: (conversation) => {
|
|
426
|
+
conversation.target = utils.copy(unknowUser);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
let formatMsg = (msg) => {
|
|
430
|
+
let { messageType } = msg;
|
|
431
|
+
let content = '[此消息类型未解析]';
|
|
432
|
+
if (messageType == 'RC:TxtMsg') {
|
|
433
|
+
if (msg.content) {
|
|
434
|
+
content = msg.content.content;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (messageType == 'RC:VcMsg' || messageType == 'RC:HQVCMsg') {
|
|
438
|
+
content = '[语音]';
|
|
439
|
+
}
|
|
440
|
+
if (messageType == 'RC:ImgMsg') {
|
|
441
|
+
content = '[图片]';
|
|
442
|
+
}
|
|
443
|
+
if (messageType == 'RC:FileMsg') {
|
|
444
|
+
content = '[文件]';
|
|
445
|
+
}
|
|
446
|
+
if (messageType == 'seal:music') {
|
|
447
|
+
content = '[音乐]';
|
|
448
|
+
}
|
|
449
|
+
return content;
|
|
450
|
+
};
|
|
451
|
+
list.forEach((conversation, index) => {
|
|
452
|
+
const { latestMessage, unreadMessageCount, type, targetId } = conversation;
|
|
453
|
+
const { sentTime } = latestMessage;
|
|
454
|
+
conversation._sentTime = utils.getTime(sentTime);
|
|
455
|
+
conversation.unReadCount = unreadMessageCount;
|
|
456
|
+
conversation.content = formatMsg(latestMessage);
|
|
457
|
+
let _type = type > 3 ? 10 : type;
|
|
458
|
+
infoMap[_type](conversation);
|
|
459
|
+
// conversation.target.name = targetId + ' (' + type + ')';
|
|
460
|
+
conversation.target.name = targetId;
|
|
461
|
+
list[index] = conversation;
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
Conversation.clearUnreadCount = (conversation) => {
|
|
466
|
+
let { type, targetId } = conversation;
|
|
467
|
+
conversation.type = +conversation.type;
|
|
468
|
+
imInstance.Conversation.get(conversation).read();
|
|
469
|
+
};
|
|
470
|
+
Conversation.watch = (watcher) => {
|
|
471
|
+
imInstance.watch({
|
|
472
|
+
conversation: function (event) {
|
|
473
|
+
const { updatedConversationList } = event;
|
|
474
|
+
conversationList = imInstance.Conversation.merge({
|
|
475
|
+
conversationList,
|
|
476
|
+
updatedConversationList
|
|
477
|
+
});
|
|
478
|
+
bindUserInfo(conversationList);
|
|
479
|
+
watcher(conversationList);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
};
|
|
483
|
+
Conversation.setStatus = (conversation, options) => {
|
|
484
|
+
const params = {
|
|
485
|
+
targetId: conversation.targetId,
|
|
486
|
+
type: conversation.type
|
|
487
|
+
};
|
|
488
|
+
return imInstance.Conversation.get(params).setStatus(options);
|
|
489
|
+
}
|
|
490
|
+
let Status = {
|
|
491
|
+
watcher: new ObserverList()
|
|
492
|
+
};
|
|
493
|
+
Status.disconnect = () => {
|
|
494
|
+
imInstance.disconnect();
|
|
495
|
+
};
|
|
496
|
+
Status.connect = (user) => {
|
|
497
|
+
currentUser = user;
|
|
498
|
+
imInstance.watch({
|
|
499
|
+
status: function ({ status }) {
|
|
500
|
+
Status.watcher.notify(status);
|
|
501
|
+
},
|
|
502
|
+
message: function ({ message }) {
|
|
503
|
+
let { messageType } = message;
|
|
504
|
+
let messageCtrol = {
|
|
505
|
+
otherMessage: () => {
|
|
506
|
+
Message._push(message);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
let messageHandler = messageCtrol[messageType] || messageCtrol.otherMessage;
|
|
510
|
+
messageHandler();
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
user.token = config.token;
|
|
514
|
+
return imInstance.connect(user).then((result) => {
|
|
515
|
+
currentUser.id = result.id;
|
|
516
|
+
return result;
|
|
517
|
+
}).catch((error) => {
|
|
518
|
+
console.log(error);
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
Status.watch = (watch) => {
|
|
523
|
+
var force = true;
|
|
524
|
+
Status.watcher.add(watch, force);
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
let File = {};
|
|
528
|
+
|
|
529
|
+
const zfbUpload = (fileInfo, token, bosHeaders, bosUrl) => {
|
|
530
|
+
return new Promise((resolve, reject) => {
|
|
531
|
+
const uploadTask = my.uploadFile({
|
|
532
|
+
url: 'https://upload.qiniup.com',
|
|
533
|
+
filePath: fileInfo.path,
|
|
534
|
+
fileType: 'image',
|
|
535
|
+
fileName: 'file',
|
|
536
|
+
header: {
|
|
537
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
538
|
+
},
|
|
539
|
+
formData: {
|
|
540
|
+
token: token
|
|
541
|
+
},
|
|
542
|
+
success: function (res) {
|
|
543
|
+
resolve(res);
|
|
544
|
+
},
|
|
545
|
+
fail: function (err) {
|
|
546
|
+
console.log('upload qiniu failed', err);
|
|
547
|
+
}
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
uploadTask.onProgressUpdate(res => {
|
|
551
|
+
console.log('上传进度', res.progress)
|
|
552
|
+
console.log('已经上传的数据长度', res.totalBytesSent)
|
|
553
|
+
console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
|
|
554
|
+
})
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
File.upload = (fileInfo, uploadType) => {
|
|
561
|
+
let fileType = uploadType || RongIMLib.FILE_TYPE.FILE;
|
|
562
|
+
let fileName = fileInfo.name || 'base64.png';
|
|
563
|
+
console.log('fileType, fileName', fileType, fileName)
|
|
564
|
+
return imInstance.getFileToken(fileType, fileName).then(result => {
|
|
565
|
+
console.log('result', result)
|
|
566
|
+
let { token, bosToken, bosDate, bos, path } = result;
|
|
567
|
+
let bosHeaders = {
|
|
568
|
+
'authorization': bosToken,
|
|
569
|
+
'x-bce-date': bosDate,
|
|
570
|
+
'Content-Type': 'multipart/form-data',
|
|
571
|
+
}
|
|
572
|
+
let bosUrl = bos + path;
|
|
573
|
+
return { fileInfo, token, bosHeaders, bosUrl }
|
|
574
|
+
// return zfbUpload(fileInfo, token, bosHeaders, bosUrl)
|
|
575
|
+
})
|
|
576
|
+
// .then(res => {
|
|
577
|
+
// let qiniuHash, qiniuName;
|
|
578
|
+
// if (!res.isBosRes) {
|
|
579
|
+
// const { data } = res;
|
|
580
|
+
// const { hash, name } = JSON.parse(data);
|
|
581
|
+
// qiniuHash = hash, qiniuName = name;
|
|
582
|
+
// }
|
|
583
|
+
// return imInstance.getFileUrl(fileType, qiniuHash, qiniuName, res);
|
|
584
|
+
// })
|
|
585
|
+
}
|
|
586
|
+
File.FileToken = (fileType) => {
|
|
587
|
+
return imInstance.getFileToken(fileType).then(result => {
|
|
588
|
+
return result
|
|
589
|
+
})
|
|
590
|
+
}
|
|
591
|
+
File.getFileUrl = (fileType, data) => {
|
|
592
|
+
return new Promise((resolve, reject) => {
|
|
593
|
+
imInstance.getFileUrl(fileType, data.hash, data.name, data).then(res => {
|
|
594
|
+
resolve(res)
|
|
595
|
+
})
|
|
596
|
+
.catch((error) => {
|
|
597
|
+
console.log('获取文件 url 失败', error);
|
|
598
|
+
reject();
|
|
599
|
+
});
|
|
600
|
+
})
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const Init = (_config) => {
|
|
604
|
+
utils.extend(config, _config);
|
|
605
|
+
config.connectType = 'comet';
|
|
606
|
+
config.debug = true;
|
|
607
|
+
imInstance = RongIMLib.init(config);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
let modules = {
|
|
611
|
+
User,
|
|
612
|
+
Message,
|
|
613
|
+
Conversation,
|
|
614
|
+
Friend,
|
|
615
|
+
Status,
|
|
616
|
+
File,
|
|
617
|
+
ConnectionStatus: RongIMLib.CONNECTION_STATUS,
|
|
618
|
+
CONNECTION_STATUS: RongIMLib.CONNECTION_STATUS,
|
|
619
|
+
Init,
|
|
620
|
+
};
|
|
621
|
+
module.exports = (_config) => {
|
|
622
|
+
// config.customCMP = ['cometqa.cn.ronghub.com:8201'];
|
|
623
|
+
// config.customCMP = ['wsqa0.cn.ronghub.com'];
|
|
624
|
+
return modules;
|
|
625
|
+
};
|