<template>
  <view class="page DengLu">
    <image mode="widthFix" src="https://cjgw.oss-cn-hangzhou.aliyuncs.com/ygcxxfb/image/signinbg.png"></image>
    <view class="Btn flex-h">
      <view class="flex-item"><text @tap="Back">取消登录</text></view>
      <view class="flex-item"><text>授权登录</text></view>
      <button open-type="getPhoneNumber" v-if="Agree && UserInfo !== null && UserInfo.PersonId !== undefined" @getphonenumber="GetPhoneNumber">授权登录</button>
    </view>
    <view class="Agree flex-h">
      <text class="iconfont icongouxuan" v-if="Agree" @tap="Agree = false"></text>
      <text class="iconfont iconweigouxuan" v-else @tap="Agree = true"></text>
      <view class="flex-item">
        <text>我已认真阅读，理解并同意<text>《用户协议及隐私政策》</text></text>
      </view>
    </view>
  </view>
</template>

<script>
import { createNamespacedHelpers } from 'vuex'
const { mapState: mapUserState, mapActions: mapUserActions, mapMutations: mapUserMutations } = createNamespacedHelpers('user')
export default {
  name: 'DengLu',
  data () {
    return {
      DataLock: false,
      Agree: false
    }
  },
  computed: {
    ...mapUserState({
      UserInfo: x => x.UserInfo // 用户信息
    })
  },
  components: {
  },
  created () {
    this.Init()
  },
  methods: {
    ...mapUserActions([
      'MainSignIn', // 获取用户信息
      'GetUserPhone' // 获取用户手机号
    ]),
    ...mapUserMutations([
      'EditUserInfo' // 编辑用户信息
    ]),
    Init () {
      this.ToLogin()
    },
    Back () {
      wx.navigateBack({ changed: true })
    },
    ToLogin () {
      let _that = this
      wx.login({
        success (res) {
          _that.MainSignIn({ queryData: { code: res.code } }).then((res) => { // 获取用户信息
            _that.EditUserInfo({ name: 'PersonId', value: res.data.data.person.personId })
            _that.EditUserInfo({ name: 'OpenId', value: res.data.data.person.openId })
            _that.GetUserPhone({ urlData: { personId: _that.UserInfo.PersonId } })
          })
        }
      })
    },
    GetPhoneNumber (e) {
      if (this.DataLock) return
      this.DataLock = true
      console.log(e)
      console.log(e.detail.errMsg)
      console.log(e.detail.iv)
      console.log(e.detail.encryptedData)
    }
  }
}
</script>

<style lang="scss">
@import "page.scss";
</style>
