diff --git a/components/AppUpdateDialog/AppUpdateDialog.vue b/components/AppUpdateDialog/AppUpdateDialog.vue index a214ed4..1f98cbd 100644 --- a/components/AppUpdateDialog/AppUpdateDialog.vue +++ b/components/AppUpdateDialog/AppUpdateDialog.vue @@ -66,6 +66,9 @@ export default { data() { return { updateLogSeq: 0, + // 远端更新配置地址。注意:当前为 http 明文,Android 9+ 默认禁止明文流量, + // 若真机请求 latest.json 失败,建议与主接口(https://api.youzixy.com)保持一致改用 https。 + updateConfigUrl: 'http://dl.wms.gztbyc.com/pda/latest.json', currentAppVersion: '', showUpdateDialog: false, isCheckingUpdate: false, @@ -96,7 +99,7 @@ export default { /** * 检查更新并弹窗 * @param {Boolean} force 为 true 时忽略 hasCheckedUpdate,强制重新检查远端配置 - * @returns {Promise} + * @returns {Promise} 是否发现新版本并弹窗;检查失败时返回 false 且 updateErrorMessage 有值 */ async checkForAppUpdate(force = false) { this.logUpdateStep('checkForAppUpdate -> 开始执行', { force }); @@ -108,12 +111,12 @@ export default { hasCheckedUpdate: this.hasCheckedUpdate, isCheckingUpdate: this.isCheckingUpdate }); - return; + return false; } if (typeof plus === 'undefined' || !plus.runtime) { this.logUpdateStep('checkForAppUpdate -> 非 APP-PLUS 环境,跳过更新检查'); - return; + return false; } this.hasCheckedUpdate = true; @@ -136,9 +139,15 @@ export default { }); this.logUpdateStep('checkForAppUpdate -> 标准化后的远端配置', latestInfo || {}); + if (!currentVersion) { + this.logUpdateStep('checkForAppUpdate -> 未获取到当前应用版本,无法比较', { currentVersion }); + this.updateErrorMessage = '获取当前应用版本失败'; + return false; + } + if (!latestInfo || !latestInfo.version || !latestInfo.url) { this.logUpdateStep('checkForAppUpdate -> 远端配置缺少必要字段,结束', latestInfo || {}); - return; + return false; } const isNewer = this.isVersionNewer(latestInfo.version, currentVersion); @@ -150,7 +159,7 @@ export default { if (!isNewer) { this.logUpdateStep('checkForAppUpdate -> 远端版本不高于当前版本,不弹窗'); - return; + return false; } this.updateInfo = { @@ -162,9 +171,12 @@ export default { }; this.showUpdateDialog = true; this.logUpdateStep('checkForAppUpdate -> 发现新版本,显示更新弹窗', this.updateInfo); + return true; } catch (error) { this.logUpdateStep('checkForAppUpdate -> 检查异常', error); console.warn('[update] check failed:', error); + this.updateErrorMessage = error?.message || '更新检查失败'; + return false; } finally { this.isCheckingUpdate = false; this.logUpdateStep('checkForAppUpdate -> 结束,isCheckingUpdate=false'); @@ -172,11 +184,11 @@ export default { }, fetchLatestVersionInfo() { this.logUpdateStep('fetchLatestVersionInfo -> 发起请求', { - url: 'http://dl.wms.gztbyc.com/pda/latest.json' + url: this.updateConfigUrl }); return new Promise((resolve, reject) => { uni.request({ - url: 'http://dl.wms.gztbyc.com/pda/latest.json', + url: this.updateConfigUrl, method: 'GET', timeout: 10000, success: (res) => { @@ -202,7 +214,8 @@ export default { }, fail: (error) => { this.logUpdateStep('fetchLatestVersionInfo -> 请求失败', error); - reject(error || new Error('更新配置获取失败')); + const message = error?.errMsg || error?.message || '更新配置获取失败'; + reject(new Error(message)); } }); }); @@ -237,22 +250,35 @@ export default { return; } + let settled = false; + const timer = setTimeout(() => { + if (settled) return; + settled = true; + this.logUpdateStep('getCurrentAppVersion -> getProperty 回调超时,返回空版本'); + resolve(''); + }, 3000); + const done = (version) => { + if (settled) return; + settled = true; + clearTimeout(timer); + resolve(version); + }; + try { + // 注意:plus.runtime.version 是 HTML5+ 运行时版本(如 3.x),不是应用版本, + // 数值可能比应用版本大,若用作比较会导致「永远认为已是最新、不弹窗」。 + // 因此这里只取 info.version(manifest 的 versionName),读不到就返回空。 plus.runtime.getProperty(plus.runtime.appid, (info) => { - const version = String(info?.version || plus.runtime.version || '').trim(); + const version = String(info?.version || '').trim(); this.logUpdateStep('getCurrentAppVersion -> 读取成功', { version, appid: plus.runtime.appid }); - resolve(version); + done(version); }); } catch (error) { - const fallbackVersion = String((typeof plus !== 'undefined' && plus.runtime && plus.runtime.version) || '').trim(); - this.logUpdateStep('getCurrentAppVersion -> 读取异常,走兜底版本', { - fallbackVersion, - error - }); - resolve(fallbackVersion); + this.logUpdateStep('getCurrentAppVersion -> 读取异常,返回空版本', { error }); + done(''); } }); }, diff --git a/pages/settings/about.vue b/pages/settings/about.vue index c4766bd..6e5e27a 100644 --- a/pages/settings/about.vue +++ b/pages/settings/about.vue @@ -27,22 +27,87 @@ - - + + + + + 发现新版本 + 当前 {{ currentAppVersion || '--' }} → 最新 {{ updateInfo.version || '--' }} + + + + + 安装包 + {{ updateInfo.file || '未提供文件名' }} + + + 发布时间 + {{ formatUpdateTime(updateInfo.publishedAtUtc) }} + + + 检测到新版本,确认后将开始下载安装包。 + 下载完成后会自动打开文件。 + + + + + 正在下载更新 + {{ updateDownloadProgress }}% + + + + + + + {{ updateErrorMessage }} + + + + + + + +