<template>
	<view class="content">
		<view class="up-wrap">
			<image src="../../static/img/upapp.png" mode="" class="upapp-bg"></image>
			<view class="tit">
				新版本升级
			</view>
			<view class="val">
				客户端体验优化
			</view>
			<view class="btn" @click="upApp">
				{{is_up?(schedule + '%'): '马上更新'}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				schedule: 0,
				is_up: false,
			}
		},
		onLoad() {
			
		},
		onShow() {
			
		},
		onBackPress() {
			return true
		},
		methods: {
			upApp() {
				let _this = this
				plus.runtime.getProperty(plus.runtime.appid, function(info) {
					console.log(info,'info');
					_this.$httpSend({
						path: '/im/app/update',
						data: {
							appid: info.appid,
							version: info.version,
						},
						success(res) {
							if (res.status) {
								// _action.checkFail();
								_this.is_up = true
								// let wgtWaiting = plus.nativeUI.showWaiting("更新开始下载"),
								let	update_url = (plus.os.name == 'Android' ? res.update_url.android : res
										.update_url.ios),
									downloadTask = uni.downloadFile({
										url: update_url,
										success: (res) => {
											// wgtWaiting.close();
											if (res.statusCode === 200) {
												plus.runtime.install(res.tempFilePath, {}, () => {
													plus.runtime.restart();
												}, (e) => {
													uni.showModal({
														content: "update error [" + e
															.code + "]：" + e.message,
														showCancel: false,
													});
												});
											} else {
												uni.showModal({
													content: "api error update fail!",
													showCancel: false,
												});
											}
										}
									});
								downloadTask.onProgressUpdate((res) => {
									console.log(res,'res+++++++++');
									_this.schedule = res.progress
									// wgtWaiting.setTitle('下载中...' + res.progress + '%');
								});
							}
						},
					});
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.content {
		position: absolute;
		height: 100%;
		width: 100%;
		background-color: #7f7f7f;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.up-wrap {
		width: 522rpx;
		height: 499rpx;
		background: #FFFFFF;
		
		border-radius: 28rpx;
		position: relative;
		.upapp-bg {
			width: 345rpx;
			height: 307rpx;
			position: absolute;
			left: 0;
			right: 0;
			top: -150rpx;
			margin: auto;
		}
		.tit {
			margin-top: 190rpx;
			font-size: 39rpx;
			font-weight: 500;
			color: #FF443F;
			text-align: center;
		}
		.val {
			font-size: 31rpx;
			font-weight: 500;
			color: #A5A5A5;
			text-align: center;
			margin-top: 10rpx;
		}
		.btn {
			width: 357rpx;
			height: 83rpx;
			background: #FF443F;
			border-radius: 41rpx;
			text-align: center;
			line-height: 83rpx;
			font-size: 34rpx;
			font-weight: 500;
			color: #FFFFFF;
			margin: 10rpx auto 0;
		}
	}
</style>