| @@ -3,22 +3,22 @@ const Http = require("./utils/HttpBasics"); | |||
| const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {} | |||
| App({ | |||
| data: {}, | |||
| onLaunch: function (options) { | |||
| onLaunch: function(options) { | |||
| var that = this; | |||
| that.globalData.sceneAddress = options.scene; | |||
| /** | |||
| * 用户登录 | |||
| * 小程序版本更新 | |||
| */ | |||
| that.userLogin(options.scene); | |||
| that.autoUpdate(); | |||
| /** | |||
| * 小程序版本更新 | |||
| * 用户登录 | |||
| */ | |||
| that.updateManager(); | |||
| that.userLogin(options.scene); | |||
| }, | |||
| /** | |||
| * 用户登录 | |||
| */ | |||
| userLogin: function (sceneAddress) { | |||
| userLogin: function(sceneAddress) { | |||
| let that = this; | |||
| // 登录 | |||
| wx.login({ | |||
| @@ -31,9 +31,9 @@ App({ | |||
| sceneAddress: sceneAddress | |||
| } | |||
| Http.post({ | |||
| url: config.api.login, | |||
| data: usrdata | |||
| }) | |||
| url: config.api.login, | |||
| data: usrdata | |||
| }) | |||
| .then(res => { | |||
| if (res.data && res.data.score) { | |||
| if (res.data.score != 0) { | |||
| @@ -54,7 +54,7 @@ App({ | |||
| title: '提示', | |||
| showCancel: false, | |||
| content: '登录失败,请重新尝试', | |||
| success: function (res) { | |||
| success: function(res) { | |||
| if (res.cancel) { | |||
| //点击取消,默认隐藏弹框 | |||
| } else { | |||
| @@ -69,54 +69,38 @@ App({ | |||
| } | |||
| }) | |||
| }, | |||
| updateManager: function () { | |||
| /** | |||
| * 小程序版本更新 | |||
| */ | |||
| autoUpdate: function() { | |||
| let that = this; | |||
| if (wx.canIUse('getUpdateManager')) { | |||
| const updateManager = wx.getUpdateManager() | |||
| updateManager.onCheckForUpdate(function (res) { | |||
| // 1. 检查小程序是否有新版本发布 | |||
| updateManager.onCheckForUpdate(function(res) { | |||
| // 请求完新版本信息的回调 | |||
| if (res.hasUpdate) { | |||
| console.log('res.hasUpdate====') | |||
| updateManager.onUpdateReady(function () { | |||
| wx.showModal({ | |||
| title: '更新提示', | |||
| content: '新版本已经准备好,是否重启应用?', | |||
| success: function (res) { | |||
| console.log('success====', res) | |||
| // res: {errMsg: "showModal: ok", cancel: false, confirm: true} | |||
| if (res.confirm) { | |||
| // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 | |||
| updateManager.applyUpdate() | |||
| } else if (res.cancel) { | |||
| //如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了 | |||
| wx.showModal({ | |||
| title: '温馨提示~', | |||
| content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~', | |||
| success: function (res) { | |||
| self.autoUpdate() | |||
| return; | |||
| //第二次提示后,强制更新 | |||
| if (res.confirm) { | |||
| // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 | |||
| updateManager.applyUpdate() | |||
| } else if (res.cancel) { | |||
| //重新回到版本更新提示 | |||
| self.autoUpdate() | |||
| } | |||
| // 检测到新版本,需要更新,给出提示 | |||
| wx.showModal({ | |||
| title: '更新提示', | |||
| content: '检测到新版本,是否下载新版本并重启小程序?', | |||
| success: function (res) { | |||
| if (res.confirm) { | |||
| //2. 用户确定下载更新小程序,小程序下载及更新静默进行 | |||
| that.downLoadAndUpdate(updateManager) | |||
| } else if (res.cancel) { | |||
| //用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了 | |||
| wx.showModal({ | |||
| title: '温馨提示~', | |||
| content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~', | |||
| showCancel: false,//隐藏取消按钮 | |||
| confirmText: "确定更新",//只保留确定更新按钮 | |||
| success: function (res) { | |||
| if (res.confirm) { | |||
| //下载新版本,并重新应用 | |||
| that.downLoadAndUpdate(updateManager) | |||
| } | |||
| }) | |||
| } | |||
| } | |||
| }) | |||
| } | |||
| }) | |||
| }) | |||
| updateManager.onUpdateFailed(function () { | |||
| // 新的版本下载失败 | |||
| wx.showModal({ | |||
| title: '已经有新版本了哟~', | |||
| content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~' | |||
| }) | |||
| } | |||
| }) | |||
| } | |||
| }) | |||
| @@ -128,6 +112,26 @@ App({ | |||
| }) | |||
| } | |||
| }, | |||
| /** | |||
| * 下载小程序新版本并重启应用 | |||
| */ | |||
| downLoadAndUpdate: function(updateManager) { | |||
| let that = this | |||
| wx.showLoading(); | |||
| //静默下载更新小程序新版本 | |||
| updateManager.onUpdateReady(function() { | |||
| wx.hideLoading() | |||
| //新的版本已经下载好,调用 applyUpdate 应用新版本并重启 | |||
| updateManager.applyUpdate() | |||
| }) | |||
| updateManager.onUpdateFailed(function() { | |||
| // 新的版本下载失败 | |||
| wx.showModal({ | |||
| title: '已经有新版本了哟~', | |||
| content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~', | |||
| }) | |||
| }) | |||
| }, | |||
| globalData: { | |||
| scene: "", | |||
| // token | |||
| @@ -153,11 +157,11 @@ App({ | |||
| }, | |||
| // websocket | |||
| socketClient: null, | |||
| socketReceiver: function (e) { }, //收到消息回调 | |||
| socketReceiver: function(e) {}, //收到消息回调 | |||
| }, | |||
| // 初始化websocket | |||
| initSocket: function () { | |||
| initSocket: function() { | |||
| let that = this; | |||
| // socket是否连接 | |||
| let socketConnected = false; | |||
| @@ -206,7 +210,7 @@ App({ | |||
| connect(); | |||
| // 监听 WebSocket 连接打开事件 | |||
| wx.onSocketOpen(function (res) { | |||
| wx.onSocketOpen(function(res) { | |||
| console.log("WebSocket 连接成功") | |||
| socketConnected = true; | |||
| ws.onopen(); | |||
| @@ -218,12 +222,12 @@ App({ | |||
| }) | |||
| // 监听 WebSocket 接受到服务器的消息事件 | |||
| wx.onSocketMessage(function (res) { | |||
| wx.onSocketMessage(function(res) { | |||
| ws.onmessage(res); | |||
| }) | |||
| // 监听 WebSocket 错误事件 | |||
| wx.onSocketError(function (res) { | |||
| wx.onSocketError(function(res) { | |||
| console.log("WebSocket 错误事件") | |||
| if (!socketConnected) { | |||
| // 断线重连 | |||
| @@ -234,7 +238,7 @@ App({ | |||
| }) | |||
| // 监听 WebSocket 连接关闭事件 | |||
| wx.onSocketClose(function (res) { | |||
| wx.onSocketClose(function(res) { | |||
| console.log("WebSocket 连接关闭") | |||
| socketConnected = false; | |||
| // 断线重连 | |||
| @@ -250,11 +254,11 @@ App({ | |||
| * The heart-beating is using window.setInterval() to regularly send heart-beats and/or check server heart-beats. | |||
| * 可看stomp.js的源码(195,207,489行),由于小程序没有window对象,所以我们要调用小程序的定时器api实现 | |||
| */ | |||
| Stomp.setInterval = function (interval, f) { | |||
| Stomp.setInterval = function(interval, f) { | |||
| return setInterval(f, interval); | |||
| }; | |||
| // 结束定时器的循环调用 | |||
| Stomp.clearInterval = function (id) { | |||
| Stomp.clearInterval = function(id) { | |||
| return clearInterval(id); | |||
| }; | |||
| @@ -262,17 +266,17 @@ App({ | |||
| that.globalData.socketClient = stompClient; | |||
| stompClient.connect({}, function (callback) { | |||
| stompClient.connect({}, function(callback) { | |||
| // 订阅自己的 | |||
| stompClient.subscribe('/user/' + config.weapp.AppId + '/' + that.globalData.openId + '/message', function (message, headers) { | |||
| stompClient.subscribe('/user/' + config.weapp.AppId + '/' + that.globalData.openId + '/message', function(message, headers) { | |||
| console.log('收到只发送给我的消息:', message); | |||
| that.globalData.socketReceiver(JSON.parse(message.body)); | |||
| // 通知服务端收到消息 | |||
| message.ack(); | |||
| }); | |||
| stompClient.subscribe('/topic/' + config.weapp.AppId, function (message, headers) { | |||
| stompClient.subscribe('/topic/' + config.weapp.AppId, function(message, headers) { | |||
| console.log('收到TOPIC的消息:', message); | |||
| that.globalData.socketReceiver(JSON.parse(message.body)); | |||
| // 通知服务端收到消息 | |||
| @@ -287,7 +291,7 @@ App({ | |||
| }, | |||
| //统一发送消息 | |||
| sendSocketMessage: function (msg) { | |||
| sendSocketMessage: function(msg) { | |||
| this.globalData.socketClient.send("/app/message", {}, JSON.stringify(msg)); | |||
| } | |||
| }); | |||
| }); | |||