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

<script>
export default {
	data() {
		return {
			// 获取用户信息
			ocode: '1015730314',
			// 商户appkey
			appkey: '9D6ACFE8CF9AFD07',
			// 跳转地址
			URL: 'https://chtech.ncjti.edu.cn/campusclock/app/'
		}
	},
	onLoad() {
		//检查是否存在用户信息
		this.loginFilter()
	},
	methods: {
		loginFilter() {
			const clockIn_token = uni.getStorageSync('clockIn_token')
			if (!clockIn_token) {
				let wxcode = this.getQueryString('wxcode')
				if (!wxcode) {
					window.location.href = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=${this.appkey}&response_type=code&scope=snsapi_base&ocode=${
						this.ocode
					}&redirect_uri=${this.URL}&state=wxcode`
				} else {
					// 获取wxcode后请求登录
					this.login(wxcode)
				}
			} else {
				uni.reLaunch({
					url: '/pages/home/home'
				})
			}
		},

		// 用户登陆获取个人信息和token
		async login(wxcode) {
			let res = await this.$myRequest_clockIn({
				url: '/attendance/api/system/user/login/app',
				method: 'post',
				header: {
					'content-type': 'application/json',
					platform: 2,
					'Accept-Language': 'zh-CN,zh;q=0.9'
				},
				data: {
					redirect_uri: this.URL,
					wxcode
				}
			})
			// console.log(res)
			if (res.code == 200) {
				uni.setStorageSync('clockIn_token', res.data.token)
				uni.reLaunch({
					url: '/pages/home/home'
				})
			}
		},
		//获取当前URL指定参数
		getQueryString(name) {
			// 获取URL
			let url = window.location.href
			// 正则匹配URL
			let pattern = new RegExp('[?&]' + name + '=([^&]+)', 'i')
			let matcher = pattern.exec(url)
			if (matcher == null || matcher.length < 1) {
				return false
			}
			// 输出指定的参数值
			return decodeURIComponent(matcher[1])
		}
	}
}
</script>

<style></style>
