index.js 3.43 KB
Newer Older
lanbaoming's avatar
lanbaoming committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
import config from './config'

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (!time || +time === 0) {
      return '-'
  }
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['', '', '', '', '', '', ''][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option = '{y}-{m}-{d}') {
  // 字符串格式的日期需要转为时间戳(不是纯数字)
  if (!(/^\d+$/.test(time))) {
    time = (new Date(time.replace(/\-/g, '/'))).getTime()
  }

  // 10位的时间戳需要转为 毫秒时间戳
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '' +
      d.getDate() +
      '' +
      d.getHours() +
      '' +
      d.getMinutes() +
      ''
    )
  }
}

/**
 * @param {number} size
 * @returns {string}
 */
export function formatSize(size) {
    if (typeof size !== 'number') {
        size = parseInt(size)
    }
    const arrUnit = ['B', 'K', 'M', 'G', 'T', 'P']
    const baseStep = 1024
    const unitCount = arrUnit.length
    let unitIndex = 0
    while (size >= baseStep && unitIndex < unitCount - 1) {
        unitIndex++
        size /= baseStep
    }
    size = size.toFixed(2)
    return size + '' + arrUnit[unitIndex]
}

// 不全静态文件地址
export function cdn(url, size = null) {
  if (!url) return ''
  if (url.substr(0, 4) === 'http') return url
  if (url.substr(0, 1) === '.') url = url.substr(1)
  // xs,sm,md,lg
  if(size){
    let matchs = /storage([\/a-zA-Z0-9]+)\.(jpg|png|jpeg)/.exec(url)
    if(matchs && matchs.length == 3){
      url = 'thumb' + matchs[1] + '@' + size + '.' + matchs[2]
    }
  }
  return config.CDN_DOMAIN + '/' + url	
}

export function desens(value) {
  if(!value || !value.length) return '-'
    if(value.length <= 6){
      return value
    }
    return value.substr(0, 6) + '...' + value.substr(-4)
}

export function getPrePage() {
	let pages = getCurrentPages()
	let prePage = pages[pages.length-2]
	// #ifdef APP-PLUS
	prePage = prePage.$vm
	// #endif
	return prePage
}