<template>
	<view>

	</view>
</template>

<script>
	import {
		login
	} from '@/api/login.js';
	import {
		setToken,
		getRedirectPath,
		setRedirectPath
	} from '@/utils/auth';
	import {
		getParams
	} from '@/utils/common.js'
	export default {
		data() {
			return {
				code: undefined,
				errInfo: '',
				stateParams: '',
				queryParams: ''
			}
		},
		onLoad(options) {
			let {code,targetPath,token} = options;
			const params = new URLSearchParams(window.location.search);
			setRedirectPath(window.location.href)
			if (targetPath && targetPath != 'undefined') {
				this.stateParams = params.get('targetPath')
				this.queryParams = params
			} else {
				this.stateParams = ''
			}
			if (token && token != 'undefined') {
				setToken(token)
				this.$store.dispatch('GetInfo')
				this.pageRedirect()
			} else {
				this.code = code;
				if (code) {
					this.loginIn();
				} else {
					this.getRedirectUrl()
				}
			}
		},

		methods: {
			loginIn() {
				let params = {
					code: this.code
				}
				login(params).then(res => {
					if (res.code == 200 && res.data) {
						setToken(res.data.token)
						this.$store.dispatch('GetInfo')
						this.pageRedirect()
					} else {
						this.errInfo = res.msg
					}
				})
			},
			// 获取回调地址
			getRedirectUrl() {
				// let params = {
				// 	path: getRedirectPath() || ''
				// }
				// getUrl(params).then(res => {
				// 	console.log(res)
				// 	if (res.code == 200 && res.data) {
				// 		window.location = res.data
				// 	}
				// })
				window.location = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww63ca87d5f8647514&redirect_uri=https%3A%2F%2Ftest.tangguo.ren/h5/pages/login/login/&response_type=code&scope=snsapi_base#wechat_redirect'
			},
			
			// 页面跳转
			pageRedirect(){
				if (this.stateParams) {
					const businessParams = new URLSearchParams();
					this.queryParams.forEach((value, key) => {
						if (key !== 'code' && key !== 'targetPath') {
							businessParams.append(key, value);
						}
					});
					uni.redirectTo({
						url: `${this.stateParams}?${businessParams.toString()}`
					})
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}
		}
	}
</script>

<style>

</style>