const update = { check : function(path, call) { var self = this; var source = this.source; if (source != 'app') { return; } var type = plus.os.name.toLowerCase(); self.post('isUpdate', {t:1}, function(res) { if (type == 'android') { res.downloadUrl = res.androidUrl; } else { res.downloadUrl = res.iosUrl; } self.data('dever_update_link', res.downloadUrl); plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { var version = widgetInfo.versionCode; var state = false; state = widgetInfo.versionCode < res.versionCode; if (state) { //强制更新 var txt = '正在为您下载更新,下载完成将重启应用'; if (res.forceUpdate == true) { self.down(res, res.downloadUrl, txt, type); } else if(call) { call(res, txt, type); } else { uni.showModal({ title: '发现新版本', content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?', success: (t) => { if (t.confirm) { if (path) { self.location(path); } else { self.down(res, res.downloadUrl, txt, type); } } } }) } } }); }); }, updateCall : function(config, type, packgePath) { // 保存更新记录到stroage,下次启动app时安装更新 var self = this; self.data('dever_update', 1); self.data('dever_update_down', packgePath); // 任务完成,关闭下载任务,开始安装 plus.runtime.install(packgePath, {force: true}, function() { self.data('dever_update', 2); self.data('dever_update_down', 'del'); uni.showModal({ title: '提示', content: '应用将重启以完成更新', showCancel: false, complete: () => { plus.runtime.restart(); } }) }); }, down : function(config, url, txt, type, call) { if (!url) { return false; } if (txt) { this.alert(txt); } if (type != 'android' && !url.match(RegExp(/.wgt/))) { plus.runtime.openURL(url); return false; } var self = this; var packgePath = self.data('dever_update_down'); if (packgePath) { self.updateCall(config, type, packgePath); return false; } var downloadTask = uni.downloadFile({ url: url, success: (res) => { if (res.statusCode === 200) { // 保存下载的安装包 uni.saveFile({ tempFilePath: res.tempFilePath, success: (res) => { var packgePath = res.savedFilePath; if (call) { call(); } self.updateCall(config, type, packgePath); downloadTask.abort(); downloadTask = null; } }) } } }); return downloadTask; }, } export default update;