rt-sim-training-client/src/utils/index.js

112 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-02 16:29:52 +08:00
/* eslint-disable no-mixed-spaces-and-tabs */
/**
* Created by PanJiaChen on 16/11/18.
*/
/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0) {
2019-07-25 10:30:30 +08:00
return null;
2019-07-02 16:29:52 +08:00
}
2019-07-25 10:30:30 +08:00
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
let date;
2019-07-02 16:29:52 +08:00
if (typeof time === 'object') {
2019-07-25 10:30:30 +08:00
date = time;
2019-07-02 16:29:52 +08:00
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
2019-07-25 10:30:30 +08:00
time = parseInt(time);
2019-07-02 16:29:52 +08:00
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
2019-07-25 10:30:30 +08:00
time = time * 1000;
2019-07-02 16:29:52 +08:00
}
2019-07-25 10:30:30 +08:00
date = new Date(time);
2019-07-02 16:29:52 +08:00
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
2019-07-25 10:30:30 +08:00
};
2019-07-02 16:29:52 +08:00
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
2019-07-25 10:30:30 +08:00
let value = formatObj[key];
2019-07-02 16:29:52 +08:00
// Note: getDay() returns 0 on Sunday
2019-07-25 10:30:30 +08:00
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ]; }
2019-07-02 16:29:52 +08:00
if (result.length > 0 && value < 10) {
2019-07-25 10:30:30 +08:00
value = '0' + value;
2019-07-02 16:29:52 +08:00
}
2019-07-25 10:30:30 +08:00
return value || 0;
});
return time_str;
2019-07-02 16:29:52 +08:00
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function timeFormat(time, option) {
if (('' + time).length === 10) {
2019-07-25 10:30:30 +08:00
time = parseInt(time) * 1000;
2019-07-02 16:29:52 +08:00
} else {
2019-07-25 10:30:30 +08:00
time = +time;
2019-07-02 16:29:52 +08:00
}
2019-07-25 10:30:30 +08:00
const d = new Date(time);
const now = Date.now();
2019-07-02 16:29:52 +08:00
2019-07-25 10:30:30 +08:00
const diff = (now - d) / 1000;
2019-07-02 16:29:52 +08:00
if (diff < 30) {
2019-07-25 10:30:30 +08:00
return '刚刚';
2019-07-02 16:29:52 +08:00
} else if (diff < 3600) {
// less 1 hour
2019-07-25 10:30:30 +08:00
return Math.ceil(diff / 60) + '分钟前';
2019-07-02 16:29:52 +08:00
} else if (diff < 3600 * 24) {
2019-07-25 10:30:30 +08:00
return Math.ceil(diff / 3600) + '小时前';
2019-07-02 16:29:52 +08:00
} else if (diff < 3600 * 24 * 2) {
2019-07-25 10:30:30 +08:00
return '1天前';
2019-07-02 16:29:52 +08:00
}
if (option) {
2019-07-25 10:30:30 +08:00
return parseTime(time, option);
2019-07-02 16:29:52 +08:00
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
2019-07-25 10:30:30 +08:00
);
2019-07-02 16:29:52 +08:00
}
}
/**
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url) {
2019-07-25 10:30:30 +08:00
const search = url.split('?')[1];
2019-07-02 16:29:52 +08:00
if (!search) {
2019-07-25 10:30:30 +08:00
return {};
2019-07-02 16:29:52 +08:00
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
2019-07-25 10:30:30 +08:00
);
2019-07-02 16:29:52 +08:00
}