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

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

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

import { myRequest } from '@/util/api.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')
	if (!card_number) {
		// 判断是否是从小程序进入
		const type = getQueryString('type')
		if (type === 'weixin') {
			// 跳转回小程序页面授权
			jWeixin.miniProgram.reLaunch({ url: '/pages/index/index' })
		} else {
			getCardnumberURL()
		}
	} else {
		uni.setStorageSync('bus_card_number', card_number)
		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);
	uni.setStorageSync('bus-userInfo', JSON.stringify(res.data))
	// 判断用户权限
	if (res.data.user_zz === '教师' || res.data.user_zz === '临时人员') {
		uni.switchTab({
			url: '/pages/home/home'
		})
	} else if (res.data.user_zz === '车队长') {
		uni.reLaunch({
			url: '/pages/record/record'
		})
	} else if (res.data.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>
