generated from LeeJim/miniprogram-starter
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.js
66 lines (57 loc) · 1.54 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
// app.js
import config from './config';
import Mock from './mock/index';
import createBus from './utils/eventBus';
import { connectSocket, fetchUnreadNum } from './mock/chat';
if (config.isMock) {
Mock();
}
App({
onLaunch() {
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate((res) => {
// console.log(res.hasUpdate)
});
updateManager.onUpdateReady(() => {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate();
}
},
});
});
this.getUnreadNum();
this.connect();
},
globalData: {
userInfo: null,
unreadNum: 0, // 未读消息数量
socket: null, // SocketTask 对象
},
/** 全局事件总线 */
eventBus: createBus(),
/** 初始化WebSocket */
connect() {
const socket = connectSocket();
socket.onMessage((data) => {
data = JSON.parse(data);
if (data.type === 'message' && !data.data.message.read) this.setUnreadNum(this.globalData.unreadNum + 1);
});
this.globalData.socket = socket;
},
/** 获取未读消息数量 */
getUnreadNum() {
fetchUnreadNum().then(({ data }) => {
this.globalData.unreadNum = data;
this.eventBus.emit('unread-num-change', data);
});
},
/** 设置未读消息数量 */
setUnreadNum(unreadNum) {
this.globalData.unreadNum = unreadNum;
this.eventBus.emit('unread-num-change', unreadNum);
},
});