优化效果

This commit is contained in:
weipengfei 2023-09-04 18:28:02 +08:00
parent af0d12bcbd
commit 2cc38a10c4

View File

@ -3,6 +3,7 @@ const mp = uni.requireNativePlugin('uniMP');
let appid = ''; // 应用id let appid = ''; // 应用id
let wgtFile = ''; // 应用文件地址 let wgtFile = ''; // 应用文件地址
let timer = null; // 加载计时器
// 比较版本号大小 // 比较版本号大小
function compareVersions(version1, version2) { function compareVersions(version1, version2) {
@ -28,11 +29,23 @@ const loadMP = async (id) => {
console.log(mp, uni); console.log(mp, uni);
// return ; // return ;
uni.showLoading({ uni.showLoading({
title: '下载中...' title: '下载中...',
mask: true
}) })
mp.getUniMPVersion(id, (ret) => { mp.getUniMPVersion(id, (ret) => {
console.log('当前版本', ret); console.log('当前版本', ret);
if (0 != ret.code || compareVersions(info.data.version, ret.versionInfo.name) == 1 || true) { if (0 != ret.code || compareVersions(info.data.version, ret.versionInfo.name) == 1 || true) {
let count = 0;
timer = setInterval(() => {
if (count < 100) uni.showLoading({
title: `下载中... ${count}%`,
mask: true
})
else uni.showLoading({
title: '安装中...',
mask: true
})
}, 600)
let downloadTask = uni.downloadFile({ let downloadTask = uni.downloadFile({
url: info.data.version_info?.dow_url, url: info.data.version_info?.dow_url,
success(res) { success(res) {
@ -41,23 +54,12 @@ const loadMP = async (id) => {
installMP(); installMP();
} }
}); });
let count = 0;
let timer = null;
timer = setInterval(()=>{
uni.showLoading({
title: `下载中... ${count}%`,
mask: true
})
}, 600)
downloadTask.onProgressUpdate((res) => { downloadTask.onProgressUpdate((res) => {
console.log('下载进度' + res.progress); console.log('下载进度' + res.progress);
if (res.progress > count + 10) count += 10; if (res.progress > count) count += 10;
if (count >= 90) { if (count >= 90) {
clearInterval(timer); clearInterval(timer);
timer = null; timer = null;
uni.showLoading({
title: '安装中...'
})
} }
// 满足测试条件,取消下载任务。 // 满足测试条件,取消下载任务。
// if (res.progress > 50) { // if (res.progress > 50) {
@ -102,7 +104,6 @@ const doInstallMP = () => {
appid: appid, appid: appid,
wgtFile: wgtFile wgtFile: wgtFile
}, (r) => { }, (r) => {
uni.hideLoading();
if (0 == r.code) { if (0 == r.code) {
// uni.showToast({ // uni.showToast({
// title: '安装成功' // title: '安装成功'
@ -110,6 +111,7 @@ const doInstallMP = () => {
console.log('小程序安装成功'); console.log('小程序安装成功');
open(); open();
} else { } else {
uni.hideLoading();
uni.showModal({ uni.showModal({
title: '安装失败', title: '安装失败',
content: JSON.stringify(r), content: JSON.stringify(r),
@ -133,6 +135,7 @@ const open = (id = null) => {
token: token, token: token,
} }
}, (ret) => { }, (ret) => {
uni.hideLoading();
if (0 != ret.code) { if (0 != ret.code) {
uni.showModal({ uni.showModal({
title: '启动失败', title: '启动失败',