import {
  wxAuthMobile_Api,
  wxRegister_Api,
  // wxUserInfo_Api
} from "@/api/login.js"
export default {

  data () {
    return {
      isLoading: false,
      code: ''
    };
  },
  watch: {
    isLoading: {
      handler (newL, oldL) {
        if (newL) {
          uni.showLoading({
            title: '登录中...'
          })
        } else {
          uni.hideLoading();
        }
      },

    }
  },
  methods: {
    // 获取code
    authLogin () {
      if (!this.checked) {
        uni.showToast({
          title: '请先勾选隐私协议!',
          icon: 'none'
        });
        return false
      }
      if (this.isLoading) return
      this.isLoading = true;
      uni.login({
        provider: 'weixin', //使用微信登录
        success: async (loginRes) => {
          if (loginRes.errMsg && loginRes.errMsg == 'login:ok' && loginRes.code) {
            this.code = loginRes.code;
            this.wxAuthorization(loginRes.code);
          } else {
            this.isLoading = false;
            uni.showToast({
              title: '登录失败!',
              icon: 'none'
            });
          }
        },
        fail: () => {
          this.isLoading = false;
          uni.showToast({
            title: '登录失败!',
            icon: 'none'
          });
        }
      })
    },
    // 获取手机号码
    getPhoneNumber (e) {
      uni.showLoading({
        title: "手机号获取中",
      })
      const {
        detail
      } = e || {}
      if (detail.errMsg === "getPhoneNumber:ok") {
        wxAuthMobile_Api({
          openId: this.openId,
          ...detail
        }).then(res => {
          const {
            mobile
          } = res.data || {}
          console.log('getPhoneNumber = ', mobile)
          if (mobile) {
            this.registerAndLogin(mobile)
          } else {
            this.getPhoneNumberError()
          }
        }).catch(err => {
          console.log("err = ", err);
          this.getPhoneNumberError()
        }).finally(() => {
          uni.hideLoading()
        })
      } else {
        this.getPhoneNumberError()
      }
    },
    getPhoneNumberError () {
      uni.hideLoading();
      uni.showToast({
        title: '获取手机号码失败!',
        icon: 'none'
      })
    },
    registerAndLogin (phone) {
      const sharerInfo = uni.getStorageSync('sharerInfo')
      this.isLoading = true
      wxRegister_Api({
        "mobile": phone,
        "open_id": this.openId,
        "invitation_code": sharerInfo.invitation_code
      }).then(res => {
        const {
          token
        } = res.data || {};
        this.getUser(token)
        console.log('registerAndLogin -= ', res)
      }).catch(err => {

      }).finally(() => {

      })
    },
    getUser (token) {
      uni.setStorageSync('apiToken', token);
      // wxUserInfo_Api().then(res => {})
      this.$store.dispatch('getUserInfo').then(res => {
        uni.setStorageSync("personal", res.data)
        this.LoginSuccess()
      })
    },
    LoginSuccess (token) {
      uni.switchTab({
        url: '/pages/index/lidaPage'
      })
    },
    close () {

    }
  }
}