<template>
	<view class="launch-v">
		<text class="title" @click="copy">{{title}}</text>
		<text class="title" @click="copy">{{token}}</text>
		<!-- #ifdef APP-PLUS -->
		<view class="launch-box">
			<view class="title">全新模式 为你而来</view>
			<view class="version">VERSION3.7</view>
			<view class="launch-img">
				<image :src="startup" mode="widthFix"></image>
				<text class="title" @click="copy">{{title}}</text>
			</view>
			<!-- <view class="copyright">Copyright © 2024 华中师范大学出品</view> -->
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import resources from '@/libs/resources.js'
	import {
		useUserStore
	} from '@/store/modules/user'
	export default {
		data() {
			return {
				startup: resources.startup.main,
				title: '',
				token: ''
			}
		},
		onLoad() {
			if (uni.getStorageSync('isUpdate')) return
			const launchFlag = uni.getStorageSync('launchFlag');
			const token = uni.getStorageSync("token") || '';
			console.log(launchFlag, 999)
			if (launchFlag) {
				alert(1)
				if (token) {
					uni.switchTab({
						url: '/pages/index/index'
					})
				} else {
					uni.redirectTo({
						url: '/pages/login/index'
					})
				}
			} else {
				alert(2)
				// #ifdef APP-PLUS
				uni.redirectTo({
					url: '/pages/launch/policy'
				})
				// #endif
				// #ifndef APP-PLUS
				if (token) {
					alert(3)
					uni.switchTab({
						url: '/pages/index/index'
					})
				} else {
					// uni.redirectTo({
					// 	url: '/pages/login/qywxLogin'
					// })
					if (!localStorage.getItem("hasLogin")) {
						alert(4)
						uni.redirectTo({
							url: '/pages/login/qywxLogin'
						})
					}

					if (this.$route.query.code) {
						alert(5)
						// 从静模授权进入首页				
						localStorage.setItem("auth_code", this.$route.query.code)
						this.title = this.$route.query.code
						this.getToken()
					}


				}
				// #endif
			}
		},
		methods: {
			// 获取敏感信息
			getToken: function() {
				const that = this
				const userStore = useUserStore()

				const map = new Map();
				map.set('name', 'John');
				map.set('age', 30);



				const data = {
					nonPassword: 'nonPassword',
					grant_type: 'password',
					code: (this.$route.query.code).trim()
				}

				// 将 Map 转换为 FormData
				const formData = new FormData();
				data.forEach((value, key) => {
					formData.append(key, value);
				});

				uni.request({
					url: 'https://dj.ccnu.edu.cn/api/oauth/wechat/getToken',
					data: formData,
					method: 'POST',
					success: function(res) {
						alert("res:" + res.data.data.token + ';' + "code2:" + data.code + ';' + "res1:" +
							res.data.data);
						// let token = res.data.data.token?res.data.data.token:'无'
						// that.token = token
						// that.title = that.$route.query.code
						userStore.setToken(token)
						// this.rememberAccount()
						userStore.getCurrentUser().then(res => {
							// this.loading = false
							uni.switchTab({
								url: '/pages/index/index'
							});
						}).catch(() => {
							// this.loading = false
						})
					}
				})
			},
			copy: function() {
				uni.setClipboardData({
					data: this.$route.query.code,
					success: () => {
						uni.showToast({
							title: '复制成功',
							duration: 2000,
							icon: 'success'
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.launch-v {
		.launch-box {
			padding-top: 170rpx;

			.title {
				font-size: 50rpx;
				line-height: 70rpx;
				color: #5098FA;
				text-align: center;
				margin-bottom: 10rpx;
			}

			.version {
				width: 250rpx;
				margin: 0 auto;
				text-align: center;
				height: 50rpx;
				border: 1px solid #5098FA;
				border-radius: 25rpx;
				line-height: 48rpx;
				color: #5098FA;
				letter-spacing: 2rpx;
			}

			.launch-img {
				margin: 0 auto;
				width: 680rpx;

				image {
					width: 100%;
				}
			}

			.copyright {
				position: fixed;
				bottom: 120rpx;
				right: 0;
				left: 0;
				text-align: center;
				color: #999999;
				font-size: 24rpx;
			}
		}
	}
</style>