<template>
<view id="phone">
    <view class="phone__warning">
        <i class="iconfont icon-anquanjinggao"></i>
    </view>
    <view class="phone__text">
        为确保您账户的安全及正常使用，依《网络安全法》相关要求，6月1日起会员账户需绑定手机。如您还未绑定，请尽快完成，感谢您的理解及支持！
    </view>
    <!-- #ifdef MP-WEIXIN -->
    <button class="wechat-phoneBtn phone__btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
        <i class="iconfont icon-hudong"></i>
        <view class="btn__text">使用注册微信手机号码</view>
    </button>
    <!-- #endif -->
    <view class="again-phoneBtn phone__btn" @tap="jumpPhone">
        <i class="iconfont icon-hudong"></i>
        <view class="btn__text">绑定新手机号码</view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      code: '',
      url: ''
    };
  },

  onLoad(e) {
    if (e.url) {
      this.setData({
        url: e.url
      });
    }
  },

  components: {},
  props: {},
  methods: {
    getCode() {},

    jumpPhone() {
      if (this.url) {
        wx.navigateTo({
          url: '/pages/user/bindingphone/bindingphone?url=' + this.url
        });
      } else {
        wx.navigateTo({
          url: '/pages/user/bindingphone/bindingphone'
        });
      }
    },

    getPhoneNumber(e) {
      if (e.detail.encryptedData) {
        wx.login({
          success: res => {
            if (res.code) {
              this.setData({
                code: res.code
              }, res => {
                this.phone(e);
              });
            } else {
              wx.showModal({
                content: " 获取code失败",
                showCancel: false
              });
            }
          }
        });
        console.log(this.code);
        return;
      } else {
        this.jumpPhone();
      }
    },

    phone(e) {
      this.$http.post({
        api: 'api/users/MiniProgramBindMobile',
        data: {
          code: this.code,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        header: {
          Authorization: this.$cookieStorage.get('user_token')
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            wx.showModal({
              content: '绑定成功',
              showCancel: false,
              success: res => {
                if (res.confirm || !res.cancel && !res.confirm) {
                  if (this.url) {
                    wx.redirectTo({
                      url: '/' + decodeURIComponent(this.url)
                    });
                  } else {
                    wx.switchTab({
                      url: '/pages/user/personal/personal'
                    });
                  }
                }
              }
            });
          } else {
            wx.showModal({
              content: res.message || '绑定失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: '请求失败，请重试',
            showCancel: false
          });
        }
      }).catch(rej => {
        wx.showModal({
          content: '请求失败，请重试',
          showCancel: false
        });
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "phone";
</style>
