Blame view

wenjuan/src/utils/index.js 3.06 KB
e5b57447   杨鑫   '分包问卷'
1
2
3
4
5
6
7
8
9
10
  /**
   * Created by PanJiaChen on 16/11/18.
   */
  
  /**
   * Parse the time to string
   * @param {(Object|string|number)} time
   * @param {string} cFormat
   * @returns {string | null}
   */
d64cd58f   wesley88   上传验收小程序
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  export function parseTime (time, cFormat) {
    if (arguments.length === 0 || !time) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if ((typeof time === 'string')) {
        if ((/^[0-9]+$/.test(time))) {
          // support "1548221490638"
          time = parseInt(time)
        } else {
          // support safari
          // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
          time = time.replace(new RegExp(/-/gm), '/')
        }
e5b57447   杨鑫   '分包问卷'
29
      }
e5b57447   杨鑫   '分包问卷'
30
  
d64cd58f   wesley88   上传验收小程序
31
32
      if ((typeof time === 'number') && (time.toString().length === 10)) {
        time = time * 1000
e5b57447   杨鑫   '分包问卷'
33
      }
d64cd58f   wesley88   上传验收小程序
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      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
e5b57447   杨鑫   '分包问卷'
52
53
54
55
56
57
58
  }
  
  /**
   * @param {number} time
   * @param {string} option
   * @returns {string}
   */
d64cd58f   wesley88   上传验收小程序
59
60
61
62
63
64
65
66
  export function formatTime (time, option) {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    } else {
      time = +time
    }
    const d = new Date(time)
    const now = Date.now()
e5b57447   杨鑫   '分包问卷'
67
  
d64cd58f   wesley88   上传验收小程序
68
    const diff = (now - d) / 1000
e5b57447   杨鑫   '分包问卷'
69
  
d64cd58f   wesley88   上传验收小程序
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    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() +
e5b57447   杨鑫   '分包问卷'
85
86
87
88
89
90
91
92
              1 +
              '月' +
              d.getDate() +
              '日' +
              d.getHours() +
              '时' +
              d.getMinutes() +
              '分'
d64cd58f   wesley88   上传验收小程序
93
94
      )
    }
e5b57447   杨鑫   '分包问卷'
95
96
97
98
99
100
  }
  
  /**
   * @param {string} url
   * @returns {Object}
   */
d64cd58f   wesley88   上传验收小程序
101
102
103
104
105
106
107
108
109
110
111
112
113
  export function param2Obj (url) {
    const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
    if (!search) {
      return {}
    }
    const obj = {}
    const searchArr = search.split('&')
    searchArr.forEach(v => {
      const index = v.indexOf('=')
      if (index !== -1) {
        const name = v.substring(0, index)
        const val = v.substring(index + 1, v.length)
        obj[name] = val
e5b57447   杨鑫   '分包问卷'
114
      }
d64cd58f   wesley88   上传验收小程序
115
116
    })
    return obj
e5b57447   杨鑫   '分包问卷'
117
118
119
  }
  
  // 中间部分
d64cd58f   wesley88   上传验收小程序
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  export function hidden (str, frontLen, endLen) {
    // let endLenData = 0
    // if (str.length !== 2) {
    //   endLenData = endLen
    // }
    // const len = str.length - frontLen - endLenData;
    // let xing = '';
    // for (let i = 0; i < len; i++) {
    //   xing += '*';
    // }
    // return (
    //   str.substring(0, frontLen) + xing + str.substring(str.length - endLenData)
    // );
    return str
  }