<template>
	<view class="app">
		<image src="../../static/logo.png" mode=""></image>
		<view class="title">
			智慧卫监职卫云
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getLastAppFileInfo
	} from '@/api/startup/index.js'
	export default {
		data() {
			return {
				apknumber: null,
			};
		},
		onShow() {
			//自动更新
			var that = this;
			plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
				console.log(wgtinfo.version)
				this.apknumber = wgtinfo.version
				getLastAppFileInfo().then(res => {
					console.log(res)
					if (res.code == 200) {
						if (res.data) {
							if (res.data.fileVersion) {
								if (res.data.fileVersion > this.apknumber) {
									uni.showToast({
										title: '有新的版本发布，程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
										mask: false,
										duration: 5000,
										icon: "none"
									});
									var dtask = plus.downloader.createDownload(
										res.data.fileUrl, {},
										function(d, status) {
											//下载完成  
											if (status == 200) {
												plus.runtime.install(plus.io.convertLocalFileSystemURL(
													d
													.filename), {}, {}, function(error) {
													uni.showToast({
														title: '安装失败',
														mask: false,
														duration: 1000
													});
												})
											} else {
												uni.showToast({
													title: '更新失败',
													mask: false,
													duration: 1000
												});
											}
										});
									try {
										dtask.start(); // 开启下载的任务
										var prg = 0;
										var showLoading = plus.nativeUI.showWaiting(
											"正在下载"); //创建一个showWaiting对象 
										dtask.addEventListener('statechanged', function(
											task,
											status
										) {
											// 给下载任务设置一个监听 并根据状态  做操作
											switch (task.state) {
												case 1:
													showLoading.setTitle("正在下载");
													break;
												case 2:
													showLoading.setTitle("已连接到服务器");
													break;
												case 3:
													prg = parseInt(
														(parseFloat(task.downloadedSize) /
															parseFloat(task.totalSize)) *
														100
													);
													showLoading.setTitle("  正在下载" + prg + "%  ");
													break;
												case 4:
													plus.nativeUI.closeWaiting();
													//下载完成
													break;
											}
										});
									} catch (err) {
										plus.nativeUI.closeWaiting();
										uni.showToast({
											title: '更新失败',
											mask: false,
											duration: 1000
										});
										that.info();
									}
								} else {
									that.info();
								}
							} else {
								that.info();
							}
						} else {
							that.info();
						}
					} else {
						that.info();
					}

				})
			})
		},
		methods: {
			info() {
				var that = this
				try {
					const value = uni.getStorageSync('token');
					if (value) {
						setTimeout(() => {
							// uni.switchTab({
							// url: '/pages/homepage/index',
							// })
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}, 1000);
					} else {
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}, 1000);
					}
				} catch (e) {
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						});
					}, 1000);
				}
			}
		},
		onLoad() {

		},
	}
</script>

<style lang="scss">
	image {
		width: 50%;
		height: 380rpx;
		position: absolute;
		left: 25%;
		top: 20%;
	}

	.title {
		width: 80%;
		height: 48rpx;
		font-size: 49rpx;
		text-align: center;
		font-family: DengXian;
		font-weight: bold;
		color: #000000;
		position: absolute;
		top: 50%;
		left: 10%;
	}
</style>
