function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
// 防抖
export function debounce(fn, wait) {
  var timeout = null;
  return function () {
    if (timeout !== null)
      clearTimeout(timeout);
    timeout = setTimeout(fn, wait);
  }
}

// 节流
export function throttle(func, delay) {
  var prev = Date.now();
  return function () {
    var context = this;
    var args = arguments;
    var now = Date.now();
    if (now - prev >= delay) {
      func.apply(context, args);
      prev = Date.now();
    }
  }
}

//格式时间
export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

//对象转querystring
export function serialize(obj) {
  var str = [];
  if (typeof obj == "string") {
    obj = JSON.parse(obj);
  }
  for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  }
  return str.join("&");
}

//获取url参数
export function getQueryVariable(query, variable) {
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) { return pair[1]; }
  }
  return (false);
}

// url完整链接把参数转成对象
export function parseQueryString(url) {
  let str = url.split("?").length > 1 ? url.split("?")[1] : url,
    items = str.split("&");
  let arr, name, value;
  let obj={}

  for (var i = 0; i < items.length; i++) {
    arr = items[i].split("=");    //["key0", "0"]
    name = arr[0];
    value = arr[1];
    console.log(81, this)
    obj[name]=value
  }
  return obj
}

//补全图片路径
export function DFSImg(path, w, h, type = 0) { //
  if (path == null || path == '') {
    return 'https://mayi-newshop.oss-cn-shanghai.aliyuncs.com/product/8ezKQbm2x3.png';
  }
  let baseImg = process.env.IMG_DOMAIN;
  if (!Array.isArray(baseImg) && (baseImg.indexOf('aliyun') >= 0 || baseImg.indexOf('cdn') >= 0) || baseImg.indexOf('cmecloud') >= 0) {
    var style = '';
    if (w) style += ',w_' + w;
    if (h) style += ',h_' + h;
    if (style.length > 0) {
      if (path.indexOf('?x-oss-process') == -1) {
        if (type == 1) {
          path += '?x-oss-process=image/resize,limit_0' + style
        } else {
          path += '?x-oss-process=image/resize,m_pad,limit_0' + style
        }

      }
    }
    if (path.indexOf('http') == 0) {
      return path;
    }
    return baseImg + path;
  } else {
    if (path.indexOf('http') == 0) {
      return path;
    }
    if (path.indexOf('/') != 0) {
      path = '/' + path;
    }
    if (w && h) {
      path += '.' + w + 'x' + h + '.jpg';
    }

    return baseImg + path;
  }
}
// 获取Navbar高度
export function getNavbarInfo(fn) {
  let menuInfo = wx.getMenuButtonBoundingClientRect(); //右上角胶囊信息
  wx.getSystemInfo({
    success: (res) => {
      fn({
        navTop: res.statusBarHeight, //状态栏高度
        navHeight: res.statusBarHeight + menuInfo.height + (menuInfo.top - res.statusBarHeight) * 2 //导航栏高度
      })
    }
  })
}


export function setTimes(value) {
  let result = parseInt(value)
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600)
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60))
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60))
  result = `${h}:${m}:${s}`
  return result
}
//url拼接参数
export function concatUrl(url,data) {
  let setUrl = url;
  if(data) {
    let cludeData = true;
    if(setUrl.indexOf("?") == -1) {
      setUrl += "?";
      cludeData = false;
    }
    let index = 0;
    for(let key in data) {
      if(!cludeData&&index===0) {
        setUrl += key+"="+data[key];
      }else if(!cludeData&&index!==0) {
        setUrl += "&"+key+"="+data[key];
      }else if(cludeData&&setUrl.indexOf(key+"=") == -1) {
        setUrl += "&"+key+"="+data[key];
      }
      index += 1
    }
    return setUrl
  }else {
    return setUrl
  }
}