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

<script setup>
import { onLoad } from '@dcloudio/uni-app'

import { getQueryString } from '@/util/getParams.js'

import { myRequest } from '@/util/api.js'

import { decryptDes } from '@/util/des.js'

const APPKEY = '4AA7B3944BDF3739'
const ocode = '1015730314'

// 线上地址
const URL = 'https://chtech.ncjti.edu.cn/carstop/carbook/appopenid.action'

// 测试地址
// const URL = 'https://chtech.ncjti.edu.cn/testingServertomcat/carbook/appopenid.action'

// 本地地址
// const URL = 'http://192.168.161.220:8088/carBook/appopenid.action'

onLoad(() => {
	loginFilter()
})

// 获取用户card_number
const loginFilter = () => {
	const card_number = getQueryString('card_number')
	const token = getQueryString('token')
	if (!card_number || !token) {
		// 判断是否是从小程序进入
		const type = getQueryString('type')
		if (type === 'weixin') {
			// 跳转回小程序页面授权
			jWeixin.miniProgram.reLaunch({ url: '/pages/index/index' })
		} else {
			getCardnumberURL()
		}
	} else {
		uni.setStorageSync('bus_card_number', card_number)
		uni.setStorageSync('bus_token', token)
		getUserInfo()
	}
}

// 获取用户信息
const getUserInfo = async () => {
	const card_number = uni.getStorageSync('bus_card_number')
	const res = await myRequest({
		url: '/appget_user.action',
		data: {
			card_number
		}
	})
	// console.log(res)

	const result = JSON.parse(decryptDes(res.data))
	// console.log(result)

	uni.setStorageSync('bus-userInfo', JSON.stringify(result))
	// 判断用户权限
	if (result.user_zz === '教师' || result.user_zz === '临时人员') {
		uni.switchTab({
			url: '/pages/home/home'
		})
	} else if (result.user_zz === '车队长') {
		uni.reLaunch({
			url: '/pages/record/record'
		})
	} else if (result.user_zz === '司机') {
		uni.reLaunch({
			url: '/pages/recordDriver/recordDriver'
		})
	} else {
		uni.reLaunch({
			url: '/pages/404/404'
		})
	}
}

// 跳转授权页面
const getCardnumberURL = () => {
	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}&state=${URL}`
}
</script>
