<template>
	<view>
		<!-- 签约页面 -->
	</view>
</template>

<script>
// md5加密文件
import md5 from '@/js_sdk/js-md5/src/md5.js'
// base64加密文件
import { Base64 } from '@/js_sdk/js-base64/base64.js'
export default {
	data() {
		return {
			newTime: '',
			requestUrl: 'https://chtech.ncjti.edu.cn/jiaofei/jiaofei-api/tuitionpayment/WeiXiaoAuth/authentication'
		}
	},
	onLoad() {
		this.getIdCard()
	},
	methods: {
		// 获取用户身份证
		getIdCard() {
			let idCard = sessionStorage.getItem('idCard')
			if (!idCard) {
				let idCard = this.getQueryString('idCard')
				if (!idCard) {
					window.location.href = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=EE28EE2C93296F4E&response_type=code&scope=snsapi_base&ocode=1015730314&redirect_uri=${this.requestUrl}&state=${this.requestUrl}`
				} else {
					sessionStorage.setItem('idCard', idCard)
					this.getName()
				}
			} else {
				this.getName()
			}
		},
		// 获取用户姓名
		getName() {
			let name = sessionStorage.getItem('name')
			if (!name) {
				let name = this.getQueryString('name')
				if (!name) {
					window.location.href = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=EE28EE2C93296F4E&response_type=code&scope=snsapi_base&ocode=1015730314&redirect_uri=${this.requestUrl}&state=${this.requestUrl}`
				} else {
					sessionStorage.setItem('name', name)
					this.getUrl()
				}
			} else {
				this.getUrl()
			}
		},

		// 获取跳转地址
		getUrl() {
			// md5加密 处理token参数
			this.getTime()
			let token = md5(md5('JANCJTISCHOOLSIGNING' + this.newTime))

			// base64加密 处理data参数
			let student_id = sessionStorage.getItem('idCard')
			let student_name = sessionStorage.getItem('name')
			let id_number = sessionStorage.getItem('idCard')

			// 收集参数
			let data = {
				student_id,
				student_name,
				id_number
			}
			// 把参数转换成JSON格式
			data = JSON.stringify(data)
			// 加密处理
			data = Base64.encode(encodeURIComponent(data))

			// // 获取授权签约地址
			let newUrl = `https://sa.jxnxs.com/SchoolAgreement/index?token=${token}&data=${data}`

			// // 跳转授权签约地址
			window.location.href = newUrl
		},

		// 获取当前时间
		getTime() {
			// 获取当前时间戳
			var date = new Date()
			// 获取年份
			var year = date.getFullYear()
			// 获取月份并补0
			var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
			// 获取日期并补0
			var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
			// 获取小时并补0
			var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
			// 拼接后赋值
			this.newTime = String(year) + String(month) + String(day) + String(hours) + '00' + '00'
		},

		//获取当前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>
