index.js
10.3 KB
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
import Vue from 'vue'
import store from '../store'
if(!Function.prototype.bind){
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}
}
let Utils = Object.create(null)
Utils.encode = function (_map, _content) {
_content = '' + _content
if (!_map || !_content) {
return _content||''
}
return _content.replace(_map.r, function ($1) {
var _result = _map[!_map.i ? $1.toLowerCase() : $1]
return _result != null ? _result : $1
});
};
Utils.escape = (function () {
let _reg = /<br\/?>$/
let _map = {
r:/\<|\>|\&|\r|\n|\s|\'|\"/g,
'<':'<',
'>':'>',
'&':'&',
' ':' ',
'"':'"',
"'":''',
'\n':'<br/>',
'\r':''
}
return function(_content){
_content = Utils.encode(_map, _content)
return _content.replace(_reg, '<br/>');
};
})();
Utils.object2query = function (obj) {
let keys = Object.keys(obj)
let queryArray = keys.map(item => {
return `${item}=${encodeURIComponent(obj[item])}`
})
return queryArray.join('&')
}
// https://cn.vuejs.org/v2/guide/reactivity.html
// Vue 不能检测到对象属性的添加或删除。然而它可以使用 Vue.set(object, key, value) 方法将响应属性添加到嵌套的对象上
Utils.mergeObject = function (dest, src) {
if (typeof dest !== 'object' || dest === null) {
dest = Object.create(null)
}
dest = Object.assign(Object.create(null), dest, src)
return dest
}
Utils.mergeVueObject = function (dest, src) {
let keys = Object.keys(src)
keys.forEach(item => {
if (typeof src[item] !== 'undefined') {
Vue.set(dest, item, src[item])
}
})
return dest
}
// 消息类型列表
Utils.mapMsgType = function (msg) {
let map = {
text: '文本消息',
image: '图片消息',
file: '文件消息',
audio: '语音消息',
video: '视频消息',
geo: '地理位置消息',
tip: '提醒消息',
custom: '自定义消息',
notification: '系统通知',
robot: '机器人消息'
}
let type = msg.type
return map[type] || '未知消息类型'
}
Utils.stringifyDate = function (datetime, simple = false) {
// let weekMap = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
let weekMap = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
datetime = new Date(datetime)
let year = datetime.getFullYear()
let simpleYear = datetime.getYear() - 100
let month = datetime.getMonth() + 1
month = month > 9 ? month : '0' + month
let day = datetime.getDate()
day = day > 9 ? day : '0' + day
let hour = datetime.getHours()
hour = hour > 9 ? hour : '0' + hour
let min = datetime.getMinutes()
min = min > 9 ? min : '0' + min
let week = datetime.getDay()
week = weekMap[week]
let thatDay = (new Date(year, month - 1, day, 0, 0, 0)).getTime()
if (simple) {
return {
withYear: `${day}/${month}/${simpleYear}`,
withMonth: `${month}-${day}`,
withDay: `${week}`,
withLastDay: `昨天`,
withHour: `${hour}:${min}`,
thatDay
}
} else {
return {
withYear: `${year}-${month}-${day} ${hour}:${min}`,
withMonth: `${month}-${day} ${hour}:${min}`,
withDay: `${week} ${hour}:${min}`,
withLastDay: `昨天 ${hour}:${min}`,
withHour: `${hour}:${min}`,
thatDay
}
}
}
/* 格式化日期 */
Utils.formatDate = function (datetime, simple = false) {
let tempDate = (new Date()).getTime()
let result = this.stringifyDate(datetime, simple)
let thatDay = result.thatDay
let deltaTime = (tempDate - thatDay) / 1000
if (deltaTime < 3600 * 24) {
return result.withHour
} else if (deltaTime < 3600 * 24 * 2) {
return result.withLastDay
} else if (deltaTime < 3600 * 24 * 7) {
return result.withDay
} else if (deltaTime < 3600 * 24 * 30) {
return result.withMonth
} else {
return result.withYear
}
}
Utils.parseSession = function (sessionId) {
if (/^p2p-/.test(sessionId)) {
return {
scene: 'p2p',
to: sessionId.replace(/^p2p-/, '')
}
} else if (/^team-/.test(sessionId)) {
return {
scene: 'team',
to: sessionId.replace(/^team-/, '')
}
}
}
Utils.parseCustomMsg = function (msg) {
if (msg.type === 'custom') {
try {
let cnt = JSON.parse(msg.content)
switch (cnt.type) {
case 1:
return '[猜拳消息]'
case 2:
return '[阅后即焚]'
case 3:
return '[贴图表情]'
case 4:
return '[白板消息]'
case 30:
return '[图文消息]'
}
} catch (e) {}
return '[自定义消息]'
}
return ''
}
/* 获得有效的备注名 */
Utils.getFriendAlias = function (userInfo) {
userInfo.alias = userInfo.custom && userInfo.custom.realName ? userInfo.custom.realName.trim() : null;
userInfo.alias = userInfo.alias ? userInfo.alias.trim() : ''
return userInfo.alias || userInfo.nick || userInfo.account
}
Utils.generateChatroomSysMsg = function (data) {
let text
switch (data.attach.type) {
case 'memberEnter':
text = `欢迎${data.attach.fromNick}进入直播间`
break
case 'memberExit':
text = `${data.attach.fromNick}离开了直播间`
break
case 'blackMember':
text = `${(data.attach.toNick[0] || data.attach.to[0])}被管理员拉入黑名单`
break
case 'unblackMember':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被管理员解除拉黑`
break
case 'gagMember':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被管理员禁言`
break
case 'ungagMember':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被管理员解除禁言`
break
case 'addManager':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被任命管理员身份`
break
case 'removeManager':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被解除管理员身份`
break;
case 'addTempMute':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被管理员临时禁言`
break;
case 'removeTempMute':
text = `${(data.attach.toNick[0]||data.attach.to[0])}被管理员解除临时禁言`
break;
case 'addCommon':
text = `管理员添加普通成员`
break
case 'removeCommon':
text = `管理员删除普通成员`
break
case 'kickMember':
text = `${data.attach.toNick[0]}被管理员踢出房间`
break;
// case 'xxx':
// 直播公告已更新
// break;
default:
text = '通知消息'
break
}
return text
}
Utils.generateTeamSysmMsg = function (data) {
var text, nicks = this.getNickNames(data.attach.users)
switch (data.attach.type) {
case 'updateTeam':
text = this.getTeamUpdateInfo(data)
break;
case 'addTeamMembers': {
let op = nicks.pop()
text = `${op}邀请${nicks.join()}加入群`
break;
}
case 'removeTeamMembers': {
let op = nicks.pop()
text = `${nicks.join()}被${op}移出群`
break;
}
case 'acceptTeamInvite': {
let op = nicks.pop()
text = `${nicks.join()}接受了${op}入群邀请`
break;
}
case 'passTeamApply': {
let op = nicks.shift()
if (nicks.length===1 && op===nicks[0]) {
// 此情况为高级群设置不需要验证,用户申请入群后,收到的群消息提示
text = `${op}加入群`
} else {
text = `${op}通过了${nicks}入群邀请`
}
break;
}
case 'addTeamManagers': {
// todo test
let op = nicks.pop()
text = `${op}新增了${nicks}为管理员`
break;
}
case 'removeTeamManagers': {
// todo test
let op = nicks.pop()
text = `${op}移除了${nicks}的管理员权限`
break;
}
case 'leaveTeam': {
text = `${nicks.join()}退出了群`
break;
}
case 'dismissTeam': {
text = `${nicks.join()}解散了群`
break;
}
case 'transferTeam': {
// todo test
let nicks = this.getNickNames(data.attach.users)
let op = nicks.shift()
text = `${op}转让群主给${nicks}`
break;
}
case 'updateTeamMute':{
let nicks = this.getNickNames(data.attach.users)
let op = nicks.shift()
text = `${nicks}被管理员${data.attach.mute ? '禁言' : '解除禁言'}`
break;
}
default:
break;
}
return text
}
// todo 写成私有成员方法
Utils.getNickNames = function(users) {
return users.map(user => {
return user.account === store.state.userUID? '你' : user.nick
})
}
// todo 写成私有成员方法
Utils.getTeamUpdateInfo = function(msg) {
let text, team = msg.attach.team, op = this.getNickNames(msg.attach.users).pop()
if (team['name']) {
text = `${op}修改群名为${team['name']}`
} else if (team['intro']) {
text = `${op}修改群介绍为${team['intro']}`
}
// 由于群公告的交互与 Android iOS 不一致,现版本不适配群公告
// else if (team['announcement']) {
// text = `${op}修改群公告为${team['announcement']}`
// }
else if (team['joinMode']) {
text = `群身份验证模式更新为${team.joinMode === 'noVerify' ? '不需要验证' : team.joinMode === 'needVerify' ? '需要验证' : '禁止任何人加入'}`
} else if (team['inviteMode']) {
text = `邀请他人权限为${team['inviteMode'] === 'all' ? '所有人' : '管理员'}`
} else if (team['updateTeamMode']) {
text = `群资料修改权限为${team['updateTeamMode'] === 'all' ? '所有人' : '管理员'}`
} else if (team['beInviteMode']) {
text = `被邀请人身份${team['beInviteMode'] === 'noVerify' ? '不需要验证' : '需要验证'}`
} else {
text = '更新群信息'
}
return text
}
Utils.teamConfigMap = {
joinMode: {
'noVerify': '不需要验证',
'needVerify': '需要验证',
'rejectAll': '禁止任何人加入'
},
beInviteMode: {
'needVerify': '需要验证',
'noVerify': '不需要验证'
},
inviteMode: {
'manager': '管理员邀请',
'all': '所有人邀请'
},
updateTeamMode: {
'manager': '管理员修改',
'all': '所有人修改'
},
memberType: {
'manager': '管理员',
'normal': '普通成员'
}
}
export default Utils