175 lines
4.4 KiB
JavaScript
175 lines
4.4 KiB
JavaScript
import request from "./request";
|
||
let version_number;
|
||
|
||
function getVersionNum() {
|
||
// 获取当前app的版本
|
||
const systemInfo = uni.getSystemInfoSync();
|
||
// 应用程序版本号
|
||
// 条件编译,只在APP渲染
|
||
// #ifdef APP
|
||
version_number = systemInfo.appWgtVersion;
|
||
// #endif
|
||
// #ifdef H5
|
||
version_number = systemInfo.appVersion;
|
||
console.log(systemInfo.appVersion, '版本号');
|
||
// #endif
|
||
// #ifdef MP-WEIXIN
|
||
const accountInfo = uni.getAccountInfoSync();
|
||
version_number = accountInfo.miniProgram.version // 小程序 版本号
|
||
console.log(accountInfo.miniProgram.version, '小程序版本号')
|
||
// #endif
|
||
|
||
return version_number;
|
||
}
|
||
// 检查版本自动更新
|
||
function getUpdateInfo() {
|
||
return new Promise(async (resolve, reject) => {
|
||
const systemInfo = await uni.getSystemInfoSync();
|
||
// 获取手机型号
|
||
const deviceBrand = systemInfo.deviceBrand;
|
||
console.log('deviceBrand', deviceBrand)
|
||
let type = deviceBrand == 'apple' ? '1' : deviceBrand == 'huawei' ? '2' : deviceBrand == 'honor' ?
|
||
'3' : deviceBrand == 'xiaomi' ? '4' : deviceBrand == 'oppo' ? '5' : deviceBrand == 'vivo' ?
|
||
'6' : '';
|
||
if (!type) return;
|
||
const response = await request.post('/sj/getAppVersion', {
|
||
type
|
||
});
|
||
console.log('response', response)
|
||
resolve({
|
||
version: response.data.version,
|
||
downloadUrl: response.data.url // APK下载地址
|
||
})
|
||
})
|
||
}
|
||
|
||
function installApp(downloadUrl) {
|
||
// 下载APK
|
||
const downloadTask = uni.downloadFile({
|
||
url: 'https://app.mrrweb.com.cn/mrrapp.com.1.1.4.apk',
|
||
success: (downloadResult) => {
|
||
console.log('downloadResult', downloadResult)
|
||
if (downloadResult.statusCode === 200) {
|
||
// 安装APK
|
||
plus.runtime.install(downloadResult.tempFilePath, {
|
||
force: false
|
||
}, () => {
|
||
console.log('安装成功')
|
||
// plus.runtime.restart();
|
||
}, (err) => {
|
||
console.log('err', err)
|
||
uni.showToast({
|
||
title: '安装失败',
|
||
icon: 'none'
|
||
})
|
||
})
|
||
}
|
||
},
|
||
fail(err) {
|
||
console.log('err', err)
|
||
}
|
||
})
|
||
|
||
// 监听下载进度
|
||
downloadTask.onProgressUpdate((res) => {
|
||
console.log('下载进度:', res.progress)
|
||
|
||
})
|
||
}
|
||
// 建议的版本比较函数
|
||
function compareVersion(current, latest) {
|
||
const curParts = current.split('.').map(Number);
|
||
const latParts = latest.split('.').map(Number);
|
||
|
||
for (let i = 0; i < Math.max(curParts.length, latParts.length); i++) {
|
||
const cur = curParts[i] || 0;
|
||
const lat = latParts[i] || 0;
|
||
if (cur < lat) return true;
|
||
if (cur > lat) return false;
|
||
}
|
||
return false;
|
||
}
|
||
// app内跟新下载
|
||
function uploadAppDowlond(url) {
|
||
const downloadTask = uni.downloadFile({
|
||
url: 'http://app.mrrweb.com.cn/mrrapp.com.1.1.4.apk',
|
||
success: downloadResult => {
|
||
if (downloadResult.statusCode === 200) {
|
||
plus.runtime.install(
|
||
downloadResult.tempFilePath, {
|
||
force: false
|
||
},
|
||
function() {
|
||
// 下载资源成功,重启应用
|
||
plus.runtime.restart()
|
||
},
|
||
function(e) {
|
||
// 下载资源失败
|
||
}
|
||
)
|
||
}
|
||
}
|
||
})
|
||
|
||
downloadTask.onProgressUpdate((res) => {
|
||
console.log('下载进度' + res.progress);
|
||
console.log('已经下载的数据长度' + res.totalBytesWritten);
|
||
console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
|
||
|
||
// 满足测试条件,取消下载任务。
|
||
// if (res.progress > 50) {
|
||
// downloadTask.abort();
|
||
// }
|
||
});
|
||
}
|
||
|
||
function showUpdateDialog(updateInfo) {
|
||
uni.showModal({
|
||
title: `发现新版本`,
|
||
content: '是否更新最新版本?',
|
||
confirmText: '立即更新',
|
||
cancelText: '稍后再说',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
// #ifdef APP-PLUS
|
||
// if (plus.os.name === 'iOS') {
|
||
// plus.runtime.openURL(updateInfo.downloadUrl)
|
||
// } else {
|
||
// installApp(updateInfo.downloadUrl)
|
||
// }
|
||
plus.runtime.openURL(updateInfo.downloadUrl)
|
||
// #endif
|
||
}
|
||
}
|
||
})
|
||
}
|
||
export default {
|
||
async uploadApp(type) {
|
||
// uploadAppDowlond()
|
||
try {
|
||
const currentVersion = getVersionNum();
|
||
if (!currentVersion) return
|
||
|
||
let updateInfo
|
||
// #ifdef APP-PLUS
|
||
updateInfo = await getUpdateInfo()
|
||
// #endif
|
||
console.log('updateInfo', updateInfo)
|
||
if (compareVersion(currentVersion, updateInfo.version)) {
|
||
showUpdateDialog(updateInfo)
|
||
} else {
|
||
if (type == 'onLaunch') return;
|
||
uni.showToast({
|
||
title: '当前已是最新版本',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
} catch (error) {
|
||
console.error('检查更新失败:', error)
|
||
uni.showToast({
|
||
title: '检查更新失败',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
}
|
||
} |