- 根因: simpleDemo/libs 缺 install-apk-release.aar(提取 SDK 时漏拷), plus.runtime.install 走静默安装路径失败且不回落 UI 安装器 - 补齐 install-apk-release.aar(与散单工程一致) - AndroidManifest 补 INSTALL_PACKAGES/REQUEST_INSTALL_PACKAGES 权限(与散单权限集一致) - update.js: installUpdatePackage 改为无参调用 plus.runtime.install(filePath)(带 options/回调会走静默安装分支),下载恢复默认路径 - login.vue: 检查更新优先于鉴权跳转(updateChecked/pendingAutoEnter 门控,检测完成前挂起自动进入打包模式) - 版本号 1.0.19(用户已改) - 真机端到端验证: 1.0.18 降级包 → 更新弹窗 → 下载 → 系统安装器弹出 → 安装成功 1.0.19
142 lines
4.4 KiB
JavaScript
142 lines
4.4 KiB
JavaScript
import { appEnv } from '@/config/env'
|
|
|
|
/** 三段版本比较:a > b 返回 1,a < b 返回 -1,相等返回 0 */
|
|
export function compareVersion(a, b) {
|
|
const pa = String(a || '').split('.').map((x) => Number(x) || 0)
|
|
const pb = String(b || '').split('.').map((x) => Number(x) || 0)
|
|
for (let i = 0; i < 3; i++) {
|
|
const x = pa[i] || 0
|
|
const y = pb[i] || 0
|
|
if (x > y) return 1
|
|
if (x < y) return -1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* 检查是否有新版本(读取发布服务器 latest.json 与当前版本对比)
|
|
* 返回 null:无更新 / 不支持平台 / 网络失败;
|
|
* 否则 { version, url, sha256, canSkip }:canSkip===1 可选更新,否则(0 或缺失)强制更新
|
|
*/
|
|
export function checkAppUpdate() {
|
|
return new Promise((resolve) => {
|
|
const url = appEnv.updateLatestUrl
|
|
if (!url) {
|
|
resolve(null)
|
|
return
|
|
}
|
|
uni.request({
|
|
// 加时间戳绕过 CDN/浏览器缓存,确保拿到最新发布信息
|
|
url: `${url}${url.indexOf('?') >= 0 ? '&' : '?'}_t=${Date.now()}`,
|
|
method: 'GET',
|
|
timeout: 8000,
|
|
success: (res) => {
|
|
try {
|
|
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
|
const latestVersion = data && data.version
|
|
if (latestVersion && compareVersion(latestVersion, appEnv.version) > 0) {
|
|
resolve({
|
|
version: latestVersion,
|
|
url: data.url || '',
|
|
sha256: data.sha256 || '',
|
|
canSkip: data.canSkip === 1 ? 1 : 0,
|
|
})
|
|
return
|
|
}
|
|
resolve(null)
|
|
} catch (error) {
|
|
resolve(null)
|
|
}
|
|
},
|
|
fail: () => resolve(null),
|
|
})
|
|
})
|
|
}
|
|
|
|
/** 格式化下载大小 */
|
|
export function formatUpdateSize(bytes) {
|
|
const num = Number(bytes) || 0
|
|
if (num <= 0) {
|
|
return '0 KB'
|
|
}
|
|
const mb = num / 1024 / 1024
|
|
if (mb >= 1) {
|
|
return `${mb.toFixed(1)} MB`
|
|
}
|
|
return `${Math.round(num / 1024)} KB`
|
|
}
|
|
|
|
/**
|
|
* 内置下载更新安装包(带进度)
|
|
* - Electron:主进程下载(https + 重定向),通过 electronAPI 回传进度
|
|
* - App:plus.downloader(默认下载目录)
|
|
* resolve(本地文件路径)
|
|
*/
|
|
export function startUpdateDownload(url, onProgress) {
|
|
return new Promise((resolve, reject) => {
|
|
const safeProgress = onProgress || (() => {})
|
|
|
|
if (appEnv.isElectron && typeof window !== 'undefined' && window.electronAPI && window.electronAPI.downloadUpdate) {
|
|
const off = window.electronAPI.onDownloadProgress((data) => {
|
|
safeProgress(data.received || 0, data.total || 0)
|
|
})
|
|
window.electronAPI
|
|
.downloadUpdate(url)
|
|
.then((filePath) => {
|
|
if (off) off()
|
|
resolve(filePath)
|
|
})
|
|
.catch((error) => {
|
|
if (off) off()
|
|
reject(error)
|
|
})
|
|
return
|
|
}
|
|
|
|
if (appEnv.isApp && typeof plus !== 'undefined') {
|
|
const task = plus.downloader.createDownload(
|
|
url,
|
|
{},
|
|
(download, status) => {
|
|
if (status === 200) {
|
|
const filePath = download.filename || ''
|
|
console.log('[update] 下载完成:', filePath)
|
|
resolve(filePath)
|
|
} else {
|
|
console.error('[update] 下载失败 status:', status)
|
|
reject(new Error(`download failed: ${status}`))
|
|
}
|
|
}
|
|
)
|
|
task.addEventListener('statechanged', (download) => {
|
|
if (download.state === 3) {
|
|
safeProgress(download.downloadedSize || 0, download.totalSize || 0)
|
|
}
|
|
})
|
|
task.start()
|
|
return
|
|
}
|
|
|
|
reject(new Error('当前平台不支持内置下载'))
|
|
})
|
|
}
|
|
|
|
/** 安装已下载的安装包 */
|
|
export function installUpdatePackage(filePath) {
|
|
if (appEnv.isElectron && typeof window !== 'undefined' && window.electronAPI && window.electronAPI.openInstaller) {
|
|
window.electronAPI.openInstaller(filePath)
|
|
return
|
|
}
|
|
if (appEnv.isApp && typeof plus !== 'undefined') {
|
|
console.log('[update] plus.runtime.install:', filePath)
|
|
// 与散单项目一致:只传路径,不传 options 与回调。
|
|
// 实测传了 options/回调时 5+ Runtime 走静默安装路径(应用进程内 PackageParser 解析 APK),
|
|
// 无系统安装权限时 error 回调收到 null 且不回落 UI 安装器 → 打不开安装包;
|
|
// 不传回调则走系统 UI 安装器(弹出安装界面)。
|
|
plus.runtime.install(filePath)
|
|
return
|
|
}
|
|
}
|
|
|
|
export default checkAppUpdate
|