forked from lizg001/room-xcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·370 lines (344 loc) · 8.74 KB
/
app.js
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
require("sdk/libs/strophe");
let WebIM = require("utils/WebIM")["default"];
let msgStorage = require("comps/chat/msgstorage");
let msgType = require("comps/chat/msgtype");
let ToastPannel = require("./comps/toast/toast");
let disp = require("utils/broadcast");
let logout = false;
function ack(receiveMsg){
// 处理未读消息回执
var bodyId = receiveMsg.id; // 需要发送已读回执的消息id
var ackMsg = new WebIM.message("read", WebIM.conn.getUniqueId());
ackMsg.set({
id: bodyId,
to: receiveMsg.from
});
WebIM.conn.send(ackMsg.body);
}
function onMessageError(err){
if(err.type === "error"){
wx.showToast({
title: err.errorText
});
return false;
}
return true;
}
function getCurrentRoute(){
let pages = getCurrentPages();
let currentPage = pages[pages.length - 1];
return currentPage.route;
}
function calcUnReadSpot(message){
}
App({
ToastPannel,
globalData: {
unReadMessageNum: 0,
userInfo: null,
saveFriendList: [],
saveGroupInvitedList: [],
isIPX: false //是否为iphone X
},
data: {
myName: WebIM.conn.context.userId
},
conn: {
closed: false,
curOpenOpt: {},
open(opt){
wx.showLoading({
title: '正在初始化客户端...',
mask: true
})
this.curOpenOpt = opt;
WebIM.conn.open(opt);
this.closed = false;
},
reopen(){
if(this.closed){
//this.open(this.curOpenOpt);
WebIM.conn.open(this.curOpenOpt);
this.closed = false;
}
}
},
// getPage(pageName){
// var pages = getCurrentPages();
// return pages.find(function(page){
// return page.__route__ == pageName;
// });
// },
onLaunch(){
// 调用 API 从本地缓存中获取数据
var me = this;
var logs = wx.getStorageSync("logs") || [];
logs.unshift(Date.now());
wx.setStorageSync("logs", logs);
//
disp.on("em.main.ready", function(){
calcUnReadSpot();
});
disp.on("em.chatroom.leave", function(){
calcUnReadSpot();
});
disp.on("em.chat.session.remove", function(){
calcUnReadSpot();
});
disp.on('em.chat.audio.fileLoaded', function(){
calcUnReadSpot()
});
disp.on('em.main.deleteFriend', function(){
calcUnReadSpot()
});
disp.on('em.chat.audio.fileLoaded', function(){
calcUnReadSpot()
});
//
WebIM.conn.listen({
onOpened(message){
WebIM.conn.setPresence();
console.log("登陆成功" + JSON.stringify(message));
wx.redirectTo({
url: '../roomlist/roomlist?myName=' + WebIM.conn.context.userId,
});
},
onReconnect(){
wx.showToast({
title: "重连中...",
duration: 2000
});
},
onClosed(){
wx.showToast({
title: "网络已断开连接",
duration: 2000
});
wx.redirectTo({
url: "../login/login"
});
me.conn.closed = true;
WebIM.conn.close();
},
onInviteMessage(message){
me.globalData.saveGroupInvitedList.push(message);
disp.fire("em.xmpp.invite.joingroup", message);
},
onPresence(message) {
console.log("*****************");
switch (message.type) {
case 'subscribe': // 对方请求添加好友
var truthBeTold = window.confirm((message.from + "申请添加您为好友:"));
if (truthBeTold) {
// 同意对方添加好友
conn.subscribed({
to: message.from,
message: "[resp:true]"
});
console.log("同意添加好友");
} else {
// 拒绝对方添加好友
conn.unsubscribed({
to: message.from,
message: "rejectAddFriend" // 拒绝添加好友回复信息
});
console.log("拒绝添加好友");
}
break;
case 'subscribed': // 对方同意添加好友,已方同意添加好友
break;
case 'unsubscribe': // 对方删除好友
break;
case 'unsubscribed': // 被拒绝添加好友,或被对方删除好友成功
break;
case 'memberJoinChatRoomSuccess': // 成功加入聊天室
console.log('join chat room success');
// wx.navigateTo({
// url: '../chatroom/chatroom',
// });
break;
case 'joinChatRoomFaild': // 加入聊天室失败
console.log('join chat room faild');
break;
case 'joinPublicGroupSuccess': // 意义待查
console.log('join public group success', message.from);
break;
case 'createGroupACK':
conn.createGroupAsync({
from: message.from,
success: function (option) {
console.log('Create Group Succeed');
}
});
break;
}
},
onRoster(message){
// let pages = getCurrentPages();
// if(pages[0]){
// pages[0].onShow();
// }
},
// onVideoMessage(message){
// console.log("onVideoMessage: ", message);
// if(message){
// msgStorage.saveReceiveMsg(message, msgType.VIDEO);
// }
// },
onAudioMessage(message){
console.log("onAudioMessage", message);
if(message){
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.AUDIO);
}
calcUnReadSpot(message);
ack(message);
}
},
onCmdMessage(message){
console.log("onCmdMessage", message);
if(message){
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.CMD);
}
calcUnReadSpot(message);
ack(message);
}
},
// onLocationMessage(message){
// console.log("Location message: ", message);
// if(message){
// msgStorage.saveReceiveMsg(message, msgType.LOCATION);
// }
// },
onTextMessage(message){
console.log("onTextMessage", message);
if(message){
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.TEXT);
}
calcUnReadSpot(message);
ack(message);
}
},
onEmojiMessage(message){
console.log("onEmojiMessage", message);
if(message){
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.EMOJI);
}
calcUnReadSpot(message);
ack(message);
}
},
onPictureMessage(message){
console.log("onPictureMessage", message);
if(message){
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.IMAGE);
}
calcUnReadSpot(message);
ack(message);
}
},
onFileMessage(message){
console.log('onFileMessage', message);
if (message) {
if(onMessageError(message)){
msgStorage.saveReceiveMsg(message, msgType.FILE);
}
calcUnReadSpot(message);
ack(message);
}
},
// 各种异常
onError(error){
// 16: server-side close the websocket connection
if(error.type == WebIM.statusCode.WEBIM_CONNCTION_DISCONNECTED && !logout){
if(WebIM.conn.autoReconnectNumTotal < WebIM.conn.autoReconnectNumMax){
return;
}
wx.showToast({
title: "server-side close the websocket connection",
duration: 1000
});
wx.redirectTo({
url: "../login/login"
});
logout = true
return;
}
// 8: offline by multi login
if(error.type == WebIM.statusCode.WEBIM_CONNCTION_SERVER_ERROR){
wx.showToast({
title: "offline by multi login",
duration: 1000
});
wx.redirectTo({
url: "../login/login"
});
}
if(error.type == WebIM.statusCode.WEBIM_CONNCTION_OPEN_ERROR){
wx.hideLoading()
disp.fire("em.xmpp.error.passwordErr");
// wx.showModal({
// title: "用户名或密码错误",
// confirmText: "OK",
// showCancel: false
// });
}
if (error.type == WebIM.statusCode.WEBIM_CONNCTION_AUTH_ERROR) {
wx.hideLoading()
disp.fire("em.xmpp.error.tokenErr");
}
},
});
this.checkIsIPhoneX();
},
onShow(){
this.conn.reopen();
},
onUnload(){
WebIM.conn.close();
WebIM.conn.stopHeartBeat();
wx.redirectTo({
url: "../login/login?myName=" + myName
});
},
onLoginSuccess: function(myName){
wx.hideLoading()
wx.redirectTo({
url: "../chat/chat?myName=" + myName
});
},
getUserInfo(cb){
var me = this;
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo);
}
else{
// 调用登录接口
wx.login({
success(){
wx.getUserInfo({
success(res){
me.globalData.userInfo = res.userInfo;
typeof cb == "function" && cb(me.globalData.userInfo);
}
});
}
});
}
},
checkIsIPhoneX: function() {
const me = this
wx.getSystemInfo({
success: function (res) {
// 根据 model 进行判断
if (res.model.search('iPhone X') != -1) {
me.globalData.isIPX = true
}
}
})
},
});