import { getVersion } from "@/api"; const updateVersion = { data() { return { updateVersion: { modalShow: false, showConfirmButton: false, showCancelButton: false, confirmText: "", cancelText: "", modalContent: "", confirmType: "", newVersionUrl: "", downloadCompleteTemporaryPath: "", }, }; }, methods: { async fnUpdateVersion(toast) { // #ifdef APP-PLUS const resData = await getVersion(); if (resData.pd) { // console.log("升级版本:" + resData.pd.VERSION + " -> " + Number(resData.pd.VERSION.replace(/\./g, ""))); // console.log("运行版本:" + plus.runtime.version + " -> " + Number(plus.runtime.version.replace(/\./g, ""))); // console.log( Number(resData.pd.VERSION.replace(/\./g, "")) > Number(plus.runtime.version.replace(/\./g, ""))); if ( Number(resData.pd.VERSION.replace(/\./g, "")) > Number(plus.runtime.version.replace(/\./g, "")) ) { this.updateVersion.newVersionUrl = this.$filePath + resData.pd.FILEURL; uni.getNetworkType({ success: ({ networkType }) => { if (networkType === "wifi") { this.updateVersion.modalContent = "发现新版本,是否更新?为了更好的体验,请更新到最新版本。"; } else { this.updateVersion.modalContent = "发现新版本,检查到您当前使用的是移动网络,是否更新?更新时请注意流量消耗。为了更好的体验,请更新到最新版本。"; } this.updateVersion.confirmType = "download"; this.updateVersion.confirmText = "立即更新"; this.updateVersion.cancelText = "稍后更新"; this.updateVersion.showConfirmButton = true; this.updateVersion.showCancelButton = true; this.updateVersion.modalShow = true; uni.hideTabBar({ animation: true, }); }, }); } else { if (toast !== false) { uni.showToast({ title: "当前已是最新版本,当前版本号:" + plus.runtime.version, icon: "none", }); } } } else { if (toast !== false) { uni.showToast({ title: "当前已是最新版本,当前版本号:" + plus.runtime.version, icon: "none", }); } } // #endif }, modalConfirm() { this.updateVersion.confirmType === "download" ? this.downloadNewVersion() : this.installNewVersion(); }, modalCancel() { this.updateVersion.modalShow = false; uni.showTabBar({ animation: true, }); }, downloadNewVersion() { this.updateVersion.showConfirmButton = false; this.updateVersion.showCancelButton = false; this.updateVersion.modalContent = "正在下载新版本,请稍后..."; const downloadTask = uni.downloadFile({ url: this.updateVersion.newVersionUrl, success: (res) => { this.updateVersion.downloadCompleteTemporaryPath = res.tempFilePath; }, }); downloadTask.onProgressUpdate((res) => { this.updateVersion.modalContent = `正在下载新版本,请稍后...
已经下载${res.progress}%`; if (res.progress === 100) { this.updateVersion.showConfirmButton = true; this.updateVersion.confirmType = "install"; this.updateVersion.confirmText = "立即安装"; this.updateVersion.modalContent = "下载成功,点击立即安装进行安装。"; } }); }, installNewVersion() { plus.runtime.install( this.updateVersion.downloadCompleteTemporaryPath, { force: true, }, function () { uni.showTabBar({ animation: true, }); plus.runtime.restart(); }, ); }, }, }; export default updateVersion;