<!--
 * @Author: euzhi
 * @Description: 登录
-->
<template>
  <view class="appCotent">


    <div class="binding">
      <div class="login__logo">
        <image src="@/static/binding/logo.png">
      </div>
      <!-- #ifdef MP-WEIXIN -->
      <!-- <view v-if="isCanUse">
          <view>
              <button class="login__btn" hover-class="none" type='primary' @click="getUserProfile">
                  微信一键登录
              </button>
          </view>
      </view> -->
      <view v-if="isCanUse2">
        <view>
          <button class="login__btn" hover-class="none" type='primary' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
            微信一键登录
          </button>
        </view>
      </view>
      <!-- #endif -->


      <div class="login__read">
        <u-checkbox-group placement="row">
          <u-checkbox activeColor="green" v-model="checked"></u-checkbox>
        </u-checkbox-group>
        已阅读并同意 <span>《京东用户注册协议》</span>、<span>《京东隐私政策》</span>。若您的手机号未注册，将为您直接注册京东账号
      </div>
    </div>
  </view>
</template>
<script>

export default {
    data() {
        return {
          checked: true,
          code: "",
          isCanUse: false, //默认为true
          isCanUse2: false, //默认为true
          openid: '',
          nc_protocol : false,
		  //扫描二维码参数
          scene : "",
		  //检查绑定挪车码
		  checkCode: {},
        };
    },
  methods: {
    //登录
    login() {
      let _this = this;
      uni.showLoading({
        title: '登录中...'
      })
    },
	//检查挪车码存在、是否绑定
	getCheckMoveCode(){
		let opts = { url: '/app/index/check/move/code', method: 'post' };
		this.http.httpTokenRequest(opts, {code:this.scene}).then(res => {
			if (res.data.code == 200) {
				this.checkCode = res.data.data
				//未绑定了的情况
				if(!this.checkCode.bindFlag){
					uni.navigateTo({
					    url: '/pagesB/pages/move-car/scan?moveCode=' + this.checkCode.code
					})
				}else if(this.checkCode.selfFlag){
					
				}else{
					
				}
			}
		})
	},
    async wechatLogin() {
      wx.login({
        success: (res) => {
          this.code = res.code
          this.wechatSession(res.code)
        }
      })
    },
    //获取session_key
    async wechatSession(code) {
      let that = this
      let param = {
        code: code,
      }
      let opts = { url: '/xcx/code/session', method: 'post' }

      await that.http.httpRequest(opts, param).then(res => {
        if (res.data.code == 200) {
          that.openid = res.data.data.openid
        }
      })
    },
    //解析手机号
    getPhoneNumber(e) {
      let that = this
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        that.decodePhoneLogin(e.detail)
      } else {
        console.log("用户点击了拒绝")
      }
    },
    //解密手机号码
    async decodePhoneLogin(detail) {
      let encryptedData = detail.encryptedData
      let iv = detail.iv
      let that = this

      let param = {
        code: detail.code,
        openid : that.openid
      }
      let opts = { url: '/xcx/login', method: 'post' }

      await that.http.httpRequest(opts, param).then(res => {
        if (res.data.code == 200) {
          uni.showToast({
            title:'授权成功',
            duration:1500
          })
          uni.setStorageSync("userToken", res.data.data.token)

          if(this.scene) {
            let param = {code : this.scene}
            opts = { url: '/app/index/check/move/code', method: 'post' }
            that.http.httpTokenRequest(opts, param).then(res => {
              console.info(res);
              if (res.data.code == 200) {
                if(res.data.data.bindFlag) {
                  that.gotoMoveCar();
                } else {
                  that.gotoBindMoveCar();
                }
              }
            })

          } else {
            that.gotoIndex()
          }

        } else {
          uni.showToast({
            title:'授权失败',
            icon:'none',
          })
        }
      })


    },
    gotoIndex() {
      //跳转到绑定页面
      uni.reLaunch({ //信息更新成功后跳转到小程序首页
        //todo 京东账号绑定界面暂缓
        //url: '/pages/binding/binding'
        url: '/pagesA/pages/index/index'
      })

      // uni.navigateTo({
      //             url: '/pages/binding/binding'
      //          });

    },
    //跳转到挪车页面
    gotoMoveCar() {
      let that = this;
      uni.navigateTo({
        url: '/pagesB/pages/move-car/oneMoveCar?scene=' + that.scene
      });
    },
    //跳转到绑定页面
    gotoBindMoveCar() {
      let that = this;
      uni.navigateTo({
        url: '/pagesB/pages/move-car/scan?moveCode=' + that.scene
      });
    },
  },
  onLoad(e) { //默认加载
    // #ifdef MP-WEIXIN
    if(e.query) {
      //获取二维码携带的参数
      //扫码后得到的参数
      let scene = decodeURIComponent(e.query.scene)
      // scene = scene.split('&')
      // let data = {
      // 	//场景值
      // 	scene: e.scene
      // }

      this.scene = scene;
    }



    this.wechatLogin()

    let userToken = uni.getStorageSync('userToken')
    if (userToken) {
      let that = this
      let opts = { url: '/app/user/info', method: 'get' }

      that.http.httpTokenRequest(opts, {}).then(res => {
        if (res.data.code == 200) {
          if(that.scene) {
            let param = {code : this.scene}
            opts = { url: '/app/index/check/move/code', method: 'post' }
            that.http.httpTokenRequest(opts, param).then(res => {
              console.info(res);
              if (res.data.code == 200) {
                  if(res.data.data.bindFlag) {
                    that.gotoMoveCar();
                  } else {
                    that.gotoBindMoveCar();
                  }
              }
            })

          } else {
            this.gotoIndex()
          }

        } else {
          uni.showToast({
            title:'授权失败',
            icon:'none',
          })
          this.isCanUse2 = true
        }
      })
    } else {
      this.isCanUse2 = true
    }
    // #endif
  }

}
</script>

<style lang="scss" scoped>
.binding {
    .login__logo {
        padding-top: 80rpx;
        text-align: center;
        padding-bottom: 100rpx;

        image {
            width: 174rpx;
            height: 174rpx;
        }
    }

    .u-checkbox-group {
        display: inline-block !important;
    }

    /deep/.u-checkbox-group--row {
        display: inline-block !important;
    }

    .login__btn {
        width: 600rpx;
        height: 90rpx;
        margin: 0 auto;
        border-radius: 20rpx;
        background: url('@/static/binding/yuan-btn.png') no-repeat;
        background-size: contain;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: PingFang SC;
        font-weight: bold;
        color: #FFFFFF;
        opacity: 0.96;
        letter-spacing: .5em;
        margin-bottom: 32rpx;
    }

    .login__read {
        margin: 0 auto;
        width: 600rpx;
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #666666;
        line-height: 36rpx;

        span {
            color: rgb(0, 81, 255);
        }
    }
}
</style>
