79 lines
1.8 KiB
JavaScript
79 lines
1.8 KiB
JavaScript
// app.js
|
|
import config from './config';
|
|
import createBus from './utils/eventBus';
|
|
const API = require('./utils/api.js');
|
|
|
|
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();
|
|
},
|
|
|
|
onShow(options) {
|
|
wx.login({
|
|
success: (res) => {
|
|
if (res.code) {
|
|
API.user.wxSignin({ code: res.code })
|
|
.then((data) => {
|
|
if (data.code == 0) {
|
|
this.globalData.user = data.data.user
|
|
this.eventBus.emit('user-login', data.data.user)
|
|
} else {
|
|
console.log('登录失败!')
|
|
}
|
|
})
|
|
} else {
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
/** 全局事件总线 */
|
|
eventBus: createBus(),
|
|
|
|
/** 初始化WebSocket */
|
|
connect() {
|
|
const socket = null //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() {
|
|
},
|
|
|
|
/** 设置未读消息数量 */
|
|
setUnreadNum(unreadNum) {
|
|
this.globalData.unreadNum = unreadNum;
|
|
this.eventBus.emit('unread-num-change', unreadNum);
|
|
},
|
|
|
|
|
|
globalData: {
|
|
user: null,
|
|
unreadNum: 0, // 未读消息数量
|
|
socket: null, // SocketTask 对象
|
|
},
|
|
});
|