<template>
 
	<view class="">
		<!-- 升级弹框 -->
		<u-popup :show="show" mode="center" width="500rpx" style="background-color: transparent;">
			<view class="" style="text-align: center;width: 500rpx;">
				<view class="" style='position: relative;'>
					<image style="width: 500rpx;height:700rpx;"
						src="http://icc-oss.oss-cn-hangzhou.aliyuncs.com/app/czzn-app/update1.png"
						mode="">
					</image>
					<view class="flex-col"
						style="position: absolute;bottom: 60rpx;width: 100%;justify-content: flex-end;align-items: center;height: 250rpx;">
						<view class="white lg"
							style="width: 80%;height: 80rpx;line-height: 80rpx;text-align: center;background:linear-gradient(to bottom,#FF7B7B,#FF4E4E);border-radius: 50rpx; color:#fff"
							@click="downloadPackage"> 立即升级
						</view>
					</view>
 
				</view>
 
				<!-- <image style="width: 80rpx;height:80rpx;margin-top: 40rpx;"
					src="https://wuzhongjie.oss-cn-beijing.aliyuncs.com/uniapp_web/wuzhongjie/videoPart/update2.png"
					mode="" @click="cancle"></image> -->
			</view>
		</u-popup>
 
		<!-- 下载弹框 -->
		<u-popup :show="show_update" mode="center" width="500rpx">
			<view
				style="background-color: #fff;height: 200rpx;border-radius: 30rpx;padding:20rpx 20rpx;text-align: center;">
				<view class="" style="margin: 30rpx;">
					下载中...
				</view>
				<u-line-progress :percentage="progress" :showText="false"/>
			</view>
		</u-popup>
 
 
	</view>
</template>
<script>
	export default {
		data() {
			return {
				show: false,
				show_update: false,
				show: false,
				content: '11',
				show1: false,
				content1: '11',
				version: '',
				hot_version: '',
				progress: 0,
				new_ver: '',
				new_down_url: '',
				ios_down_url:"",
				update_content:'',
			}
		},
		mounted() {
			console.log('wgt包啵啵啵啵啵啵啵啵啵')
				// this.show = true
			this.isUpdate()
		},
		methods: {
			cancle() {
				this.show = false
			},
			getLocalVersion() { //获取当前版本号
				return new Promise((resolve, reject) => {
					plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						console.log(widgetInfo, 'wgt包啵啵啵啵啵啵啵啵啵')
						this.version = widgetInfo.version
						this.hot_version = widgetInfo.versionCode
					  resolve(this.version)
					})
				})
			},
			async isUpdate() { //后端获取最新版本号，版本号对比
				var ver = await this.getLocalVersion();
        console.log('当前版本号',ver);
        uni.request({
          url: 'http://zhijian.chinayimatong.com/admin/sys/app/download/getDownloadList/1808782670845468674',
          method: 'get',
        }).then(res => { //判断手机类型
					console.log('接口版本号', res[1].data)
          let data = res[1].data.data
					if (ver !== data.version) { //大版本更新
						if (uni.getSystemInfoSync().platform == 'android') {
								this.show = true
								this.new_down_url = data.androidDownload
						} else { 
								this.show = true
								this.ios_down_url = data.iosDownload
						}
					}else{
						this.$emit('noNeedUpdate','')
					}
				}).catch(()=>{
						this.$emit('noNeedUpdate','')
				})
			},
			downloadPackage() { //确认升级版本-新建下载任务
				this.show = false
				if(this.new_down_url==''){   //苹果大版本更新去应用商店
					uni.navigateTo({
						url:this.ios_down_url
					})
					
				}else{
				  var dtask = plus.downloader.createDownload(this.new_down_url, {}, function(d, status) {
					if (status == 200) { // 下载完成-进行安装
						console.log('下载status', d, status)
						// console.log("Download success: " + d.filename);
						this.show_update = false
						plus.runtime.install(d.filename, {
							force: true
						}, function() {
							plus.runtime.restart();
						}, function(e) {
							console.log(e)
							plus.nativeUI.toast("安装失败");
						});
					} else {
						console.log("Download failed: " + status);
					}
				});
				this.onProgress(dtask)
				dtask.start();}
			},
			onProgress(task) { //  获取下载进度
				console.log('下载中onProgress');
				this.show_update = true
				task.addEventListener('statechanged', e => {
					console.log('下载中', e)
					if (e && e.downloadedSize > 0) {
						let progress = ((e.downloadedSize / e.totalSize) * 100).toFixed(0)
						console.log('下载中111', progress)
						this.progress = progress
					}
				}, false)
			}
		},
	}
</script>
<style lang="less" scoped>
.flex-col{
	display: flex;
	flex-direction: column;
}
</style>