<template>
	<view class="screen-container">
		<image class="bg" src="../../static/img/screen/screen-bg.png" mode="scaleToFill" />
		<button v-if="unregistered" @click="changeTabBar" class="btn">
			马上进入
		</button>
		<button v-else class="btn" open-type="getPhoneNumber" @getphonenumber="getInfoPhoneNumber">
			授权绑定
		</button>
	</view>
</template>
<script>
	import http from '@/shopro/request/index';
	import store from '@/shopro/store';
	import {
		mapActions,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				unregistered: false
			}
		},
		computed: {
			...mapGetters(['userInfo', 'todayNum'])
		},
		methods: {
			...mapActions(['getPhoneNumber', 'registerInfoFn',  'authCheck', 'getUserInfo']),
			// 切换底部tab;
			changeTabBar() {
				if (this.unregistered) {
					uni.navigateTo({
						url: '/pages/h5/index',
					})
				}
			},
			// 获取手机号
			async getInfoPhoneNumber(e) {
				let number = await this.getPhoneNumber(e);
				let register = await this.registerInfoFn();
				if (register) { //注册成功
					this.unregistered = true;
				}
			},
			async authAndWeRun() {
				let auth = await this.authCheck();
				console.log("调用authcheck的值"+auth)
				let that = this; 
				if (auth == 0) {
					that.unregistered = true;
				} else if (auth == 2001) { 
					that.unregistered = false;  
				}
			},
			async loginSession() {
				let token = uni.getStorageSync('token');
				let that = this;
				/**
				 * session有效{
					 用户注册：token有/无/失效  homepaeg- authcheck(2001)- code-wxRun
					 用户未注册：token有/无  authcheck(1001)-homepage-code-手机号-注册-wxRun（点击授权登录）
				 }
				 * session无效{
					用户注册：token有/无/失效 login-更新session -homepage -authcheck - wxrun
					用户未注册：token无/失效  login-更新session -homepage -authcheck （点击授权登录）
				 }
				
				 * *
				*/
				if (this.checkSessionStatus && token) {
					let token = uni.getStorageSync('token');
					console.log("token的值===================="+token)
					this.authAndWeRun();
				} else {
					// session 不存在，调用login。userInfo 。更新session 
					uni.login({
						success: function(info) { //调用code
							console.log("code000000000000000000000000==========", info.code)
							that.$store.commit('wxCode', info.code); //按钮切换全选。
							let userInfo = that.getUserInfo(info.code);
							userInfo.then(res => {
								that.authCheck().then(checkCode => {
									if (checkCode == 0) {
										that.unregistered = true;
									}
								})
							})
						}
					})
				}

			},
			async getAuth() {
				let that = this;
				uni.checkSession({ //校验session
					success(res) { //有效
						if (res.errMsg === "checkSession:ok") {
							that.checkSessionStatus = true;
							// 如果有缓存信息
							that.loginSession()
						}
					},
					fail(err) { //无效
						console.log("无效session")
						that.checkSessionStatus = false;
						that.loginSession()
					}
				})

			}
		},
		onHide() {},
		onShow() {
			// uni.hideTabBar();
		},
		onReady() {
			this.getAuth();
		}
	}
</script>

<style lang="scss" scoped>
	.screen-container {
		// position: fixed;
		position: absolute;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.bg {
			width: 100%;
			height: 100%;
		}

		.btn {
			position: absolute;
			transform: translateX(-50%);
			left: 50%;
			bottom: 100rpx;
			width: 640rpx;
			height: 100rpx;
			line-height: 100rpx;
			background-color: #fff;
			color: red;
			border-radius: 30rpx;
			font-size: 50rpx;
			font-weight: 400;
			color: #ff6427;
			box-shadow: 0 10px 15px 0 rgba(255, 255, 255, 0.2);
		}
	}
</style>
