|
@@ -10,28 +10,28 @@
|
|
|
*/
|
|
|
export function parseTime(time, cFormat) {
|
|
|
if (arguments.length === 0 || !time) {
|
|
|
- return null
|
|
|
+ return null;
|
|
|
}
|
|
|
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
|
- let date
|
|
|
+ const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
|
|
|
+ let date;
|
|
|
if (typeof time === 'object') {
|
|
|
- date = time
|
|
|
+ date = time;
|
|
|
} else {
|
|
|
- if ((typeof time === 'string')) {
|
|
|
- if ((/^[0-9]+$/.test(time))) {
|
|
|
+ if (typeof time === 'string') {
|
|
|
+ if (/^[0-9]+$/.test(time)) {
|
|
|
// support "1548221490638"
|
|
|
- time = parseInt(time)
|
|
|
+ time = parseInt(time);
|
|
|
} else {
|
|
|
// support safari
|
|
|
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
|
|
- time = time.replace(new RegExp(/-/gm), '/')
|
|
|
+ time = time.replace(new RegExp(/-/gm), '/');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|
|
- time = time * 1000
|
|
|
+ if (typeof time === 'number' && time.toString().length === 10) {
|
|
|
+ time = time * 1000;
|
|
|
}
|
|
|
- date = new Date(time)
|
|
|
+ date = new Date(time);
|
|
|
}
|
|
|
const formatObj = {
|
|
|
y: date.getFullYear(),
|
|
@@ -41,14 +41,16 @@ export function parseTime(time, cFormat) {
|
|
|
i: date.getMinutes(),
|
|
|
s: date.getSeconds(),
|
|
|
a: date.getDay()
|
|
|
- }
|
|
|
+ };
|
|
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|
|
- const value = formatObj[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
|
|
|
+ if (key === 'a') {
|
|
|
+ return ['日', '一', '二', '三', '四', '五', '六'][value];
|
|
|
+ }
|
|
|
+ return value.toString().padStart(2, '0');
|
|
|
+ });
|
|
|
+ return time_str;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -58,27 +60,27 @@ export function parseTime(time, cFormat) {
|
|
|
*/
|
|
|
export function formatTime(time, option) {
|
|
|
if (('' + time).length === 10) {
|
|
|
- time = parseInt(time) * 1000
|
|
|
+ time = parseInt(time) * 1000;
|
|
|
} else {
|
|
|
- time = +time
|
|
|
+ time = +time;
|
|
|
}
|
|
|
- const d = new Date(time)
|
|
|
- const now = Date.now()
|
|
|
+ const d = new Date(time);
|
|
|
+ const now = Date.now();
|
|
|
|
|
|
- const diff = (now - d) / 1000
|
|
|
+ const diff = (now - d) / 1000;
|
|
|
|
|
|
if (diff < 30) {
|
|
|
- return '刚刚'
|
|
|
+ return '刚刚';
|
|
|
} else if (diff < 3600) {
|
|
|
// less 1 hour
|
|
|
- return Math.ceil(diff / 60) + '分钟前'
|
|
|
+ return Math.ceil(diff / 60) + '分钟前';
|
|
|
} else if (diff < 3600 * 24) {
|
|
|
- return Math.ceil(diff / 3600) + '小时前'
|
|
|
+ return Math.ceil(diff / 3600) + '小时前';
|
|
|
} else if (diff < 3600 * 24 * 2) {
|
|
|
- return '1天前'
|
|
|
+ return '1天前';
|
|
|
}
|
|
|
if (option) {
|
|
|
- return parseTime(time, option)
|
|
|
+ return parseTime(time, option);
|
|
|
} else {
|
|
|
return (
|
|
|
d.getMonth() +
|
|
@@ -90,7 +92,7 @@ export function formatTime(time, option) {
|
|
|
'时' +
|
|
|
d.getMinutes() +
|
|
|
'分'
|
|
|
- )
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -99,19 +101,32 @@ export function formatTime(time, option) {
|
|
|
* @returns {Object}
|
|
|
*/
|
|
|
export function param2Obj(url) {
|
|
|
- const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
|
|
+ const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ');
|
|
|
if (!search) {
|
|
|
- return {}
|
|
|
+ return {};
|
|
|
}
|
|
|
- const obj = {}
|
|
|
- const searchArr = search.split('&')
|
|
|
+ const obj = {};
|
|
|
+ const searchArr = search.split('&');
|
|
|
searchArr.forEach(v => {
|
|
|
- const index = v.indexOf('=')
|
|
|
+ const index = v.indexOf('=');
|
|
|
if (index !== -1) {
|
|
|
- const name = v.substring(0, index)
|
|
|
- const val = v.substring(index + 1, v.length)
|
|
|
- obj[name] = val
|
|
|
+ const name = v.substring(0, index);
|
|
|
+ const val = v.substring(index + 1, v.length);
|
|
|
+ obj[name] = val;
|
|
|
}
|
|
|
- })
|
|
|
- return obj
|
|
|
+ });
|
|
|
+ return obj;
|
|
|
}
|
|
|
+/**
|
|
|
+ * 若第二数组不设置默认根据下标数设置
|
|
|
+ * @param {[type]} label [显示文案数组]
|
|
|
+ * @param {[type]} value [表示值的数组]
|
|
|
+ * @return {[type]} [description]
|
|
|
+ */
|
|
|
+export const setStatus = (label, value) => {
|
|
|
+ let res = label.map((x, i) => ({
|
|
|
+ label: x,
|
|
|
+ value: value.length > i ? value[i] : i
|
|
|
+ }));
|
|
|
+ return res;
|
|
|
+};
|