import WelfareMall from '../api/welfareMall'
function getUserInfo() {
    return wx.getStorageSync('userInfo') || null;
}

function getMobileCache() {
    const userInfo = getUserInfo()
    if (userInfo != null) {
        return userInfo.mobile || ''
    }
    return ''
}

function getPhoneNumber(e, func) {
    var encryptedData = e.detail.encryptedData;
    console.log(encryptedData);
    var iv = e.detail.iv;
    if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
      return;
    }
    //获取手机号
    getApp().doDecodePhone(encryptedData, iv, function () {
      func(getMobileCache())
    });
}
function getPhoneNumberNew(e, func) {
    var code = e.detail.code;
    console.log(code);
  
    //如果版本过低 没有code 就用之前的放吧
    if (!code || code.length == 0 ) {
      getPhoneNumber(e,func)
    }else{
        //获取手机号
        getApp().doDecodePhoneNew(code, function () {
          func(getMobileCache())
        });
    }
  
}

/**
 * 获取统一颜色
 */
async function getColor(){
  let configinfo = wx.getStorageSync('configinfo') || null 
  if(configinfo != null){
    return configinfo;
  }
  let res = await WelfareMall.getIndexList();
  //数据重组
  let info = res.data;
  delete info['activityList'];
  delete info['adList'];
  delete info['bannerList'];
  delete info['navigationList'];
    //颜色存到缓存中
  wx.setStorageSync('configinfo',info);
  return info;
}

module.exports = {
    getUserInfo,
    getMobileCache,
    getPhoneNumber,
    getPhoneNumberNew,
    getColor
}
  
