<!-- 初始化判断页面 -->
<template>
	<view>
		<view class="loading-container-bg">
			<!-- <image mode="aspectFill" src="../../../static/loading.jpg"></image> -->
		</view>
	</view>
</template>

<script>
	import config from '../../../common/config.js';
	import reqUtil from "@/util/util-js/requstUtil.js";
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {}
		},
		computed: mapState(['bNewUser']),
		onLoad() {},
		onShow() {
			let bCanLoading = false;
			//验证登录存储
			//如果登录过，直接跳转登录页面
			const value = uni.getStorageSync('token');
			var _self = this;
			if (value) {
				console.log("验证登录Token值:",value,config.URL.VERIFICATION);
				reqUtil.requestData(config.URL.VERIFICATION, {
					token: value
				}).then(res => {
						console.log('requestData VERIFICATION =====', res);

						if (res.code == 0) {
							// TODO token时效性问题
							_self.$store.state.token = value;
							_self.$store.state.bNewUser = res.data.newUser;
							_self.accountLogin(function() {
								// console.log("======");
								if (_self.bNewUser) {
									_self.onNavTo("userInfo");
								} else {
									_self.onNavTo("personal");
								}

							});
							

						} else {
							//清空保存的storage
							uni.clearStorageSync();
							//如果token 不合法，不存在，跳转登录页面
							_self.onNavTo("login");
						}
					},
					e => {
						console.log(e)
					});
			} else {
				//如果没有token，进入登录界面
				_self.onNavTo("login");
			}
			
			//预加载
			// uni.preloadPage({url: "../../platform/app-plus/subNVue/guide"});
		},
		methods: {
			...mapMutations(['accountLogin']),
			onNavTo(navType) {
				// uni.hideToast();
				console.log('当前loading 跳转的页面是 navType:',navType);
				if (navType == "userInfo") {
					uni.redirectTo({
						url: "../../my-page/userInfo/userInfo"
					})
				} else if (navType == "personal") {
					// uni.redirectTo({
					// uni.switchTab({
					// 	url: "../../personal-page/personal/personal"
					// })
					uni.reLaunch({
						url: "../../personal-page/personal/personal"
					})
				} else {
					uni.redirectTo({
						url: "../../login-page/login/login"
					})
				}

			}
		}
	}
</script>

<style>
	.loading-container-bg {
		position: absolute;
		left: 0;
		top: 0;
		bottom: 0;
		right: 0;
		width: 750rpx;
		height: 100%;
	}

	.loading-container-bg image {
		width: 100%;
		height: 100%;
	}
</style>
