1.html代码
<uni-popup ref="versionUpdate" :mask-click="false" type="center">
<view class="version-content">
<view class="version-content-title">
版本更新提示
</view>
<view class="version-content-text">
<view class="version-content-text-edition">
发现新版本{{versionName}}
</view>
<view class="version-content-text-tips">
优化 | 修复部分已知问题,优化功能体验
</view>
</view>
<view class="version-content-button">
<view class="version-content-button-cancel" @click="cancelVersionUpdate">
暂不更新
</view>
<view class="version-content-button-confirm" @click="confirmVersionUpdate">
立即更新
</view>
</view>
</view>
</uni-popup>
2.data定义
data() {
return {
privacyShow: false,
account: '',
password: '’',
isRememberAccount: false, // 是否记住用户名密码
device: {}, // 设备信息
jsonData: {}, // 入库数据
appVersion: '', // app版本号
// 可更新APP版本号
versionName: '',
// 版本更新下载地址:
downloadUrl: ''
};
},
3.调用方法
this.chkVersion()
4.方法
chkVersion() {
// const server = "http://116.52.158.139:9998";//服务器
// const server = "http://192.168.16.147:8080";//本地服务器
const server = "http://116.52.158.139:9997"; //本地调试
// const server = "http://192.168.16.166:8085"; //配合xlj调试
var checkVersionUrl = server + '/api/anon/checkVersion'; //检查版本更新地址
// 检测更新
let url = `${checkVersionUrl}?appid=${plus.runtime.appid}&version=${plus.runtime.version}`
let that = this
uni.request({
url: url,
method: 'GET',
success: (res) => {
if (res.data.code == '200') {
var updateFlag = res.data.data.updateFlag;
var appVersion = res.data.data.appVersion;
that.versionName = appVersion.versionName
that.downloadUrl = appVersion.downloadUrl
if (updateFlag == '1'&& that.$updateStatus === '0') {
that.$refs.versionUpdate.open('center')
}
} else {
uni.showToast({
icon: 'none',
title: '数据异常'
})
}
},
fail: (err) => {
uni.showToast({
icon: 'none',
title: '连接失败'
})
}
});
},
5.//关闭版本更新弹窗
cancelVersionUpdate() {
Vue.prototype.$updateStatus = '1'
this.$refs.versionUpdate.close()
},
6. // 更新版本
confirmVersionUpdate() {
this.$refs.versionUpdate.close()
uni.showLoading({
title: '正在下载...'
});
// 应用内下载app
uni.downloadFile({
url: 'http://116.52.158.139:9997' +
this.downloadUrl,
success: (res) => {
uni.hideLoading();
if (res.statusCode === 200) {
// 调用第三方程序打开指定的临时文件
plus.runtime.openFile(res
.tempFilePath, {},
function() {
uni.showToast({
title: '安装失败',
duration: 2000
});
});
}
}
});
},