1.封装热更新方法??
import {
httpRequest,
hostURL
} from '@/utils/httpRequest'
async function getNewVersion( success, fail) {
let params = {};
if (uni.getSystemInfoSync().platform == "ios") {
params = {
appType: "storeApp",
client: "ios",
};
} else {
params = {
appType: "storeApp",
client: "az",
};
}
var [err, res] = await uni.request({
url: hostURL + '/data-dist/api/dist/appHotUpdate/version',
data: params
});
if (res && success) {
success(res.data);
}
if (err && fail) {
fail(err);
}
}
function update(fileName) {
let downURL = hostURL + "/upload-app/" + fileName;
console.log('下载地址', downURL, hostURL);
//后台显示进度条
let dtask = plus.downloader.createDownload(
downURL, {},
function(d, status) {
//下载完成
uni.showModal({
title: "更新提示",
content: "新版本已经准备好,是否更新应用?",
success: (res) => {
if (res.confirm) {
install(d, status);
}
},
});
}
);
start(dtask);
};
function start(dtask) {
try {
dtask.start(); //开启下载任务
let prg = 0;
let showLoading = plus.nativeUI.showWaiting("正在下载");
dtask.addEventListener("statechanged", function(task, status) {
switch (task.state) {
case 1:
showLoading.setTitle("正在下载");
break;
case 2:
showLoading.setTitle("已连接到服务器");
break;
case 3:
prg = parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
);
showLoading.setTitle(" 正在下载" + prg + "% ");
break;
case 4:
plus.nativeUI.closeWaiting();
break;
}
});
} catch (err) {
plus.nativeUI.closeWaiting();
uni.showToast({
title: "更新失败-03",
mask: false,
duration: 1500,
});
}
};
function install(d, status) {
//下载完成
if (status === 200) {
plus.runtime.install(d.filename, {
force: false
}, function() {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("应用资源更新完成!是否重启应用!", function() {
plus.runtime.restart();
});
}, function(e) {
plus.nativeUI.closeWaiting();
console.log("安装wgt文件失败[" + e.code + "]:" + e.message);
plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]:" + e.message);
});
} else {
uni.showToast({
title: "下载失败",
mask: false,
duration: 1500,
});
}
};
function close(version) {
try {
console.log( '保存要跳过的版本', version)
uni.setStorageSync('skip_version', version);
} catch (e) {
// error
}
};
function signOut() {
// #ifdef APP-PLUS
plus.runtime.quit();
// #endif
}
export const getUpdate = function(oldversion, back) {
getNewVersion( function(res) {
console.log('获取热更新版本信息', res, oldversion, back);
if (res.succ == 1 && res.result) {
let result = res.result;
if(back){
try {
const value = uni.getStorageSync('skip_version');
console.log( '是否跳过当前版本',value, result.version)
if (value && value === result.version) {
return;
}
} catch (e) {
}
}
if (result.version != oldversion) {
console.log('有新版本要更新', result);
result.back = back;
plus.nativeUI.confirm(result.remark, function(e) {
if (e.index === 0) {
update(result.fileName);
} else if (result.strategy == '强制') {
signOut();
} else if (result.strategy == '推荐') {
close(result.version);
}
}, {
"title": "新版本",
"buttons": ["立即更新", result.strategy == '强制' ? "退出应用" : "暂不更新"]
});
} else if (!back) {
plus.nativeUI.alert("当前已是最新版本!");
}
}else if (!back) {
plus.nativeUI.alert("当前已是最新版本!");
}
});
}
2.调用热更新方法? (放在App.vue onLaunch生命周期中,在未登录之前就调用)
<style lang="scss">
/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
@import "uview-ui/index.scss";
</style>
<script>
import {getUpdate} from '@/utils/hotUpdate';
export default {
onLaunch: function() {
console.log('App Launch');
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
uni.setStorageSync("当前版本", widgetInfo.version);
let oldversion = widgetInfo.version;
console.log("我的版本login", oldversion);
getUpdate(oldversion, true);
});
// #endif
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
globalData: {
test: ''
},
methods: {
},
}
</script>
再附上后台管理热更新的图片
?
自己项目中的代码,真实有效!?
|