<template>
	<view class="content">
		  <!-- #ifdef MP-WEIXIN -->
		                <official-account class="relevance" @error="loadFail" @load='loadSucc'></official-account>
		  <!-- #endif -->
		<image class="logo" src="/static/bg-home.jpg" mode="widthFix" :style="{marginTop:marginTopCss}"></image>
		<view class="getPhone-box" :style="{ bottom: bottomCss}">
			<button type="primary" class="custom-btn"  open-type="getPhoneNumber"  style="margin-right: 5%!important;"
			@getphonenumber="getPhoneNumber($event,'custom')" >观展登录</button> 
			<button type="primary" class="custom-btn dealer-btn"  open-type="getPhoneNumber"
			@getphonenumber="getPhoneNumber($event,'dealer')" >经销商登录</button> 
		</view>
	</view>
</template>

<script>
	var md5 = require("../../common/md5.js");
	export default {
		components: {
		},
		data() {
			return {
				title: 'Hello',
				pageInfo:{},
				isAuth:getApp().globalData.isAuth,
				isAuthPhone:getApp().globalData.user_phone,
				productList:[
				],
				bottomCss:'15%',
				marginTopCss:'0',
				userInfoObj:{},
				// phoneHeight:'',
			}
		},
		onLoad() {
		  this.getEquipmentHeight();
		  uni.hideTabBar({})
		  console.log('onload')
		  // getApp().globalData.lastId = options.scene ? options.scene : getApp().globalData.user_id
		},
		onShow() {
			this.loginRequest();
			console.log('onshow')
		},
		onShareAppMessage() {
			  return {
			      title: '科尔卡诺展会入场登记',
			      path:'/pages/index/index'
			    }
		},
		methods: {
			loadFail(e){
				 console.log("FAIL-Offical：",e)
				// this.marginTopCss = 0;
				let phoneHeight = uni.getSystemInfoSync().windowHeight;
				this.bottomCss = phoneHeight > 640 ? '35%':'25%';
				if(phoneHeight > 820){
					this.bottomCss = '45%';
				}
			},
			loadSucc(e){
				 console.log("SUCCESS-Offical：",e)
			},
			getEquipmentHeight(){
				console.log('getHeight')
			    let phoneHeight = uni.getSystemInfoSync().windowHeight;
				console.log(phoneHeight)
				this.bottomCss = phoneHeight > 640 ? '25%':'15%';
				if(phoneHeight > 820){
					this.bottomCss = '35%';
				}
			},
			getPhoneNumber(e,type){
				let that = this;
				console.log(type)
				 if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
				  that.phoneRequest(e.detail.iv, e.detail.encryptedData, that.userInfoObj.session_key,type)
				} else { //
				   uni.showToast({
				   	title: '您已拒绝授权登录',
				   	icon: 'none'
				   });
				}
			},
			phoneRequest(myIv,myEncryptedData,sKey,type){
				let that = this;
				uni.request({
					url:getApp().globalData.shareUrl, //需要设置为全局
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						method: 'auth',
						timestamp: getApp().globalData.globalTimestamp, //Date.now()
						sign: md5('auth' + getApp().globalData.globalTimestamp),
						iv:myIv,
						session_key:sKey,
						encryptedData:myEncryptedData,
					},
					success: res => {
						// getApp().globalData.user_phone = res.data.phoneNumber;
						console.log(res)
						if(res.data.code === 200){
							getApp().globalData.userPhone = res.data.msg.phone;
							type === 'dealer' ? uni.navigateTo({
												url: '/pages/index/dealerPage/index',
												success: res => {},
												fail: () => {},
												complete: () => {}
											}) : uni.navigateTo({
												url: '/pages/index/customPage/index',
												success: res => {},
												fail: () => {},
												complete: () => {},
											})
						}else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
					} 
				});
			},
			judgeUserType(type,pass){
				console.log(type,pass)
				switch (true){
					case type === 3:  //需要授权
					    console.log(type,pass)
						break;
					case type === 1 && pass === 1:  //普通用户且允许入场
							uni.redirectTo({
								url: '/pages/index/successPage/index?types='+'custom', //游客成功页面
								success: res => {},
								fail: () => {},
								complete: () => {}
							})
						break;
					case type === 2 && pass === 1:  //经销商用户且允许入场
					    	uni.redirectTo({
					    		url: '/pages/index/successPage/index?types='+'dealer', //经销商成功页面
					    		success: res => {},
					    		fail: () => {},
					    		complete: () => {}
					    	})
						break;
					case  pass === -1:  //禁止入场
					    	uni.redirectTo({
					    		url: '/pages/index/failPage/index', //失败页面
					    		success: res => {},
					    		fail: () => {},
					    		complete: () => {}
					    	});
						break;	
					default:
						break;
				}
			},
			 loginRequest(){
				 let that = this;
				 	uni.login({
				 		success: (res) => {
				 			uni.request({
				 				url: getApp().globalData.shareUrl, //需要设置为全局
				 				method: 'POST',
				 				header: {
				 					'content-type': 'application/x-www-form-urlencoded'
				 				},
				 				data: {
				 					method: 'login',
				 					timestamp: getApp().globalData.globalTimestamp, //Date.now()
				 					code: res.code,
				 					sign: md5('login' + getApp().globalData.globalTimestamp)
				 				},
				 				success: res => {
				 					// 通过openid发起会员登录
									//type  1-普通客户  2-经销商客户  3-不存在需授权登录
									//pass  0-未知   1-允许入场  -1 - 禁止入场   ，
									if(res.data.code === 200){
										that.userInfoObj = res.data.msg;
										getApp().globalData.openId = that.userInfoObj.openid;
										getApp().globalData.userCodeNumber = that.userInfoObj.code;
										getApp().globalData.managerPhone = that.userInfoObj.qyjl_phone;
										getApp().globalData.managerName = that.userInfoObj.qyjl_name;
										getApp().globalData.addTime = that.userInfoObj.addtime;
										that.judgeUserType(that.userInfoObj.type,that.userInfoObj.pass)
									}else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										});
									}
									// getApp().globalData.session_key = res.data.msg.session_key;
				 				}
				 			});
				 	}
				 });
			 },
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		// height: 100%;
		// background-image: url('/static/bg-home.jpg');
		// background-repeat: no-repeat;
		// background-size: 100% 100%;
	}
	.more-height{
		height: 100%!important;
	}
	/* #ifdef MP-WEIXIN */
    .relevance {
		width: 100%;
		// height: 10px;
		// position: absolute;
		// top: 0;
		z-index: 999;
	}
	/* #endif */
	.logo {
		width: 100%;
		position: relative;
		// margin-top: 75px;
	}
	.getPhone-box {
		width: 100%;
		height: 200rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		bottom: 25%;
	}
	.custom-btn {
		width: 230rpx;
		height: 80rpx;
		line-height:80rpx;
		color: #fff!important;
		background-color: rgb(136,143,137)!important;
		font-size: 30rpx!important;
		letter-spacing: 2rpx;
		margin: 0!important;
		padding: 0!important;
	}
	.dealer-btn {
		background-color: rgb(202,148,98)!important;
	}
</style>

