<template>
	<view class="app-page">
		<u-loading-icon text="加载中" textSize="18" :vertical="true"></u-loading-icon>
		<u-no-network @disconnected="disconnected" @connected="connected" @retry="retry"></u-no-network>
	</view>
</template>

<script>
	import {
		userLogin
	} from "@/common/api/user.js";
	import {
		bindFrom
	} from "@/common/api/common.js";

	export default {
		data() {
			return {
				from: null,
				goodCode: null,
			}
		},
		onLoad(options) {
			console.log('options=>', options);
			if (options.id) {
				this.from = options.id;
			}
			if (options.code) {
				this.goodCode = options.code;
			}
			this.getWxCode();
			// uni.reLaunch({
			// 	url: '/pages/afterSales/afterSales'
			// 	// url: "/pages/home/index"
			// })
			// if (!options.id && !options.code) {
			// 	setTimeout(() => {
			// 		uni.reLaunch({
			// 			url: "/pages/home/index"
			// 		})
			// 	}, 10);
			// } else {
			// 	// && uni.getStorageSync('token')
			// 	this.getWxCode();
			// }
		},
		methods: {
			disconnected() {
				console.log('disconnected');
			},
			connected() {
				console.log('connected');
				// this.getWxCode();
			},
			retry() {
				console.log('retry');
			},
			getWxCode() {
				const that = this;
				uni.login({
					provider: "weixin",
					success: (wxRes) => {
						console.log("wxCode==>", wxRes.code)
						uni.showToast({
							title: 'wxlogin'
						})
						that.userLogin(wxRes.code);

					},
					fail: (err) => {
						uni.showToast({
							title: err
						})
					}
				})
			},
			userLogin(code) {
				const data = {
					code: code
				};
				userLogin(data)
					.then((res) => {
						console.log("userLogin==>", res.data)
						console.log("userLogin==>", res.data.token.token)
						uni.showToast({
							title: 'userLogin'
						})
						if (res.data) {
							uni.setStorageSync('token', res.data.token.token)
							uni.showToast({
								title: 'successserLogin'
							})
							const userInfo = uni.getStorageSync('userInfo') || '{}';
							console.log(89999, userInfo, JSON.parse(userInfo).username)
							if (userInfo && !JSON.parse(userInfo).username) {} else {
								uni.setStorageSync('userInfo', JSON.stringify(res.data))
							}
							console.log(8888, this.from)
							if (this.from) {
								if (this.from === JSON.parse(userInfo).id) {
									if (this.goodCode) {
										uni.reLaunch({
											url: '/pages/home/index?code=' + this.goodCode
										})
									} else {
										uni.reLaunch({
											url: "/pages/home/index"
											// url: '/pages/privateSphere/index'
											// url: '/subPages/psGoodsDetail/index?code=G7831850573794621080&type=common'
										})
									}
									return
								}
								this.bindFrom()
							} else {
								if (this.goodCode) {
									uni.reLaunch({
										url: '/pages/home/index?code=' + this.goodCode
									})
								} else {
									// uni.showToast({
									// 	title: '5'
									// })
									uni.reLaunch({
										url: "/pages/home/index",
										// url: '/subPages/orderDetail/orderDetail?orderNo=GO2023092454006061'
										// url: '/subPages/myOrder/myOrder'
										// url: '/subPages/poster/poster',
										// url: "/subPages/goodsDetail/goodsDetail?code=G7946559351964068502&type=gift"
										// url: '/subPages/addAddress/addAddress?code=UA795093359090873249'
										// url: '/subPages/settleCenter/settleCenter?type=cart&ids=63&isGift=0'
									})
								}
							}
						}
					}).catch((err) => {
						// uni.showToast({
						// 	title: '6'
						// })
					})
			},
			bindFrom(code) {
				const data = {
					puid: this.from
				};
				bindFrom(data)
					.then((res) => {
						console.log("bindFrom==>", res.data)
						if (res.data) {
							if (this.goodCode) {
								uni.reLaunch({
									url: '/pages/home/index?code=' + this.goodCode
								})
							} else {
								uni.reLaunch({
									url: "/pages/home/index"
									// url: '/pages/privateSphere/index'
								})
							}

						} else {
							if (this.goodCode) {
								uni.reLaunch({
									url: '/pages/home/index?code=' + this.goodCode
								})
							} else {
								uni.reLaunch({
									url: "/pages/home/index"
									// url: '/pages/privateSphere/index'
								})
							}
						}
					})
					.catch(() => {
						uni.hideLoading();
						if (this.goodCode) {
							uni.reLaunch({
								url: '/pages/home/index?code=' + this.goodCode
							})
						} else {
							uni.reLaunch({
								url: "/pages/home/index"
								// url: '/pages/privateSphere/index'
							})
						}
					});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-page {
		width: 100%;
		justify-content: center;
		height: 100vh;
		display: flex;
		background: transparent;
		align-items: center;
		flex-direction: column;
	}


	.u-content {
		padding: 150px 60px 0;
		@include flex(column);
		align-items: center;
		justify-content: center;

		&__circle {
			background-color: $u-success;
			@include flex;
			border-radius: 100px;
			width: 60px;
			height: 60px;
			align-items: center;
			justify-content: center;
		}

		&__normal {
			font-size: 15px;
			color: $u-success;
			margin-top: 15px;
		}
	}
</style>