在uni中打开如下模块
<uses-permission android:name=\"android.permission.INSTALL_PACKAGES\"/>
<uses-permission android:name=\"android.permission.REQUEST_INSTALL_PACKAGES\"/>
php部分
public function Index ($appid, $version) {
//系统信息
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
// IOS
if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
{
}
// 安卓
if(strpos($agent, 'android'))
{
$arr = [];
if($version != "2.0.24"){
$arr = [
"status" => 1, // 1,需要升级; 0,无需升级
"msg" => "v2.0.24:1、新增秒视频相关功能",
"url" => "http://upapp.city1000.cn/ap2024.apk",
"exe" => "安卓"
];
}
return json_encode($arr);
}
}
uni部分
<template>
<view>
<view class="updateBox bs" v-show="up_show">
<view class="title">有新的版本!</view>
<view class="content">
<view class="up_msg">
{{up_msg}}
</view>
</view>
<view class="button">
<view class="btn no" v-show="up_no_show" @click="closeUp()">取消更新</view>
<view class="btn ok" @click="lijiUp()">{{up_ok_msg}}</view>
</view>
</view>
</view>
</template>
<script>
data () {
return {
'up_msg': '1、啊啊啊;2、2222',
'up_show': false,
'up_no_show': true,
'up_ok_msg': '立即更新',
'up_url': '',
'up_exp': '安卓'
}
},
onLoad (options) {
var ts = this;
// 判断是否需要升级
var appid = plus.runtime.appid
var version = plus.runtime.version
uni.request({
url: ts.url+"/api/anping_app/update/"+appid+"/"+version,
method: "GET",
success: (res) => {
console.log('更新')
var res_ = res.data
console.log(res)
if (res.data.status === 1) {
// 需要更新
ts.up_url = res.data.url
ts.up_msg = res.data.msg
ts.up_exe = res.data.exe
// 打开弹窗
ts.up_show = true
}
}
})
},
methods: {
// 立即更新
lijiUp () {
var that = this
if(that.up_exe == "安卓"){
this.up_ok_msg = '下载中...'
this.up_no_show = false
// 下载
var dtask = plus.downloader.createDownload(that.up_url, {}, function(d, status){
// 下载完成
if(status == 200){
that.up_ok_msg = '即将自动更新'
console.log("Download success: " + d.filename);
setTimeout(function(){
// 安装
plus.runtime.install(d.filename, {
force: false
}, function() {
//进行重新启动;
plus.runtime.restart();
}, (e) => {
uni.showToast({
title: '安装升级包失败:'+JSON.stringify(e),
icon: 'none'
})
});
}, 1500);
} else {
this.tui.toast("下载升级包失败,请手动去站点下载安装,错误码: " + status);
console.log("Download failed: " + status);
}
});
// 启动
dtask.start();
}
if(that.up_exp == "苹果"){
}
},
// 取消更新
closeUp () {
this.up_show = false
}
}
</script>
<style>
.updateBox{
width: 540rpx;
background-color: #fff;
padding: 0 30rpx 30rpx 30rpx;
position: fixed;
left: 0;
right: 0;
top: 40%;
margin: auto;
z-index: 10000;
border-radius: 20rpx;
}
.updateBox .title{
display: flex;
justify-content: center;
align-items: center;
font-size: 34rpx;
height: 70rpx;
}
.updateBox .content .up_msg{
padding: 40rpx;
font-size: 28rpx;
}
.updateBox .button{
display: flex;
justify-content: center;
align-items: center;
}
.updateBox .button .btn{
margin: 0 20rpx;
width: 200rpx;
height: 70rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 28rpx;
border-radius: 10rpx;
}
.updateBox .button .no{
background-color: #ccc;
color: #fff;
}
.updateBox .button .ok{
background-color: #EE7854;
color: #fff;
}
</style>