228 lines
6.5 KiB
JavaScript
228 lines
6.5 KiB
JavaScript
import { getLastData } from '../api/user.js'
|
||
|
||
export const checkAppUpdate = () => {
|
||
// 开发环境跳过检查
|
||
const isDev = process.env.NODE_ENV === 'development'
|
||
// if (isDev) {
|
||
console.log('开发环境,不执行更新检查')
|
||
return
|
||
// }
|
||
|
||
// 获取当前应用信息
|
||
plus.runtime.getProperty(plus.runtime.appid, (info) => {
|
||
const currentVersion = info.version
|
||
|
||
getLastData()
|
||
.then((res) => {
|
||
|
||
// let res = {
|
||
// data: {
|
||
// versionName: 'v1.6.83',
|
||
// forceUpdate: '1',
|
||
// androidPath: 'https://app.liuyingyong.cn/build/download/3c26e400-3a33-11f1-8997-a16e76fa35b3',
|
||
// // androidPath: 'http://112.4.144.18:8040/shiningCloud/file/canneng_wulian.apk',
|
||
// iosPath: 'xxxx',
|
||
// },
|
||
// }
|
||
if (!res?.data) {
|
||
console.log('未获取到版本信息')
|
||
return
|
||
}
|
||
|
||
// 适配新的接口返回格式
|
||
const { versionName, androidPath, iosPath, forceUpdate = '1' } = res.data
|
||
|
||
// 版本相同则不需要更新
|
||
if (versionName.includes(currentVersion)) {
|
||
console.log('已是最新版本')
|
||
return
|
||
}
|
||
|
||
const isForce = forceUpdate === '1' // 字符串 '1' 表示强制更新
|
||
|
||
const iosUrl = iosPath
|
||
|
||
handleUpdate({ version: versionName, androidPath, iosUrl, isForce })
|
||
})
|
||
.catch((err) => {
|
||
console.error('获取版本接口失败', err)
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 处理更新逻辑
|
||
*/
|
||
const handleUpdate = ({ version, androidPath, iosUrl, isForce }) => {
|
||
const isAndroid = plus.os.name === 'Android'
|
||
const isIOS = plus.os.name === 'iOS'
|
||
|
||
if (isAndroid) {
|
||
handleAndroidUpdate({ androidPath, isForce })
|
||
} else if (isIOS) {
|
||
handleIOSUpdate({ iosUrl, isForce })
|
||
} else {
|
||
console.warn('未知操作系统')
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理安卓更新
|
||
*/
|
||
const handleAndroidUpdate = ({ androidPath, isForce }) => {
|
||
if (!androidPath?.length) {
|
||
console.error('未找到安卓安装包')
|
||
uni.showToast({
|
||
title: '更新包不存在',
|
||
icon: 'error',
|
||
})
|
||
return
|
||
}
|
||
|
||
const downloadUrl = androidPath
|
||
|
||
uni.showModal({
|
||
title: '更新提示',
|
||
content: '发现新版本,是否立即更新?',
|
||
showCancel: !isForce, // 强制更新隐藏取消按钮
|
||
confirmText: '去更新',
|
||
cancelText: '暂不更新',
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
downloadAndInstallApk(downloadUrl)
|
||
} else if (isForce) {
|
||
// 强制更新且用户取消,退出应用
|
||
plus.runtime.quit()
|
||
}
|
||
},
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 处理iOS更新
|
||
*/
|
||
const handleIOSUpdate = ({ iosUrl, isForce }) => {
|
||
if (!iosUrl) {
|
||
console.error('未找到iOS下载链接')
|
||
uni.showToast({
|
||
title: '更新链接不存在',
|
||
icon: 'error',
|
||
})
|
||
return
|
||
}
|
||
|
||
uni.showModal({
|
||
title: '更新提示',
|
||
content: '发现新版本,请前往 App Store 更新',
|
||
showCancel: !isForce,
|
||
confirmText: '去更新',
|
||
cancelText: '暂不更新',
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
plus.runtime.openURL(iosUrl)
|
||
}
|
||
|
||
// 强制更新时,无论确认还是取消都退出应用
|
||
if (isForce) {
|
||
setTimeout(() => {
|
||
plus.runtime.quit()
|
||
}, 300) // 给跳转留一点时间
|
||
}
|
||
},
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 下载并安装APK(安卓专用)
|
||
*/
|
||
const downloadAndInstallApk = (url) => {
|
||
// 显示原生进度条
|
||
let progressWaiting = plus.nativeUI.showWaiting('正在下载中,请稍等...', {
|
||
modal: true,
|
||
round: true,
|
||
close: false, // 不允许用户关闭
|
||
padlock: true, // 锁定屏幕
|
||
})
|
||
|
||
const options = {
|
||
filename: '_doc/update/canneng_wulian.apk',
|
||
timeout: 120,
|
||
}
|
||
|
||
const downloadTask = plus.downloader.createDownload(url, options, (downloadedFile, status) => {
|
||
progressWaiting.close()
|
||
|
||
if (status === 200) {
|
||
installApk(downloadedFile.filename, url)
|
||
} else {
|
||
handleDownloadError(url)
|
||
}
|
||
})
|
||
|
||
// // 更新进度
|
||
downloadTask.addEventListener('statechanged', (task) => {
|
||
if (task.state === 3 && task.totalSize > 0) {
|
||
const percent = ((task.downloadedSize / task.totalSize) * 100).toFixed(0)
|
||
console.log("🚀 ~ downloadAndInstallApk ~ percent:", percent)
|
||
// 直接更新 waiting 的标题,不会闪烁
|
||
progressWaiting.setTitle(`正在下载更新 ${percent}%`)
|
||
}
|
||
})
|
||
|
||
downloadTask.start()
|
||
}
|
||
|
||
/**
|
||
* 安装APK
|
||
*/
|
||
const installApk = (filePath, downloadUrl) => {
|
||
console.log('🚀 ~ installApk ~ filePath:', filePath)
|
||
plus.runtime.install(
|
||
filePath,
|
||
{ force: true },
|
||
() => {
|
||
// 安装成功
|
||
uni.showModal({
|
||
title: '安装成功',
|
||
content: '是否立即重启应用?',
|
||
showCancel: false,
|
||
confirmText: '立即重启',
|
||
success: () => {
|
||
plus.runtime.restart()
|
||
},
|
||
})
|
||
},
|
||
(error) => {
|
||
console.error('安装失败', error)
|
||
uni.showModal({
|
||
title: '安装失败',
|
||
content: `安装失败:${error.message}\n请检查是否已开启安装权限`,
|
||
confirmText: '重试',
|
||
cancelText: '取消',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
downloadAndInstallApk(downloadUrl)
|
||
}
|
||
},
|
||
})
|
||
},
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 处理下载错误
|
||
*/
|
||
const handleDownloadError = (downloadUrl) => {
|
||
uni.showModal({
|
||
title: '下载失败',
|
||
content: '网络异常或下载链接失效,是否重试?',
|
||
confirmText: '重试',
|
||
cancelText: '取消',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
downloadAndInstallApk(downloadUrl)
|
||
}
|
||
},
|
||
})
|
||
}
|