<template>
	<view>
		<u-loading-page loadingText="加载中...." :loading="true"></u-loading-page>
	</view>
</template>

<script>
	import apiLogin from '@/api/login.js'
	import { getToken,setToken } from '@/utils/token'
	export default{
		data(){
			return {
				toUrl:''
			}
		},
		onLoad(options) {
			this.toUrl=options.returnUrl;
			if(this.toUrl){
				if(this.toUrl=='/'){
					this.toUrl='';
				}
				var pvArray=Object.keys(options);
				var params='';
				pvArray.forEach(pv=>{
					if(pv!='returnUrl'){
						params+='&'+pv+'='+options[pv];
					}
				});
				this.toUrl=this.toUrl+params;
			}
			console.log(this.toUrl,'options.returnUrl');
		},
		mounted() {
			let token=getToken();
			let $this=this;
			let urlPre=window.location.protocol+'//'+window.location.host+this.$config.context;
			//console.log(token,'得到token');
			if(!token){
				console.log('未获得token,转向登录');
				//this.$router.push({ path: process.env.VUE_APP_LOGIN_URL })
				// #ifdef APP-PLUS
				plus.runtime.openURL(this.$config.loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开
				// #endif
				// #ifdef H5
				//console.log('this.toUrl',this.toUrl);
				//console.log('this.$config.loginPath',this.$config.loginPath);
				if(this.toUrl){
					location.href=this.$config.loginPath+"?returnUrl="+encodeURIComponent(urlPre+'/pages/login/loading?returnUrl='+encodeURIComponent(this.toUrl));
				}else{
					location.href=this.$config.loginPath+"?returnUrl="+encodeURIComponent(urlPre+'/pages/login/loading');
				}
				// #endif
			}else{
			  apiLogin.boeLogin(token).then(rs=>{
				if(rs.status==200){
				  //加载用户信息
				  this.$store.dispatch("InitData").then(res => {
					 //console.log(res)
					 //this.$watermark.set(res.result.name + res.result.loginName);
					 // uni.switchTab({
					 // 	url:'/pages/index/index'
					 // })
					 uni.setStorageSync("boe_new_login",1);//记录新登录
					 let openUrl=$this.toUrl;
					 //http://localhost:8081/mobile/pages/login/loading?returnUrl=/pages/study/courseStudy?id=1028700304503054336
					 if(openUrl){
						 //如果openUrl的页面不存在，或不对，就会出现跳转不动的问题，这个情况
						if(openUrl.startsWith('http')){
							location.href=openUrl
						}else{
							if(openUrl=='/pages/index/index' || openUrl=='/pages/study/index' || openUrl=='/pages/plus/index' || openUrl=='/pages/my/index'){
								uni.switchTab({
									url:openUrl
								})
							}else{
								uni.navigateTo({url:openUrl})
							}
						}
					 }else{
						uni.switchTab({
						 	url:'/pages/index/index'
						})
					 }
					 
				  });						
				}else{
					console.log(rs.message+','+rs.error);
					//#ifdef APP-PLUS
					plus.runtime.openURL(this.$config.loginPath) //这里默认使用外部浏览器打开而不是内部web-view组件打开
					// #endif
					// #ifdef H5
					if(this.toUrl){
						location.href=this.$config.loginPath+"?returnUrl="+encodeURIComponent(urlPre+'/pages/login/loading?returnUrl='+encodeURIComponent(this.toUrl));
					}else{
						location.href=this.$config.loginPath+"?returnUrl="+encodeURIComponent(urlPre+'/pages/login/loading');
					}
					// #endif
				}
			  }).catch(err=>{
				  //
				  uni.showToast({
				  	title:'请求超时'
				  })
			  })
			}
		}
	}
</script>

<style>
</style>
