util.js 1.4 KB
Newer Older
S
docs  
shaoxuezheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
const 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()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const goToLink = url => {
  wx.navigateTo({
    url
  })
}
Z
zhuzhiyong 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

const getPartUrlByParam = (url, param) => {
  const reg = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
  const res = reg.exec(url)
  const fields = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash'];
  return res[fields.indexOf(param)]
}

const search2Json = search => {
  if (search != undefined) {
    let o = {}
    const arr = search.split('&')
    arr.forEach(item => {
        const a = item.split('=')
        o = {
            [a[0]]: a[1]
        }
    })
    return o
  } else {
    return {}
  }
}

const isArray = list => {
  return Object.prototype.toString.call(list).slice(8, -1) == 'Array'
}

Z
zhuzhiyong 已提交
49 50 51 52 53 54 55 56 57 58 59 60
const deepClone = obj => {
  if(typeof obj !="object"){
      return obj
  }
  var newObj = {};
  for (var key in obj) {
    newObj[key] = deepClone(obj[key])
  }
  return newObj;
}


Z
zhuzhiyong 已提交
61

S
docs  
shaoxuezheng 已提交
62 63
module.exports = {
  formatTime: formatTime,
Z
zhuzhiyong 已提交
64 65 66
  goToLink,
  search2Json,
  getPartUrlByParam,
Z
zhuzhiyong 已提交
67 68
  isArray,
  deepClone
S
docs  
shaoxuezheng 已提交
69
}