uniapp 开发app代码版本升级

思路:

app:后端给个接口前端,前端传版本号给后端,后端返回一个是否升级状态以及安装包的地址。
pc端:一个列表接口(查看所有版本的),还有输入版本号、上传apk保存的接口。

//可以在登录后进行判断
<u-modal v-model="showUpdate" title="检测到有新版本" :content="content" :async-close="true" @confirm="confirm"
			confirm-text="开始下载">
			<view v-if="percent !== 0">
				<u-line-progress :striped="true" :percent="percent" :striped-active="true"
					type="primary"></u-line-progress>
			</view>
		</u-modal>
export default {
	data() {
		return {
 
			appInformation: {
				version: ''//保存版本号
			},
			appAddress: '',//保存后端返回的地址
			showUpdate: false,//是否更新
			content: null,
			percent: 0,//显示进度条

		}
	},	
onLoad: function () {
		this.updateApp()
	},
	methods: {
		updateApp() {
			plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
				// 	console.log(JSON.stringify(wgtinfo)); //客户端详细数据
				// 	console.log(wgtinfo.version); //应用版本号
				this.appInformation.version = wgtinfo.version
				this.$http.post("接口", this.appInformation).then(res => {
			
					console.log('返回是否升级版本', res.data);
					if (res.data.code == 200) {
						this.appAddress = res.data.message//这里是下载apk的整个路径
						if (res.data.message !== '已经是最新版本') {
							this.content = '请升级app到最新版本!'
							this.showUpdate = true
						}
					}

				})
			})
		},
        //点击确定,下载安装包
		confirm() {
			this.content = '下载APP版本中,请勿关闭App!'
			const downloadTask = uni.downloadFile({
				// 下载资源包
				url: this.appAddress,
				success: (downloadResult) => {
					console.log('下载资源包', downloadResult)
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath,
							{
								// 安装资源包
								force: false
							},
							function () {
								plus.runtime.restart() // 重启APP
							},
							function (e) { }
						)
					}
				}
			})
			downloadTask.onProgressUpdate((res) => {
				const { progress } = res
				this.percent = progress//这里是进度条
			})
		},
		

	}
}

pc 端页面,后端做逻辑处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值