mall-app-t/peach/platform/provider/android/index.js

96 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2024-06-17 18:16:46 +08:00
const device = uni.getSystemInfoSync()
/**
* @author Ankkaya
* @description 检查更新
* @param {String} newVersion - 最新版本
* @returns {Type}
*/
function checkUpdate(newVersion) {
plus.runtime.getProperty(plus.runtime.appid, (wgtInfo) => {
if (String(device.platform).toLowerCase() === 'android') {
console.log(wgtInfo)
if (wgtInfo.version !== newVersion) {
uni.showModal({
title: '温馨提示',
content: '发现新版本APP您是否要升级体验?',
cancelText: '暂时忽略',
confirmText: '立即升级',
success: (action) => {
if (action.confirm) {
// 下载 app
downLoadApp()
} else {
// 忽略
}
},
})
}
}
})
}
function downLoadApp() {
let percent = 0
let downnUrl = import.meta.env.MALL_APP_DOWNLOAD_PATH
let dtask = plus.downloader.createDownload(downnUrl, {}, (d, status) => {
if (status == 200) {
let path = d.filename
plus.runtime.install(path, {}, () => {})
} else {
uni.showToast({
title: '下载失败',
icon: 'none',
})
}
})
try {
dtask.start()
let showLoading = plus.nativeUI.showWaiting('正在下载...')
dtask.addEventListener('statechanged', (download, status) => {
switch (download.state) {
case 1:
showLoading.setTitle('正在下载...')
break
case 2:
showLoading.setTitle('已连接到服务器')
break
case 3:
percent = parseInt((parseFloat(download.downloadedSize) / parseFloat(download.totalSize)) * 100)
showLoading.setTitle(`正在下载` + percent + '%')
break
case 4:
plus.nativeUI.closeWaiting()
break
}
})
} catch (e) {
plus.nativeUI.closeWaiting()
uni.showToast({
title: '更新失败!',
icon: 'none',
})
}
}
/**
* @author Ankkaya
* @description 本地版本
* @param {Type} -
* @returns {Type}
*/
function getVersion() {
let version = ''
plus.runtime.getProperty(plus.runtime.appid, (wgtInfo) => {
version = wgtInfo.version
})
return version
}
export default {
checkUpdate,
downLoadApp,
getVersion,
}