思路如下,供新手参考
检测升级部分代码
upapp: function(isup) {
//#ifdef APP-PLUS
/* 5+环境锁定屏幕方向 */
plus.screen.lockOrientation('portrait-primary'); //锁定
/* 5+环境升级提示 */
var server = ""; //检查更新地址
var req = { //升级检测数据
"appid": plus.runtime.appid,
"version": plus.runtime.version,
"imei": plus.device.imei
};
uni.request({
url: server,
data: req,
success: (res) => {
if (res.statusCode == 200 && res.data.state == "yes") {
let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;
var url = res.data.urlapp;
var content = res.data.content;
uni.showModal({ //提醒用户更新
title: '更新提示',
cancelText:'确定',
confirmText:'取消',
content: res.data.content ? res.data.content : '有新版本,是否确定更新?',
success: (res) => {
if (!res.confirm) {
//跳转到升级进度页面
uni.navigateTo({
url: "/pages/upapp/index?url=" + url + "&content=" + content
})
} else {
//取消
}
}
})
} else if (res.statusCode == 200) {
uni.setStorageSync("upapp", time);
uni.showToast({
"title": res.data.content ? res.data.content : "已是最新版本",
});
} else {
//
}
}
})
//#endif
}
下载升级进度页面:/pages/upapp/index
<template>
<view>
<view class="page-body">
{{title}} {{content}}
<view class="page-section page-section-gap">
<view class="progress-box">
<progress :percent="percent" show-info stroke-width="30" />
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '正在下载中,请稍候!',
content: "",
percent: 0
}
},
onLoad: function (option) {
if (option.url) {
this.content = option.content;
this.up_app(option.url);
} else {
uni.navigateBack({
delta: 1,
});
return;
}
},
onHide: function () {
},
onShow: function () {
},
methods: {
up_app: function (url) {
plus.screen.lockOrientation('portrait-primary'); //锁定
this.download(url)
},
download: function (url) {
console.log(url)
var that = this;
const downloadTask = uni.downloadFile({
url: url,
success: function (res) {
if (res.statusCode === 200) {
console.log(JSON.stringify(res))
console.log('下载成功');
var tempFilePaths = res.tempFilePath;
that.install(tempFilePaths);
return;
}
}
});
downloadTask.onProgressUpdate(function (res) {
that.percent = res.progress;
//console.log('下载进度' + res.progress);
//console.log('已经下载的数据长度' + res.totalBytesWritten);
//console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
});
},
install: function (path) {//安装
console.log(path)
plus.runtime.install(path, {
force: true
}, function () {
//uni.hideNavigationBarLoading();
console.log("加载完成!");
uni.navigateBack({
delta: 1,
});
}, function (e) {
console.log(JSON.stringify(e));
});
}
}
}
</script>
<style>
progress {
width: 100%;
}
.progress-box {
display: flex;
height: 50px;
margin-bottom: 60px;
}
.progress-cancel {
margin-left: 40px;
}
</style>
后端代码(php):
$version="";//是接收到的版本号
$now_v="1.0.1";//要升级最新版本号
$data_json=["state"=>"not","content"=>"已是最新版本!",];
if ($version !== $now_v) { //这里是示例代码,真实业务上,最新版本号及relase no
$data_json =array(
"state"=>"yes",
"content"=>"增加一些新功能。",
"mark"=>$now_v,
"urlapp"=>"",//下载app地址
);
}
4 个评论
要回复文章请先登录或注册
3***@qq.com
小Q (作者)
dyjx2004
maozai