<template>
	<div id="loader-wrapper">
		<div id="loader"></div>
		<div class="loader-section section-left"></div>
		<div class="loader-section section-right"></div>
		<div class="load_title">sso单点登录中，请耐心等待</div>
	</div>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'
	import {
		setSsoAdminToken,
		getOpenToken,
		openWindowSso,
		 ssoLogout,
		getSsoAdminToken,
		setEnv,
		removeEnv,
		setToken,
		getToken,
		isLoginSso
	} from '@/utils/auth';
	export default {
		data() {
			return {
				codeUrl: '',
				ssoCode: '',
			}
		},
		onLoad(query) {
			if (!isLoginSso) {
				uni.redirectTo({
					url: '/pages/login'
				})
				return false
			}
			// console.log(query, 3333)
			this.ssoCode = query.ssoCode
			setSsoAdminToken(this.ssoCode);

			// console.log(getSsoAdminToken(), 'getSsoAdminToken')
			this.loginSso()
		},
		methods: {

			params() {
				var query = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+", "g"));
				console.log(query, 'query')
				query = query || '';
				var result = {};
				for (var i = 0; i < query.length; i++) {
					var item = query[i].substring(1).split('=');
					result[item[0]] = item[1];
				}
				return result;
			},
			loginSso() {
				// var route = useRoute();
				// const ssoCode = route.query.ssoCode;

				if (getSsoAdminToken() && getToken()) {
					uni.redirectTo({
						url: '/pages/index'
					})
				};
				if (!this.ssoCode) {
					if (!localStorage.getItem('ssoLogout')) {
						localStorage.removeItem('ssoLogout');
						// setEnv();
					};
					ssoLogout();
					openWindowSso();
					return;
				};

				// 请求头

				getOpenToken().then(res => {
					removeEnv()
					setToken(res.data)
					// var options = uni.getStorageSync('options')
					this.$tab.reLaunch('/pages/index')
				}).catch(err => {
					console.log('获取token失效');
					removeEnv();
				})
			}
		}
	}
</script>

<style lang="scss">
	#loader-wrapper .load_title span {
		font-weight: normal;
		font-style: italic;
		font-size: 13px;
		color: #FFF;
		opacity: 0.5;
	}
</style>