<template>
	<view class="centerV hb base-bg-f">
		<view class="w100 centerV" v-if="isH5vip!=true">
			<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/image/becomevip.png" class="h193 w193 mb40"></image>
			<view class="bold">是否申请成为会员</view>
			<view class="justify-center w100 mt40 mb40">
				<view class="quxiao">取消</view>
				<view class="sure" @click="becomeVipfun">确定</view>
			</view>
		</view>
		<u-popup :show="showvip" @close="close" @open="open"
		 mode='center' round='20' style="flex: 0;" >
			<view class="VIPvie w600 h600 column justify-center align-center">
				<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/manergevip/becomeTrue.png" class="h400 w400"></image>
				<view class="bold fs50 center mt80">{{tips}}！</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		becomeVip
	} from "@/api/courseManage.js"
	import {
		loginByMp
	} from '@/api/user'
	const isWechat = () => {
		return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
	}
	export default {
		data() {
			return {
				userId: '',
				companyId: '',
				companyUserId: '',
				code: '',
				showvip: false,
				user:'',
				tips:'',
				isH5vip:false,
				becomeuser:{}
			}
		},
		onLoad(option) {
			if (option && option.user) {
				// console.log('分享链接进入',option)
				this.becomeuser = JSON.parse(option.user)
				this.companyId = this.becomeuser.companyId
				this.companyUserId = this.becomeuser.companyUserId
				// console.log('分享链接进入',this.becomeuser)
				// if(uni.getStorageSync("isH5vip")==true){
				// 	this.becomeVipfun()
				// }else{
				// 	setTimeout(() => {
				// 		this.getWechatCode()
				// 		this.userId=this.user.userId
				// 		console.log(this.userId)
				// 	}, 200)
				// }
				
			} else {
				this.companyId = option.companyId
				this.userId = option.userId
				this.companyUserId = option.companyUserId
				this.code = option.code
			}
		},
		onShow() {
			this.isH5vip = uni.getStorageSync("isH5vip")
			console.log(this.isH5vip)
			if (this.becomeuser&&JSON.stringify(this.becomeuser)!='{}'){
				if(uni.getStorageSync("isH5vip")==true){
					this.becomeVipfun()
				}else{
					setTimeout(() => {
						this.getWechatCode()
						this.userId=this.user.userId
						console.log(this.userId)
					}, 200)
				}
			}
		},
		methods: {
			close() {},
			open() {},
			becomeVipfun() {
				uni.showLoading({
					title: '正在加载中...'
				})
				if (this.becomeuser!=''){
					this.user = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {}
					this.userId=this.user.userId
				} 
				const param = {
					companyId: this.companyId,
					userId: this.userId,
					companyUserId: this.companyUserId,
				}
				console.log(param)
				becomeVip(param).then(res => {
					if (res.code == 200) {
						console.log(res)
						// if(this.becomeuser==''){
						// 	setTimeout(()=>{
								this.showvip = true
								this.tips=res.msg
						// 	},1000)
						// }
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				})
			},
			// 获取code
			getWechatCode() {
				if (isWechat) {
					let appid = "wx93ce67750e3cfba3"; //微信APPid
					let code = this.getUrlCode().code; //是否存在code
					let local = window.location.href;
					if (code == null || code === "") {
						let urlPaths = local.split("/courseh5");
						uni.setStorageSync('beforLoginPage', urlPaths[1]);
						window.location.href =
							"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
							appid +
							"&redirect_uri=" +
							encodeURIComponent(local) +
							"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
					} else {
						this.code = code;
						this.loginByMp()
					}
				}
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			loginByMp() {
				if (this.code == null) {
					return;
				}
				uni.showLoading({
					title: "处理中..."
				});
				// let that = this;
				var data = {
					code: this.code,
					companyUserId:this.companyUserId
				}
				loginByMp(data).then(res => {
						uni.hideLoading();
						if (res.code == 200) {
							// 登录后存token和用户信息
							uni.setStorageSync('UserAppToken', res.token);
							this.isH5vip = uni.setStorageSync("isH5vip",res.isH5Vip);
							uni.setStorageSync('userInfo', JSON.stringify(res.user));
							let beforLoginUrl = uni.getStorageSync('beforLoginPage');
							// console.log("beforLoginUrl:"+beforLoginUrl);
							console.log(`登录成功后跳转${beforLoginUrl}`);
							uni.reLaunch({
								url: beforLoginUrl
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					},
					err => {}
				);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.VIPvie {
		width: 580rpx;
		background: linear-gradient(to bottom, #c3dbfe 2%, #f6fbfe 50%);
		border-radius: 20rpx;
		height: 600rpx;
		position: relative;

		image {
			position: absolute;
			top: -120rpx;
			left: 16%;
		}
	}

	.quxiao {
		width: 30%;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		border: 2rpx solid #ccc;
		border-radius: 8rpx;
		color: #666;
		margin-right: 10rpx;
	}

	.sure {
		width: 30%;
		background-color: #1777ff;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		border-radius: 8rpx;
		color: #fff;
		margin-left: 10rpx;
	}
</style>