<template>
	<view class="loadTxt">Loading...</view>
</template>

<script setup>
	import { ref } from "vue";
	import api from '@/api/api';
	import { onLaunch, onLoad } from '@dcloudio/uni-app'
	import * as environment from '@/environments/environment'
	import dataHandling from "@/util/dataHandling";
	import wx from 'weixin-js-sdk'
	let jumpInfo = ref({})
	let dataToken = ref()
	onLoad((options) => {
		jumpInfo.value = JSON.parse(JSON.stringify(options))
		
		// 参数带token了重新登录
		if (options.dataToken) {
			console.log('jumpInfo.value', jumpInfo.value);
			// uni.clearStorageSync();
			dataToken.value = decodeURIComponent(options.dataToken)
			reLogin(options.landingPage)
			return
		} else if (options.miniShare) {
			// 通过小程序分享进来的但没登录
			if (options.landingPage == 'courseDetail') {
				if(jumpInfo.value.shareUserId){
					uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&coursesharing=1&serialsNo=${jumpInfo.value.serialsNo}&shareCode=${jumpInfo.value.shareCode}&shareUserId=${jumpInfo.value.shareUserId}&jumpUrl=${jumpInfo.value.jumpUrl}&miniShare=1` })
				}else {
					uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&miniShare=1` })
				}
				
			} else if (options.landingPage == 'businessCard') {
				uni.reLaunch({ url: `/myPackageA/businessCard/businessCard?userId=${jumpInfo.value.userId}&isShareCard=1&cardId=${jumpInfo.value.cardId}&miniShare=1` })
			} else if (options.landingPage == 'applyFranchise') {
				uni.reLaunch({ url: `/myPackageA/applyFranchise/applyFranchise?shareId=${jumpInfo.value.shareId}&invitationCode=${jumpInfo.value.invitationCode}&inviteUserId=${jumpInfo.value.inviteUserId}&miniShare=1` })
			}else if(options.landingPage == 'index'){
				uni.reLaunch({
					url:'/pages/index/index?miniShare=1'
				})
			}else if(options.landingPage == 'courselist'){
				uni.reLaunch({
					url:'/pages/courselist/courselist'
				})
			}else if(options.landingPage == 'personalCenter'){
					uni.reLaunch({
						url:'/pages/personalCenter/personalCenter'
					})
				}
			return
		}else if(options.noLogin){
			// 去到小程序登录页了，没登录
			if (options.landingPage == 'courseDetail') {
				uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}` })
			}else if(options.landingPage == 'index'){
				uni.reLaunch({
					url:'/pages/index/index'
				})
			}else if(options.landingPage == 'courselist'){
				uni.reLaunch({
					url:'/pages/courselist/courselist'
				})
			}else if(options.landingPage == 'personalCenter'){
					uni.reLaunch({
						url:'/pages/personalCenter/personalCenter'
					})
				}
		}

	})
	
	const reLogin = (landingPage) => {
		console.log('lanuch调用了');
		uni.showLoading({
			title: '加载中...'
		});
		// 添加iOS平台检测
		const isIOS = navigator.platform.match(/(iPhone|iPod|iPad)/i);
		const params = {
			loginType: '5',
			authToken: dataToken.value,
			visitorUuid:uni.getStorageSync('visitorUuid')?uni.getStorageSync('visitorUuid'):dataHandling.uuid()
		}
		api.loginVerification(params).then((res) => {
			if (res['success']) {
				uni.setStorageSync('isLogin', '1');
				uni.setStorageSync('loginType', 'codelogin');
				uni.setStorageSync('cffp_userId', res.data.userId);
				uni.setStorageSync('uni-token', res.data['token']);
				uni.setStorageSync('visitorUuid', res.data['visitorUuid']);
				if (landingPage == 'sfpHousePurchase') {
					uni.reLaunch({ url: `/sfp/housePurchase/housePurchase` })

				} else if (landingPage == 'sfpIndex') {
					uni.reLaunch({ url: `/sfp/index/index` })
				}  else if (landingPage == 'courseDetail') {
					if(jumpInfo.value.noShare){
						uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&noShare=1`})
					} else {
						if(jumpInfo.value.shareUserId){
							uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&coursesharing=1&serialsNo=${jumpInfo.value.serialsNo}&shareCode=${jumpInfo.value.shareCode}&shareUserId=${jumpInfo.value.shareUserId}&jumpUrl=${jumpInfo.value.jumpUrl}&miniShare=1` })
						}else {
							uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&miniShare=1` })
						}
						// uni.reLaunch({ url: `/pages/courseDetail/courseDetail?fileId=${jumpInfo.value.fileId}&coursesharing=1&serialsNo=${jumpInfo.value.serialsNo}&shareCode=${jumpInfo.value.shareCode}&shareUserId=${jumpInfo.value.shareUserId}&jumpUrl=${jumpInfo.value.jumpUrl}&miniShare=1` })
					}
					return
				} else if (landingPage == 'businessCard') {
					uni.reLaunch({ url: `/myPackageA/businessCard/businessCard?userId=${jumpInfo.value.userId}&isShareCard=1&cardId=${jumpInfo.value.cardId}&miniShare=1` })
				} else if (landingPage == 'applyFranchise') {
					uni.reLaunch({ url: `/myPackageA/applyFranchise/applyFranchise?shareId=${jumpInfo.value.shareId}&invitationCode=${jumpInfo.value.invitationCode}&inviteUserId=${jumpInfo.value.inviteUserId}&miniShare=1` })
				}else if(landingPage == 'index'){
					uni.reLaunch({
						url:'/pages/index/index?miniShare=1'
					})
				}else if(landingPage == 'courselist'){
					uni.reLaunch({
						url:'/pages/courselist/courselist'
					})
				}else if(landingPage == 'personalCenter'){
					uni.reLaunch({
						url:'/pages/personalCenter/personalCenter'
					})
				}else if(landingPage == 'orderStatus'){
					uni.reLaunch({
						url:`/pages/orderStatus/orderStatus?orderId=${jumpInfo.value.orderId}&fileId=${jumpInfo.value.fileId}&orderStatus=2&userId=${res.data.userId}&backMySelf=1`
					})
				}
			} else {
				uni.showToast({
					title: res['message'],
					duration: 2000,
					icon: 'none'
				})
				uni.reLaunch({ url: `/pages/index/index` })
			}
		})
		uni.hideLoading()

	}
</script>
<style lang="scss" scoped>
	.loadTxt {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
</style>