直接上代码不墨迹
wgtUrlupload(){
let _this = this;
var urlfun = 你自己后台服务器的接口方法 判断是否需要升级
plus.screen.lockOrientation('portrait-primary');
console.log("onLaunch锁定屏幕方向")
plus.runtime.getProperty(plus.runtime.appid, function(info) {
getApp().globalData.editionnum = info.version;
uni.request({
url: urlfun,
method:"POST",
data: {
"appid": plus.runtime.appid,
"version": plus.runtime.version,
"system":uni.getStorageSync('systemname'),
"editionnum":info.version,
},
success: (res) => {
console.log("AJAX数据请求接口--返回状态--"+JSON.stringify(res.data));
var data = res.data;
var status = data.status;
if(status == 200){
_this.checkVersionToLoadUpdate(info.version,data);
}else{
uni.showToast({
title: data.msg,
duration: 1500
});
}
}
});
})
},
checkVersionToLoadUpdate:function(server_version,data){
if(server_version !== data.edition){
if(plus.networkinfo.getCurrentType()!=3){
uni.showToast({
title: '有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新',
mask: true,
duration: 5000,
icon:"none"
});
return;
}else{
uni.showModal({
title: "版本更新",
content: '有新的版本发布,检测到您当前为Wifi连接,是否立即进行新版本下载?',
confirmText:'立即更新',
cancelText:'稍后进行',
success: function (res) {
if (res.confirm) {
uni.showToast({
icon:"none",
mask: true,
title: '有新的版本发布,检测到您目前为Wifi连接,程序已启动自动更新。新版本下载完成后将自动弹出安装程序',
duration: 5000,
});
var downloadApkUrl = data.uplodurl;
var dtask = plus.downloader.createDownload( downloadApkUrl, {}, function ( d, status ) {
if ( status == 200 ) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){
uni.showToast({
title: '安装失败',
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
duration: 1500
});
}
});
dtask.start();
} else if (res.cancel) {
console.log('稍后更新');
}
}
});
}
}
},
## PHP 服务器后台方法
function EditionUploadapp(){
$appid = $this->request->param('appid');
$system = $this->request->param('system');
$editionnum = $this->request->param('editionnum');
if($appid == 'HBuilder'){
if ($system == 'Android') {
$where['type'] = 1;
# code...
}if ($system == 'iOS') {
$where['type'] = 2;
# code...
}
$look = db::name($this->appedition)->where($where)->order("id desc")->find();
if ($look) {
$data['status'] = 200;
$data['edition'] = $look['edition'];
$data['uplodurl'] = $look['url'];
$data['msg'] = "数据查询成功";
# code...
}else{
$data['status'] = 100;
$data['msg'] = "数据查询失败";
}
}else{
$data['status'] = 100;
$data['msg'] = "appid 序列号不匹配";
}
return json($data);
}