<template>
	<view class="content">
		<image class="logo" src="/static/bg-home.jpg" mode="widthFix"></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:'25%',
			}
		},
		onLoad(options) {
		  this.getEquipmentHeight();
		  console.log('onload')
		  // getApp().globalData.lastId = options.scene ? options.scene : getApp().globalData.user_id
		},
		onShow() {
			console.log('onshow')
		},
		onShareAppMessage() {
			  return {
			      title: '科尔卡诺展会入场登记',
			      path:'/pages/index/index'
			    }
		},
		methods: {
			getEquipmentHeight(){
			    let phoneHeight = uni.getSystemInfoSync().windowHeight;
				console.log(phoneHeight)
				this.bottomCss = phoneHeight > 672 ? '35%':'25%';
				if(phoneHeight > 820){
					this.bottomCss = '40%';
				}
			},
			getPhoneNumber(e,type){
				let that = this;
				console.log(type)
				 if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
				 type === 'dealer' ? uni.navigateTo({
			 				url: '/pages/index/dealerPage/index',
			 				success: res => {},
			 				fail: () => {},
			 				complete: () => {}
			 			}) : uni.navigateTo({
			 				url: '/pages/index/customPage/index',
			 				success: res => {},
			 				fail: () => {},
			 				complete: () => {},
						})	
				  //that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
				} else { //
				   uni.showToast({
				   	title: '您已拒绝授权登录',
				   	icon: 'none'
				   });
				}
			},
			phoneRequest(myIv,myEncryptedData,sKey){
				let that = this;
				uni.request({
					url:getApp().globalData.shareUrl, //需要设置为全局
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						method: 'getPhoneNumber',
						timestamp: getApp().globalData.globalTimestamp, //Date.now()
						uid:getApp().globalData.user_id,
						sign: md5('getPhoneNumber' + getApp().globalData.globalTimestamp),
						iv:myIv,
						sessionKey:sKey,
						encryptedData:myEncryptedData,
					},
					success: res => {
						// getApp().globalData.user_phone = res.data.phoneNumber;
						console.log(res)
					} 
				});
			},
			getShareInfo(uid){
				uni.request({
					url: getApp().globalData.shareUrl, //需要设置为全局
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						method: 'addShare',
						timestamp: getApp().globalData.globalTimestamp, //Date.now()
						shangjiid:uid,
						userid:getApp().globalData.user_id,
						sign: md5('addShare' + getApp().globalData.globalTimestamp)
					},
					success: res => {
						console.log(res)
					}
				});
			},
			 loginRequest(){
				 	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发起会员登录
									console.log('userId',res.data.msg.id)
									console.log('lastId',getApp().globalData.lastId)
									getApp().globalData.user_id = res.data.msg.id;
									getApp().globalData.open_id = res.data.msg.openid;
									getApp().globalData.isAuth = res.data.msg.auth_status === '1';
									getApp().globalData.user_name = res.data.msg.name;
									getApp().globalData.user_headUrl = res.data.msg.headimg;
									getApp().globalData.session_key = res.data.msg.session_key;
									getApp().globalData.user_phone = res.data.msg.phone;
									getApp().globalData.user_status = res.data.msg.status;
									// getApp().globalData.times = res.data.msg.times;
									// getApp().globalData.total_times = res.data.msg.total_times;
									this.isAuth = getApp().globalData.isAuth;
									this.isAuthPhone = getApp().globalData.user_phone;
									let lastId =  getApp().globalData.lastId ? getApp().globalData.lastId : res.data.msg.id;
									this.getPageInfo(res.data.msg.id);
									this.getShareInfo(lastId)
				 				}
				 			});
				 	}
				 });
			 },
			 getPageInfo(uId){ //获取首页面基本信息接口
			     uni.showLoading({
			     	title: '加载中',
			     });
				 let that = this;
				uni.request({
					url: getApp().globalData.shareUrl, //需要设置为全局
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						method: 'getHomePageInfo',
						timestamp: getApp().globalData.globalTimestamp, //Date.now()
						id: 1,
						sign: md5('getHomePageInfo' + getApp().globalData.globalTimestamp)
					},
					success: res => {
						if (res.data.code === 200) {
							uni.hideLoading();
							that.pageInfo = res.data.msg;
							getApp().globalData.shareImg = res.data.msg.picture;
							getApp().globalData.shareContent = res.data.msg.sharecontent;
							getApp().globalData.shareTaobaoCode = res.data.msg.copycontent;
							getApp().globalData.shareStoreImg = res.data.msg.heading;
						}
					}
				});
			},
			 showShareModal(){
			         // 需要在 popup 组件，指定 ref 为 popup
			 		if(this.isAuthPhone){
						this.$refs.popup.open();
			 		}else {
			 			uni.navigateTo({
			 				url: '../auth/index',
			 				success: res => {},
			 				fail: () => {},
			 				complete: () => {}
			 			});
			 		}
			       },
			 cancelModal(){
			 	          // 需要在 popup 组件，指定 ref 为 popup
			 	     this.$refs.popup.close();
			 },
			 goPostShare() {
						this.$refs.popup.close()
						uni.navigateTo({
							url: '/pages/index/postShare/index'
						})
					},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		// background-image: url('/static/bg-home.jpg');
		// background-repeat: no-repeat;
		// background-size: 100% 100%;
	}

	.logo {
		width: 100%;
		position: relative;
	}
	.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>

