<template>
	<!-- 版本更新提示升级 -->
	<view class="upbox" v-if="isUpdate">
		<view class="content">
	<!-- 		<view class="upimg">
				<image class="img" src="/static/img/updata.png"></image>
			</view> -->
			<view class="versioninfo">
				<view class="title">更新提示</view>
				<view class="content">有新版本发布，请及时更新！</view>
				<view class="schedule" v-if="isSchedule">	
					<progress :percent="upProgress" activeColor="#49b856" show-info stroke-width="15" />
				</view>
				<view class="btn">
					<button size="mini" :disabled="isSchedule" @click="nowUpdate()">立即更新</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
	  get,
	  post
	} from "../../http/http.js";
	export default {
		name:"appUpdata",
		data() {
			return {
				isUpdate:false,
				// 服务器图片地址
				// baseUrl: configSetting.baseUrl,
				isSchedule: false,
				upProgress: 0,
				Version: true,
				// manifest 中应用版本名称
				appVersion: '',
				// manifest 中应用版本号
				apkInfo: {},
				apkUrl:'',
				updateContent: []
			};
		},
		created() {
			this.getProperty()
		},
		methods: {
			getProperty(){
				// 获取本地应用资源版本号
				let that = this
				uni.getSystemInfo({
					success: function (res) {
						// console.log(7777,res)
						that.appVersion = res.appVersion
						that.getVersionNumber();
					}
				});
				
				// plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
				// 	this.appVersion = wgtinfo.version;
				// 	this.getVersionNumber();
				// })
			},
			// 获取后台版本号 检查是否有新版本
			getVersionNumber() {
				uni.showLoading({
				  title: "加载中",
				});
				let postData = {
				  "idx": 0,
				  "sum": 1,
				  systemConfiguration: {
				    keyconfig: "supportAppVersion"
				  }
				}
				post("/simple/data/fetchDataList/systemConfiguration", postData).then((res) => {
				  uni.hideLoading();
					if (res.status == 200) {
				    this.supportAppVersion = res.list[0].valueconfig
						let ip = uni.getStorageSync('ip')
						this.apkUrl = ip + `/getapk/pdazV${this.supportAppVersion}.apk`
						console.log(444,this.apkUrl)
						if(this.supportAppVersion != this.appVersion){
							this.isUpdate = true;
						}else{
							this.isUpdate = false;
						}
				  }
				})
				
			},
			// 点击立即升级
			nowUpdate() {
				this.androidUpdate();
			},
			// 安卓更新
			androidUpdate() {
				// 浏览器安装
				// plus.runtime.openURL(this.apkUrl, function(res) {
				// 	console.log(res);  
				// }); 
				const downloadTask = uni.downloadFile({
					url: this.apkUrl, //apk下载链接
					success: (res) => {
						if (res.statusCode !== 200) {
							uni.showToast({
								title: '下载安装包失败',
								icon: 'error',
								duration: 2000
							});
							this.isSchedule = false;
							this.isUpdate = false;
							return;
						}
						// 异步请求
						if (res.statusCode === 200) {
							console.log('更新成功');
						// 更新好直接安装，下次启动生效
						plus.runtime.install(res.tempFilePath, {
							force: true
						}, () => {
							// uni.showModal({
							// 	title: '安装成功是否重启？',
							// 	success: res => {
							// 		if (res.confirm) {
							// 			//更新完重启app
							// 			plus.runtime.restart();
							// 		}
							// 	}
							// });				
						}, err => {
							uni.showToast({
								title: '更新失败',
								icon: 'error',
								duration: 2000
							});
						})
					 }	
					},
					fail: (err) => {
						console.log('err',err);
					},
					//接口调用结束 调用成功、失败都会执行
					complete: ()=>{
						downloadTask.offProgressUpdate();//取消监听加载进度
					}
				});
				//监听下载进度
				downloadTask.onProgressUpdate(res => {
					this.isSchedule = true;
					this.upProgress = res.progress;
					if(res.progress >= 100) {
						this.isSchedule = false;
						this.isUpdate = false;
						this.$emit('getUpdate');
					}
				});
			}
		},
		
	}
</script>

<style scoped lang="scss">
.upbox {
	position: fixed;
	z-index: 999;
	top: 0;
	right: 0;
	width: 100%;
	height: 100vh;
	background-color: rgba(153, 153, 153, 0.75);
	display: flex;
	align-items: center;
}	
.content {
	margin: 0 5%;
	width: 90%;
	text-align: center;
	.upimg {
		height: 420rpx;
		width: 100%;
		.img {
			height: 100%;
			width: 100%;
		}
	}
	.versioninfo {
		// padding: 15rpx 0rpx;
		border-radius: 25rpx;
		background-color: #fff;
		.title {
			font-size: 36rpx;
			// border-bottom: 1px solid #eee;
			padding: 20rpx 0;
		}
		.content{
			
		}
	}
	.info {
		position: relative;
		padding-left: 38rpx;
		view {
			height: 50rpx;
			line-height: 50rpx;
		}
	}
	.schedule {
		margin: 15rpx;
		/deep/.uni-progress-bar {  
		    border-radius: 30rpx;  
		}  
		/deep/.uni-progress-inner-bar {  
		    border-radius: 30rpx;  
		}
	}
	.btn {
		height: 110rpx;
		margin-top: 30rpx;
		display: flex;
		justify-content: space-evenly;
		button {
			width: 46%;
			height: 75rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #49b856;
			color: #fff;
		}
	}
}
</style>
