index.js 4.29 KB
Newer Older
程默 committed
1
function formatNumber(n) {
程默 committed
2 3 4
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
程默 committed
5 6 7
// 防抖
export function debounce(fn, wait) {
  var timeout = null;
程默 committed
8 9 10 11
  return function () {
    if (timeout !== null)
      clearTimeout(timeout);
    timeout = setTimeout(fn, wait);
程默 committed
12 13 14 15 16 17
  }
}

// 节流
export function throttle(func, delay) {
  var prev = Date.now();
程默 committed
18 19 20 21 22 23 24 25
  return function () {
    var context = this;
    var args = arguments;
    var now = Date.now();
    if (now - prev >= delay) {
      func.apply(context, args);
      prev = Date.now();
    }
程默 committed
26 27
  }
}
程默 committed
28 29

//格式时间
程默 committed
30
export function formatTime(date) {
程默 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
  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}`
}

程默 committed
45 46 47
//对象转querystring
export function serialize(obj) {
  var str = [];
程默 committed
48 49 50
  if (typeof obj == "string") {
    obj = JSON.parse(obj);
  }
程默 committed
51 52 53 54 55 56 57 58
  for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  }
  return str.join("&");
}

程默 committed
59
//获取url参数
程默 committed
60
export function getQueryVariable(query, variable) {
程默 committed
61 62 63 64 65 66
  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);
程默 committed
67
}
程默 committed
68 69 70

// url完整链接把参数转成对象
export function parseQueryString(url) {
李嘉林 committed
71
  let str = url.split("?").length > 1 ? url.split("?")[1] : url,
程默 committed
72 73 74 75 76 77 78 79 80 81
    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
程默 committed
82
  }
程默 committed
83
  return obj
程默 committed
84
}
程默 committed
85 86

//补全图片路径
程默 committed
87
export function DFSImg(path, w, h, type = 0) { //
程智春 committed
88 89 90
  if (path == null || path == '') {
    return 'https://mayi-newshop.oss-cn-shanghai.aliyuncs.com/product/8ezKQbm2x3.png';
  }
程默 committed
91
  let baseImg = process.env.IMG_DOMAIN;
李嘉林 committed
92
  if (!Array.isArray(baseImg) && (baseImg.indexOf('aliyun') >= 0 || baseImg.indexOf('cdn') >= 0) || baseImg.indexOf('cmecloud') >= 0) {
程默 committed
93 94 95 96
    var style = '';
    if (w) style += ',w_' + w;
    if (h) style += ',h_' + h;
    if (style.length > 0) {
程默 committed
97 98
      if (path.indexOf('?x-oss-process') == -1) {
        if (type == 1) {
程智春 committed
99
          path += '?x-oss-process=image/resize,limit_0' + style
程默 committed
100
        } else {
程智春 committed
101 102
          path += '?x-oss-process=image/resize,m_pad,limit_0' + style
        }
程默 committed
103

程默 committed
104
      }
程默 committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }
    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';
    }
程智春 committed
120

李嘉林 committed
121
    return baseImg + path;
程默 committed
122
  }
程默 committed
123
}
李嘉林 committed
124 125 126 127 128 129 130 131 132 133 134 135
// 获取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 //导航栏高度
      })
    }
  })
}
程智春 committed
136 137


程默 committed
138
export function setTimes(value) {
程智春 committed
139
  let result = parseInt(value)
程默 committed
140 141 142 143 144
  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
张卓 committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}
//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
  }
程智春 committed
170
}