<template>
	<!-- 1.获取购票凭证页面，索道等景区进入添加企业微信获取电子门票 -->
	<!--2.判断是否添加，已经添加直接进入订单列表页面 ，没有添加就添加企业微信-->
	<!-- 3.如果企业微信人数达到上线，直接跳转到订单列表页面 -->
	<!-- 4.如果企业微信码停用，走公众号流程 -->
	<!-- 5.暂时没用，用的公众号 -->
	<view class="bigBox">
		<image src="../../../static/img/common/getTicketBg.jpg" class="bigImg"></image>
		<view class="title">获取电子门票</view>
		<!-- 企业微信流程 -->
		<view v-if="showCompany">
			<view class="imgbox">
				<image :src="imgUrl" style="width:360rpx;height:360rpx;"></image>
			</view>
			<view class="content1">长按二维码，添加“客服”</view>
			<view class="content2">获取您的电子门票</view>
		</view>
	
		<!-- 公众号流程 -->
		<div v-if="showPublic">
			<view class="imgbox">
				<image :src="imgUrl" style="width:360rpx;height:360rpx;"></image>
			</view>
			<view class="content1">长按二维码，关注“胖丁旅游”</view>
			<view class="content2">获取您的电子门票</view>
		</div>
	</view>
</template>

<script>
export default {
  data() {
    return {
      showCompany:false,//企业微信流程
      showPublic:false,//公众号流程
      merchantId:'',//商户Id
      companyId:'',//公司Id
      imgUrl:'',//图片地址
      enterUrl:'',//进入页面路由
    }
  },
  onLoad(option) {
    //#ifdef MP-WEIXIN
    this.enterUrl=option.q
    //#endif
    if(this.enterUrl){
      this.merchantId=this.getUrlKey('merchantId')
      this.companyId=this.getUrlKey('companyId')
    }else{
      this.merchantId=option.merchantId
      this.companyId=option.companyId
    }
    this.ifAddWxUser()
  },
  methods: {
    //----获取url
    getUrlKey(name){
		  return(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(decodeURIComponent(this.enterUrl)) || [, ''])[1].replace(/\+/g, '%20') || ''
    },
    //---是否添加企业微信
    ifAddWxUser(){
      let data = {
			  openid:uni.getStorageSync('openid'),//openId
      }
      this.$request('wechatUser/login/findWxUserAndLogin',data).then(res => {
			  if (res.code == '00') {
          if(res.data.alreadyAddAttendant=='1'){
            //已添加
            uni.navigateTo({
						  url: '/pages/my/order/orderList/orderList'
            })
          }else{
            //未添加
            this.getCompanyCode()
          }
			  } else {
			    uni.showToast({
			      title: res.message,
			      icon: 'none'
			    })
			  }
      })
    },
    //---获取企业微信二维码编号
    getCompanyCode() {
		  let data = {
		    merchantId: this.merchantId,
		    companyId:this.companyId
		  }
		  this.$request('scenic/wechatCodeConfig/load',data).then(res => {
		    if (res.code == '00') {
          //微信码正常
          if(res.data.status=='0'){
            //达到上线跳转小程序页面
            if(res.data.alreadyAddNumber>=res.data.canAddNumber){
						  uni.navigateTo({
						    url: '/pages/my/order/orderList/orderList'
						  })
            }else{
              this.showCompany=true
              this.imgUrl = res.data.codeUrl
            }
          }else{
            //微信码停用获取公众号二维码
            this.showPublic=true
            this.getCode()
          }
		    } else {
		      uni.showToast({
		        title: res.message,
		        icon: 'none'
		      })
		    }
		  })
    },
    //---获取公众号二维码编号
    getCode() {
		  let data = {
		    codeType: 1,
		    select: 1,
		    companyId: this.companyId
		  }
		  this.$request('user/company/generateQrCodeUrl', data).then(res => {
		    if (res.code == '00') {
		      this.imgUrl = res.data.codeImgUrl
		    } else {
		      uni.showToast({
		        title: res.message,
		        icon: 'none'
		      })
		    }
		  })
    },	
  }
}
</script>

<style>
.bigBox {
  height: 100%;
  padding-top: 18vh;
  text-align: center;
  font-weight: bold;
  font-size:28rpx;
  box-sizing: border-box;
	position: relative;
}
.bigImg{
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	z-index: -1;
}
.title {
  font-size:66rpx;
  font-weight: bold;
  color: #ff570e;
}
.imgbox {
  margin-top: 6vh;
}
.content1 {
  margin-top: 6vh;
}
.content2 {
  margin-top: 3vh;
}
@media only screen and (max-height:760px) {
	.bigBox {
	  padding-top: 16vh;
	}
  .imgbox {
    margin-top: 5vh;
  }
  .content1 {
    margin-top: 5vh;
  }
  .content2 {
    margin-top: 2vh;
  }
}
</style>
