<template>
	<view>
		<!-- 授权页面 -->
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getQueryString } from '@/utils/getParams.js'
import { getInfoByTokenReq } from '@/api/index.js'

const APPKEY = '9F3C6FD7E3D98CE0'
const ocode = '1015730314'

// 1:信息采集
// 2:宿舍选取
// 3:车辆预约
const pageValue = ref()

//1：学生 2：非学生
const type = ref()

// 线上地址
// const URL = 'https://chtech.ncjti.edu.cn/welcome/welcome_api/welcome/api/home/Openid'

// 本地地址
const URL = 'http://192.168.161.221:8080/welcome/api/home/Openid'

onLoad(() => {
	const studentInfo = uni.getStorageSync('studentInfo')
	if (studentInfo) {
		// pageValue.value = getQueryString('urlstr')
		// goPage()
		uni.reLaunch({
			url: '/pages/read/read'
		})
	} else {
		loginFilter()
	}
})

// 获取用户token
const loginFilter = async () => {
	const token = getQueryString('token')
	// pageValue.value = getQueryString('urlstr')
	type.value = getQueryString('type')
	if (!token) {
		getCardnumberURL()
	} else {
		uni.setStorageSync('token', token)

		// console.log(pageValue.value)
		// console.log(type.value)

		if (type.value == 1) {
			// 获取信息
			const res = await getInfoByTokenReq()
			// console.log(res)
			if (res.code == 200) {
				uni.setStorageSync('studentInfo', res.data)
				uni.setStorageSync('TOKEN', res.data.token)
				// 是学生的话如何跳转
				// goPage()
				uni.reLaunch({
					url: '/pages/read/read'
				})
			}
		} else if (type.value == 2) {
			// 不是学生的话跳转登录页面
			// if (pageValue.value == 'false') {
			// 	uni.setStorageSync('token', '')
			// 	uni.reLaunch({
			// 		url: '/pages/404/404?message=暂无页面'
			// 	})
			// } else {
			// 	uni.reLaunch({
			// 		url: `/pages/login/login?pageValue=${pageValue.value}`
			// 	})
			// }
			uni.reLaunch({
				url: `/pages/login/login`
			})
		}
	}
}

const goPage = () => {
	if (pageValue.value == 1) {
		uni.reLaunch({
			url: '/pages/read/read'
		})
	} else if (pageValue.value == 2) {
		uni.reLaunch({
			url: '/pages/select/select'
		})
	} else if (pageValue.value == 3) {
		uni.reLaunch({
			url: '/pages/subscribe/subscribe'
		})
	} else {
		uni.setStorageSync('token', '')
		uni.setStorageSync('studentInfo', '')
		uni.reLaunch({
			url: '/pages/404/404?message=暂无页面'
		})
	}
}

// 跳转授权页面
const getCardnumberURL = () => {
	let URL_param = URL + '?urlstr=' + pageValue.value
	window.location.href = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=${APPKEY}&response_type=code&scope=snsapi_base&ocode=${ocode}&redirect_uri=${URL_param}&state=${URL_param}`
}
</script>

<style lang="scss" scoped></style>
