// shoppingGuid/page/pages/openCard/openCard.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const utils = require('../../../../utils/util')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    showOpenCardModal : false,
    imageUrl: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_u_open_card_small.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断用户是不是已经登录了
    let userInfo = wx.getStorageSync('_baseUserInfo');
    if (userInfo) {
      wx.reLaunch({
        url: '/pages/userCenter/userCenter',
      });
      return;
    }

    //未登录 需要授权登录 弹出设置的开卡图片
    this.setData({
      showOpenCardModal: true,
    });
  },

  //进入首页
  onTapToMall(){
    wx.reLaunch({
      url: '/pages/userCenter/userCenter',
    });
  },

  //授权回调
  userAuthBtnCallBack(res = {}) {
    wx.showLoading({
      title: '请求中..',
    });
    
    const userInfo = res.detail || {};
    if (res.detail.userInfo) {
      utils.getUserInfoByBtn(userInfo).then((data = {}) => {
        const { token } = data
        wx.setStorageSync('_accreditUserInfo', userInfo);
        wx.setStorageSync('token', token ? token : '');
        wx.setStorageSync('_baseUserInfo', data);
        // 存储用户开卡状态
        this.setUserInfo();
      }).catch(err => {
        if (err) {
          wx.showToast({
            title: `会员系统异常请稍后重试!`,
            icon: 'none'
          })
        }
      })
    } else {
      wx.showToast({
        title: `授权失败，请核实！`,
        icon: 'none'
      });
    }
  },

  //设置用户信息
  setUserInfo() {
    // 判断有无开卡
    // const {memberActivateStatus} = wx.getStorageSync('_baseUserInfo')
    const userInfo = wx.getStorageSync('_baseUserInfo')
    // 新用户去激活领卡 
    if (userInfo && userInfo.member && !userInfo.member.mobile) {
      wxService.openCard()
      return false
    }

    //如果已经是会员则跳转到首页
    wx.reLaunch({
      url: '/pages/userCenter/userCenter',
    });
  },

});