2019-07-02 16:29:52 +08:00
|
|
|
/**
|
2019-07-25 11:07:32 +08:00
|
|
|
* Created by jiachenpan on 16/11/18.
|
2019-07-02 16:29:52 +08:00
|
|
|
*/
|
2019-07-25 11:07:32 +08:00
|
|
|
import md5 from 'js-md5';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 11:07:32 +08:00
|
|
|
// 时间转换格式
|
2019-07-02 16:29:52 +08:00
|
|
|
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 {
|
2019-07-25 11:07:32 +08:00
|
|
|
if (('' + time).length === 10) time = parseInt(time) * 1000;
|
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-25 11:07:32 +08:00
|
|
|
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1];
|
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
|
|
|
}
|
2019-07-25 11:07:32 +08:00
|
|
|
// 判断多久之前
|
|
|
|
export function formatTime(time, option) {
|
|
|
|
time = +time * 1000;
|
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-25 11:07:32 +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 {
|
2019-07-25 11:07:32 +08:00
|
|
|
return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分';
|
2019-07-02 16:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-25 11:07:32 +08:00
|
|
|
* 获取dom对象偏移位置 设置初始位置
|
|
|
|
* @param {*} dom - 需要获取的dom
|
|
|
|
* @param {*} isFixed - 父级是否position: fixed
|
2019-07-02 16:29:52 +08:00
|
|
|
*/
|
2019-07-25 11:07:32 +08:00
|
|
|
export function getDomOffset(dom) {
|
|
|
|
let pol = 0;
|
|
|
|
let pot = 0;
|
|
|
|
let offsetLeft = 0;
|
|
|
|
let offsetTop = 0;
|
|
|
|
while (dom) {
|
|
|
|
if (pol != dom.offsetLeft) {
|
|
|
|
offsetLeft += dom.offsetLeft;
|
|
|
|
pol = dom.offsetLeft;
|
|
|
|
}
|
|
|
|
if (pot != dom.offsetTop) {
|
|
|
|
offsetTop += dom.offsetTop;
|
|
|
|
pot = dom.offsetTop;
|
|
|
|
}
|
|
|
|
dom = dom.offsetParent;
|
|
|
|
}
|
|
|
|
const offset = {
|
|
|
|
x: offsetLeft,
|
|
|
|
y: offsetTop
|
|
|
|
};
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 矩形碰撞检测
|
|
|
|
*/
|
|
|
|
export function checkRectCollision(rect1, rect2) {
|
|
|
|
const center1 = { x: rect1.point.x + rect1.width / 2, y: rect1.point.y + rect1.height / 2 };
|
|
|
|
const center2 = { x: rect2.point.x + rect2.width / 2, y: rect2.point.y + rect2.height / 2 };
|
|
|
|
if (
|
|
|
|
// 横向判断 和 纵向判断
|
|
|
|
Math.abs(center1.x - center2.x) < rect1.width / 2 + rect2.width / 2 &&
|
|
|
|
Math.abs(center1.y - center2.y) < rect1.height / 2 + rect2.height / 2
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSessionId() {
|
|
|
|
return md5([Math.random().toFixed(5), +new Date()].join('_'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 获取倒计时
|
|
|
|
* data 目标时间
|
|
|
|
*/
|
|
|
|
export function getCountTime(data) {
|
|
|
|
const stationDate = parseInt(data);
|
|
|
|
const date = Date.parse(new Date()) / 1000;
|
|
|
|
const time = stationDate - date;
|
|
|
|
if (time > 0) {
|
|
|
|
let h = Math.floor(time / (60 * 60));
|
|
|
|
let m = Math.floor(time / 60) - (h * 60);
|
|
|
|
let s = Math.floor(time) - (h * 60 * 60) - (m * 60);
|
|
|
|
if (h <= 9) h = '0' + h;
|
|
|
|
if (m <= 9) m = '0' + m;
|
|
|
|
if (s <= 9) s = '0' + s;
|
|
|
|
return `${h}:${m}:${s}`;
|
|
|
|
} else {
|
|
|
|
return -1;
|
2019-07-02 16:29:52 +08:00
|
|
|
}
|
|
|
|
}
|