From 76fefb30c4a2ef41a2ab1a3635bd0ff6c7373799 Mon Sep 17 00:00:00 2001 From: wangfukang <15630117759@163.com> Date: Wed, 4 Nov 2026 20:18:33 +0800 Subject: [PATCH] 1 --- App.vue | 92 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/App.vue b/App.vue index 4db70db..5156797 100644 --- a/App.vue +++ b/App.vue @@ -99,30 +99,7 @@ const baseUrl = getApp().globalData.mainurl //这里是热更新的代码 // #ifdef APP-PLUS - uni.request({ - url: baseUrl + '/setting/app-update/latest', - data: {}, - success: (res) => { - var data = res.data.result; - if (data.url) { - uni.downloadFile({ - url: data.url, - success: (downloadResult) => { - if (downloadResult.statusCode === 200) { - plus.runtime.install(downloadResult.tempFilePath, { - force: false - }, function() { - console.log('install success...'); - plus.runtime.restart(); - }, function(e) { - console.error('install fail...', e); - }); - } - } - }); - } - } - }); + this.checkAppUpdate(baseUrl) // #endif // this.getRegistrationID() // #ifdef APP-PLUS @@ -260,6 +237,73 @@ }); } }, + checkAppUpdate(baseUrl) { + plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => { + const currentVersion = widgetInfo && widgetInfo.version ? widgetInfo.version : plus.runtime.version + uni.request({ + url: baseUrl + '/setting/app-update/latest', + data: {}, + success: (res) => { + const body = res.data || {} + const data = body.result || {} + console.log('app update response:', JSON.stringify(data), 'current:', currentVersion) + if (!data.url) { + console.log('app update skipped: url is empty') + return + } + if (data.version && this.compareVersion(data.version, currentVersion) <= 0) { + console.log('app update skipped: current version is latest', currentVersion, data.version) + return + } + const downloadUrl = this.appendUpdateQuery(data.url, data.version) + this.downloadAndInstallWgt(downloadUrl) + }, + fail: (err) => { + console.error('app update request fail:', JSON.stringify(err)) + } + }) + }) + }, + downloadAndInstallWgt(url) { + uni.downloadFile({ + url: url, + success: (downloadResult) => { + console.log('app update download status:', downloadResult.statusCode, downloadResult.tempFilePath) + if (downloadResult.statusCode !== 200) { + console.error('app update download fail status:', downloadResult.statusCode) + return + } + plus.runtime.install(downloadResult.tempFilePath, { + force: true + }, function() { + console.log('app update install success...') + plus.runtime.restart() + }, function(e) { + console.error('app update install fail...', JSON.stringify(e)) + }) + }, + fail: (err) => { + console.error('app update download fail:', JSON.stringify(err)) + } + }) + }, + appendUpdateQuery(url, version) { + if (!url) return '' + const query = 'v=' + encodeURIComponent(version || Date.now()) + return url + (url.indexOf('?') === -1 ? '?' : '&') + query + }, + compareVersion(remoteVersion, currentVersion) { + const remote = String(remoteVersion || '').split('.') + const current = String(currentVersion || '').split('.') + const len = Math.max(remote.length, current.length) + for (let i = 0; i < len; i++) { + const r = parseInt(remote[i] || '0', 10) + const c = parseInt(current[i] || '0', 10) + if (r > c) return 1 + if (r < c) return -1 + } + return 0 + }, getPushInAppExtras(result) { if (!result) return ''; if (this.NB.isAndroidOrIOS() == 'iOS') {