<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')
		getInfo()
	} else {
		loginFilter()
	}
})

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

		if (type.value == 1) {
			getInfo()
		} else if (type.value == 2) {
			// 不是学生的话跳转登录页面

			if (pageValue.value == 'false') {
				uni.clearStorageSync()
				uni.reLaunch({
					url: '/pages/404/404?message=暂无页面'
				})
			} else {
				uni.reLaunch({
					url: `/pages/login/login?pageValue=${pageValue.value}`
				})
			}
		}
	}
}

// 获取信息
const getInfo = async () => {
	const res = await getInfoByTokenReq()
	// console.log(res)
	if (res.code == 200) {
		uni.setStorageSync('studentInfo', res.data)
		uni.setStorageSync('TOKEN', res.data.token)
		// 是学生的话如何跳转
		goPage()
	}
}

const goPage = () => {
	if (pageValue.value == 1) {
		const studentInfo = uni.getStorageSync('studentInfo')
		if (studentInfo.fillStatus == '已填报') {
			uni.reLaunch({
				url: '/pages/myMsg/myMsg'
			})
		} else {
			uni.reLaunch({
				url: '/pages/read/read'
			})
		}
	} else if (pageValue.value == 2 || pageValue.value == 3) {
		let { isPay } = uni.getStorageSync('studentInfo')

		if (isPay == 0) {
			// uni.clearStorageSync()
			uni.reLaunch({
				url: `/pages/404/404?message=请先完成缴费&pageValue=${pageValue.value}`
			})
		} else if (isPay == 1) {
			if (pageValue.value == 2) {
				uni.reLaunch({
					url: '/pages/select/select'
				})
			} else {
				uni.reLaunch({
					url: '/pages/subscribe/subscribe'
				})
			}
		} else {
			uni.showToast({
				title: '暂无缴费信息',
				icon: 'none'
			})
		}
	} else {
		uni.clearStorageSync()
		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>
