热更新的弹出框
热更新的下载进度条, 进度条用的是uView2.0的组件
filters.js文件源码
function sizeMB(size){ if(size<1024){ return size+'B'; }else if(size/1024>=1 && size/1024/1024<1){ return Math.floor(size/1024*100)/100+'KB'; }else if(size/1024/1024>=1){ return Math.floor(size/1024/1024*100)/100+'MB'; } } export default { sizeMB }
data里声明的变量
// 热更新 hotRefresh() { const that = this; //plus.runtime.getProperty(),获取本机安装的版本,具体使用请参照如下链接 plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { // 将本机版本号存起来,然后与接口返回的版本号进行对比 uni.setStorageSync('AndroidVersion', widgetInfo.version); console.log('本地缓存的版本号', widgetInfo.version) // 请求接口 that.Req('AppVersion/renewApp', { yj_atype: 1 }, 'POST').then(res => { console.log('res', res) console.log('接口返回的版本号', res.data.yj_version) if (res.code == 200) { let onlineAndroidVersion = res.data.yj_version.split(".") // 获取本机安装版本转化为数组 let localAndroidVersion = widgetInfo.version.split(".") let isUpload = false; for (var i = 0; i < onlineAndroidVersion.length; i++) { // 服务端版本是否大于本地版本 if (parseInt(onlineAndroidVersion[i]) > parseInt(localAndroidVersion[i])) { isUpload = true; } } that.upLoadUrl = res.data.yj_path; that.androidVersion = res.data.yj_version; that.renewContent = res.data.yj_desc; that.isUpdate = res.data.yj_type; if (isUpload == true) { // 下载文件资源到本地,下载刚才打包的 .wgt文件 uni.hideTabBar(); // 立即更新的弹窗 that.renewShow = true } } }).catch(err => { console.log('请求失败', err) }) }) },
// 立即更新 onClickNowRenew() { this.renewShow = false this.updateBarShow = true console.log('立即更新upLoadUrl', this.upLoadUrl) this.downWgt() }, downWgt() { let _this = this; this.downloadTask = uni.downloadFile({ url: _this.upLoadUrl, success: (downloadResult) => { console.log('downloadResult', downloadResult) if (downloadResult.statusCode === 200) { // 安装应用 plus.runtime.install(downloadResult.tempFilePath, { force: false }, () => { uni.setStorageSync('AndroidVersion', _this.androidVersion); _this.updateBarShow = false _this.renewShow = false uni.showTabBar(); plus.nativeUI.toast('最新版本下载完成') // 安装成功之后重启 plus.runtime.restart(); }, (e) => { _this.updateBarShow = false uni.showTabBar(); plus.nativeUI.toast("补丁下载失败") }); } }, fail: (err) => { _this.updateBarShow = false uni.showTabBar(); plus.nativeUI.toast("补丁下载失败") } }) this.downloadTask.onProgressUpdate((res) => { console.log('当前下载进度', res) // 当前下载进度 if (this.time % 10 == 0) { this.schedule = res this.schedule.totalBytesExpectedToWrite = filters.sizeMB(res.totalBytesExpectedToWrite) this.schedule.totalBytesWritten = filters.sizeMB(res.totalBytesWritten) } this.time += 1 }); }, onClickCancel() { this.renewShow = false uni.showTabBar(); },