<template>
	<view class="app">
		<image src="../../static/logo.png" mode=""></image>
		<view class="title">
			泉医助手
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		createMobileToken
	} from '@/api/login/index.js'
	import {
		appFileInfoByOneId
	} from '@/api/startup/index.js'
	export default {
		data() {
			return {
				apknumber: null,
			};
		},
		onShow() {
			createMobileToken().then(res => {
				uni.setStorageSync("token", res.data.token)
			})
			//自动更新
			var that = this;
			plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
				that.apknumber = wgtinfo.version
				appFileInfoByOneId().then(res => {
					if (res.fileVersion > that.apknumber) {
						uni.showToast({
							title: '有新的版本发布，程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
							mask: false,
							duration: 5000,
							icon: "none"
						});
						var dtask = plus.downloader.createDownload(
							res.fileUrl, {},
							function(d, status) {
								//下载完成  
								if (status == 200) {
									plus.runtime.install(plus.io.convertLocalFileSystemURL(d
										.filename), {}, {}, function(error) {
										uni.showToast({
											title: '安装失败',
											mask: false,
											duration: 1500
										});
									})
								} else {
									uni.showToast({
										title: '更新失败',
										mask: false,
										duration: 1500
									});
								}
							});
						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: 1500
							});
							that.info();
						}
					} else {
						that.info();
					}
				})
			})
		},
		methods: {
			info() {
				var that = this
				const nursePersonId = uni.getStorageSync("nursePersonId")
				if (!nursePersonId) {
					uni.reLaunch({
						url: "/pages/login/login"
					})
				} else {
					uni.switchTab({
						url: "/pages/homepage/homepage"
					})
				}
			}
		},
	}
</script>

<style lang="scss">
	.app {
		height: 100vh;
		background-color: #fff;
	}

	image {
		width: 390rpx;
		height: 380rpx;
		position: absolute;
		top: 28%;
		margin-left: 50%;
		transform: translateX(-50%);
	}

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