huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * Created by jiachenpan on 16/11/18. |
| 3 | */ |
| 4 | |
| 5 | export function parseTime(time, cFormat) { |
| 6 | if (arguments.length === 0) { |
| 7 | return null |
| 8 | } |
| 9 | const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' |
| 10 | let date |
| 11 | if (typeof time === 'object') { |
| 12 | date = time |
| 13 | } else { |
| 14 | if (('' + time).length === 10) time = parseInt(time) * 1000 |
| 15 | date = new Date(time) |
| 16 | } |
| 17 | const formatObj = { |
| 18 | y: date.getFullYear(), |
| 19 | m: date.getMonth() + 1, |
| 20 | d: date.getDate(), |
| 21 | h: date.getHours(), |
| 22 | i: date.getMinutes(), |
| 23 | s: date.getSeconds(), |
| 24 | a: date.getDay() |
| 25 | } |
| 26 | const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { |
| 27 | let value = formatObj[key] |
| 28 | if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1] |
| 29 | if (result.length > 0 && value < 10) { |
| 30 | value = '0' + value |
| 31 | } |
| 32 | return value || 0 |
| 33 | }) |
| 34 | return time_str |
| 35 | } |
| 36 | |
| 37 | export function formatTime(time, option) { |
| 38 | time = +time * 1000 |
| 39 | const d = new Date(time) |
| 40 | const now = Date.now() |
| 41 | |
| 42 | const diff = (now - d) / 1000 |
| 43 | |
| 44 | if (diff < 30) { |
| 45 | return '刚刚' |
| 46 | } else if (diff < 3600) { // less 1 hour |
| 47 | return Math.ceil(diff / 60) + '分钟前' |
| 48 | } else if (diff < 3600 * 24) { |
| 49 | return Math.ceil(diff / 3600) + '小时前' |
| 50 | } else if (diff < 3600 * 24 * 2) { |
| 51 | return '1天前' |
| 52 | } |
| 53 | if (option) { |
| 54 | return parseTime(time, option) |
| 55 | } else { |
| 56 | return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export function param2Obj(url) { |
| 61 | const search = url.split('?')[1] |
| 62 | if (!search) { |
| 63 | return {} |
| 64 | } |
| 65 | return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') |
| 66 | .replace(/mapBean%5B/g, '').replace(/%5D/g, '') + '"}') |
| 67 | } |
| 68 | |
| 69 | export function resetForm(listQuery) { |
| 70 | for (const attr in listQuery) { |
| 71 | if (attr === 'pageIndex' || attr === 'pageSize') { |
| 72 | continue |
| 73 | } |
| 74 | listQuery[attr] = null |
| 75 | } |
| 76 | } |