<!--登录页面-->
<script>
import ApiConstant from "@/common/ApiConstant";
import Request from '@/common/request'
import StaticConstant from "@/common/StaticConstant";
import commonTool from "@/common/js/commonTool";

let request = new Request()
export default {
  name: "home",
  components: {},
  computed: {
    StaticConstant() {
      return StaticConstant
    }
  },
  data() {
    return {
      showType: 0,
      uuid:'',
      showFlag: false
    }
  },
  onReady() {
  },
  onLoad: function (options) {
    if(options.scene){
      console.log('scene')
      this.uuid = decodeURIComponent(options.scene)
      console.log(options.scene)
      console.log(this.uuid)
	  this.getQrCodeStatus()

    }
  },
  onShow() {
    this.showFlag = true
  },
  methods: {
    //刷新到首页
    reloadHome() {
      setTimeout(function () {
        wx.reLaunch({
          url: '/pages/zyb/home',
        })
      }, 500)
    },
    getPhoneNumber(e) {
      if(!this.uuid){
        return
      }
      if (e && e.detail && e.detail.code) {
        //选择授权
        uni.login({
          success: (res) => {
            console.log('获取code', res)
            if (res.code) {
              let code = res.code
              request.post(ApiConstant.User.wxLogin, {
                uuid:this.uuid,
                code: code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv
              }).then(r => {
                console.log('请求登录', r)
                if (r.success) {
                  console.log(r)
                  //token
                  uni.setStorageSync('token', r.result.token)
                  //用户信息
                  if (r.result.userInfo) {
                    uni.setStorageSync('userInfo', r.result.userInfo)
                  }
                  //vip信息
                  commonTool.checkVipInfo(r.result.vipInfo)
                  //重新打开首页
                  this.showType = 2
                } else {
                  uni.showToast({
                    title: r.message,
                    icon: 'none'
                  })
                }
              }).catch(err => {
              }).finally(() => {
              });
            } else {
              uni.showModal({
                title: '未获取到code'
              })
            }
          }
        });
      } else {
        uni.showToast({
          title: '授权失败,请同意授权',
          icon: 'none'
        })
        return
      }
      console.log(e)
    },
    gotoMianze() {
      uni.navigateTo({
        url: '/pages/zyb/other/disclaimer'
      })
    },
    gotoXieyi() {
      uni.navigateTo({
        url: '/pages/zyb/other/userAgreement'
      })

    },
	//获取二维码状态
	getQrCodeStatus(){
	  request.get(ApiConstant.User.wxQrCodeStatus+"/"+this.uuid, {}, {showLoad: false}).then(r => {
	    if (r.success) {
			if(r.result.qr_state === 'SUCCESS' || r.result.qr_state === 'SCAN' || r.result.qr_state === 'NULL'){
				//已通过
				this.showType = 3
			}else{
				this.showType = 1
				this.reloadQrCodeStatus()
			}
	    }else{
			this.showType = 3
		}
	  }).catch(err => {
	  }).finally(() => {
	  });
	},
    //更新二维码状态
    reloadQrCodeStatus(){
      //将微信登录二维码关闭
      request.get(ApiConstant.User.wxQrCodeUpdateStatus+"/"+this.uuid, {}, {showLoad: false}).then(r => {
        if (r.success) {
        }
      }).catch(err => {
      }).finally(() => {
      });
    },
    //取消
    exit(){
      wx.exitMiniProgram({
        success: (res) => {}
      })
    },
	exitQrCode(){
		wx.exitMiniProgram({
		  success: (res) => {
		    console.log(res)
		    if(this.uuid){
		      //将微信登录二维码关闭
		      request.get(ApiConstant.User.wxQrCodeExit+"/"+this.uuid, {}, {showLoad: false}).then(r => {
		        if (r.success) {
		        }
		      }).catch(err => {
		      }).finally(() => {
		      });
		    }
		  }
		})
	}
  },
}
</script>

<template>
  <view class="header" style="border-top:3rpx solid #f6f6f6"></view>
  <view class="content" v-if="showFlag && showType===1">
    <view class="textcenter">
      <view class="flex">
        <image src="/static/icons/wechat.png" class="icon256 marginCenter" />
      </view>
      <text class="low-title">
        将在电脑上登录{{ StaticConstant.systemName }}
      </text><br/>
      <text class="low-title">
        请确认是否是本人操作
      </text>
    </view>
    <view style="margin-top: 200rpx" >
      /*#ifdef MP-WEIXIN*/
      <button class='login-but' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
        手机号快捷登录
      </button>
      /*#endif*/
      <button class='login-pwd-but margin-top-20' @click="exitQrCode()">
        取消
      </button>
    </view>
    <view class="uni-flex flex" style="margin-top: 100rpx" v-show="false">
      <checkbox-group @change="checkboxChange">
        <label>
          <checkbox value="cb"/>
          登录代表{{ StaticConstant.systemName }}
          <text style="color: #4975fd" @click="gotoXieyi">{{commonTool.getSystemName()}}用户协议、</text>
          <text style="color: #4975fd" @click="gotoMianze">免责声明</text>
          ，
          并授权使用您的{{ StaticConstant.systemName }}账号信息（如昵称、头像）以便您统一管理
        </label>
      </checkbox-group>
    </view>
  </view>
  <view class="content" v-else-if="showFlag && showType===2">
    <view class="textcenter">
      <view class="flex">
        <image src="/static/icons/success.png" class="icon256 marginCenter" />
      </view>
      <text class="low-title">
        登录成功
      </text><br/>
      <text class="low-title">
        欢迎使用{{ StaticConstant.systemName }}
      </text>
    </view>
    <view style="margin-top: 200rpx" >
      <button class='login-but' @click="reloadHome()">
        确定
      </button>
    </view>
  </view>
  <view class="content" v-else-if="showFlag && showType===3">
    <view class="textcenter">
      <text class="low-title">
        二维码已失效
      </text><br/>
    </view>
    <view style="margin-top: 200rpx" >
      <button class='login-but' @click="exit()">
        确定
      </button>
    </view>
  </view>

</template>

<style scoped lang="scss">
.content {
  margin-top: 200rpx;
  padding: 30rpx;
}

.container {
  height: 100%;
}

.uni-form-item .title {
  padding: 20rpx 0;
}

.textcenter {
  text-align: center;
  margin: 30rpx 0;
}

/*标题*/
.title {
  font-family: "Bookshelf Symbol 7";
  margin: 0 auto;
  //color:white;
  font-size: 90rpx;
  font-weight: 600;
}

/*小标题*/
.low-title {
  font-size: 40rpx;
}

///登录按钮
.login-but {
  color: white;
  width: 500rpx;
  background-color: #25cd4d;
  border-radius: 10rpx;
  font-weight: 550;
}
.login-pwd-but{
  width: 500rpx;
}

.login-but-disabled {
  background: #eeeeee;
}
</style>
