<style lang="less">
.login-box {
  padding-top: 40px;
  .avatar-wrapper {
    padding: 0px;
    width: 196rpx;
    height: 196rpx;
    border-radius: 50%;
    image {
      width: 196rpx;
      height: 196rpx;
      border-radius: 50%;
    }
  }
  .avatar-tip {
    text-align: center;
    font-family: PingFang SC;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 44rpx;
    text-align: center;
  }

  .bind-btn-box {
    position: fixed;
    bottom: 228rpx;
    left: 50%;
    transform: translateX(-50%);
    .bind-btn {
      width: 548rpx;
      height: 100rpx;
      line-height: 100rpx;
      border-radius: 56rpx;
      background: rgba(61, 203, 204, 1);
      border: none;
      font-family: PingFang SC;
      font-size: 32rpx;
      font-weight: 400;
      letter-spacing: 0px;
      text-align: center;
      color: #fff;
    }
    .bind-btn-tip {
      //styleName: 14/常规;
      font-family: PingFang SC;
      padding-top: 32rpx;
      font-size: 28rpx;
      font-weight: 400;
      line-height: 44rpx;
      text-align: center;
      color: rgba(139, 148, 158, 1);
    }
  }

  .home-btn {
    margin-top: 240rpx;
    width: 548rpx;
    height: 100rpx;
    line-height: 100rpx;
    border-radius: 56rpx;
    border: 1px solid rgba(61, 203, 204, 1);
  }
}
</style>
<template>
 <div class="login-box">
  <button class="avatar-wrapper"
    v-if="canIUseGetUserProfile"
    open-type="chooseAvatar" 
    bind:chooseavatar="onChooseAvatar">
    <image class="avatar" src="{{avatarUrl}}"/>
  </button>  
  <div class="avatar-tip" v-if="canIUseGetUserProfile">可点击获取头像</div>
  <div class="bind-btn-box">
   <button class="bind-btn"
   open-type="getPhoneNumber"
   bindgetphonenumber="phonenumberAuth"
   >微信手机号认证</button>
   <!-- @click="goBindTenant" -->
   <!-- bindgetphonenumber="phonenumberAuth" -->
   <div class="bind-btn-tip">确保是入职公司使用的手机号</div>
  </div>
  <button class="home-btn" @click="goHome">首页</button>
 </div>
</template>

<script>
import wepy from '@wepy/core';
import { mapState } from '@wepy/x';
import store from '@/store';
import config from '@/config';
import { setAvatar, checkLoginNew, setUserInfoByAuth } from '@/service/user';
import { getHttpPhoneNumber } from '@/api/user';
let defaultAvatarUrl =
  config.h5StaticPath + '/page-bind-tenant/default_avatar.svg';
wepy.page({
  store,
  config: {
    navigationBarTitleText: 'test'
  },
  data: {
    nicknameValue: '',
    avatarUrl: defaultAvatarUrl,
    msg: '测试数据',
    canIUseGetUserProfile: false,
    userInformation: wx.getStorageSync('userInformation')
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.canIUseGetUserProfile = true;
    }
  },
  onShow() {
    // 过期重定向处理
    // checkLoginNew().then(res => {
    //   if (res.data && res.data.isActivated == 1) {
    //     this.goHome();
    //   } else {
    //     wx.navigateTo({
    //       url: '/pages/index'
    //     });
    //   }
    // });
  },
  methods: {
    // 绑定租户
    bindTenant() {},
    onChooseAvatar(e) {
      this.avatarUrl = e.$wx.detail.avatarUrl;
      let avatarUrl = this.avatarUrl;
      let that = this;
      wx.getFileSystemManager().readFile({
        filePath: avatarUrl,
        encoding: 'base64',
        success: function(res) {
          console.log(res.data);
          that.avatarUrl = 'data:image/png;base64,' + res.data;
          setAvatar(that.avatarUrl);
        }
      });
    },
    getPageCompanyByPhone() {},
    goHome() {
      wx.navigateTo({
        url: '/packagesEnv/pages/home/index'
      });
    },
    // 手机号认证
    phonenumberAuth(e) {
      let detail = e.$wx.detail;
      let code = detail.code;
      // store.commit('setUserPhone', 13810794283);
      // this.goBindTenant();
      this.getHttpPhoneNumber(code);
    },
    // code 换取手机号
    getHttpPhoneNumber(code) {
      getHttpPhoneNumber(code)
        .then(res => {
          let data = res.data || {};
          let phoneNumber = data.purePhoneNumber;
          if (phoneNumber) {
            store.commit('setUserPhone', phoneNumber);
            this.goBindTenant();
          } else {
            console.log('没获取到手机号');
          }
        })
        .catch(error => {
          console.log('获取手机号的接口失败');
        });
    },
    goBindTenant() {
      console.log('被点击了');
      wx.navigateTo({
        url: '/pages/bindTenant/index'
      });
    }
  },
  created() {}
});
</script>
<config>
{
navigationBarTitleText: '首页',
}
</config>
