|
|
|
import deepMerge from '../vendors/deep-merge.js';
|
|
|
|
import deepClone from '../vendors/clone.js';
|
|
|
|
/**
|
|
|
|
* 格式化时间
|
|
|
|
* @param {Datetime} source 时间对象
|
|
|
|
* @param {String} format 格式
|
|
|
|
* @return {String} 格式化过后的时间
|
|
|
|
*/
|
|
|
|
|
|
|
|
const formatDate = (source, format) => {
|
|
|
|
const o = {
|
|
|
|
'M+': source.getMonth() + 1,
|
|
|
|
// 月份
|
|
|
|
'd+': source.getDate(),
|
|
|
|
// 日
|
|
|
|
'H+': source.getHours(),
|
|
|
|
// 小时
|
|
|
|
'm+': source.getMinutes(),
|
|
|
|
// 分
|
|
|
|
's+': source.getSeconds(),
|
|
|
|
// 秒
|
|
|
|
'q+': Math.floor((source.getMonth() + 3) / 3),
|
|
|
|
// 季度
|
|
|
|
'f+': source.getMilliseconds() // 毫秒
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (/(y+)/.test(format)) {
|
|
|
|
format = format.replace(RegExp.$1, (source.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let k in o) {
|
|
|
|
if (new RegExp('(' + k + ')').test(format)) {
|
|
|
|
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return format;
|
|
|
|
};
|
|
|
|
|
|
|
|
const smartDate = originDate => {
|
|
|
|
let nowDate = new Date(),
|
|
|
|
isToday = false,
|
|
|
|
isYesterday = false,
|
|
|
|
isPastYear = false,
|
|
|
|
isPreviousYear = false,
|
|
|
|
originYear,
|
|
|
|
originMonth,
|
|
|
|
originDay,
|
|
|
|
diffMinute,
|
|
|
|
formatedDate;
|
|
|
|
originDate = new Date(originDate * 1000);
|
|
|
|
diffMinute = Math.round((nowDate.getTime() - originDate.getTime()) / (1000 * 60));
|
|
|
|
originYear = originDate.getFullYear();
|
|
|
|
originMonth = originDate.getMonth() + 1;
|
|
|
|
originDay = originDate.getDate();
|
|
|
|
|
|
|
|
if (diffMinute <= nowDate.getHours() * 60) {
|
|
|
|
isToday = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nowDate.getDate() - originDay == 1) {
|
|
|
|
isYesterday = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nowDate.getFullYear() - originYear == 1) {
|
|
|
|
isPreviousYear = true;
|
|
|
|
} else if (nowDate.getFullYear() - originYear > 1) {
|
|
|
|
isPastYear = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diffMinute < 30 && isToday) {
|
|
|
|
formatedDate = '刚刚';
|
|
|
|
} else if (diffMinute < 60 && isToday) {
|
|
|
|
formatedDate = `${diffMinute} 分钟前`;
|
|
|
|
} else if (diffMinute < 60 * 24 && isToday) {
|
|
|
|
formatedDate = `${Math.floor(diffMinute / 60)} 小时前`;
|
|
|
|
} else if (diffMinute < 60 * 24 * 2 && isYesterday) {
|
|
|
|
formatedDate = '昨天';
|
|
|
|
} else if (isPreviousYear) {
|
|
|
|
formatedDate = '去年';
|
|
|
|
} else if (isPastYear) {
|
|
|
|
formatedDate = `${originYear} 年`;
|
|
|
|
} else {
|
|
|
|
formatedDate = `${originMonth} 月 ${originDay} 日`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatedDate;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isObject = val => {
|
|
|
|
return val != null && typeof val === 'object' && Array.isArray(val) === false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isEmptyObject = obj => {
|
|
|
|
return Object.keys(obj).length == 0 && obj.constructor == Object;
|
|
|
|
};
|
|
|
|
|
|
|
|
const assign = function (target) {
|
|
|
|
// We must check against these specific cases.
|
|
|
|
if (target === undefined || target === null) {
|
|
|
|
throw new TypeError('Cannot convert undefined or null to object');
|
|
|
|
}
|
|
|
|
|
|
|
|
var output = Object(target);
|
|
|
|
|
|
|
|
for (var index = 1; index < arguments.length; index++) {
|
|
|
|
var source = arguments[index];
|
|
|
|
|
|
|
|
if (source !== undefined && source !== null) {
|
|
|
|
for (var nextKey in source) {
|
|
|
|
if (source.hasOwnProperty(nextKey)) {
|
|
|
|
output[nextKey] = source[nextKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
const merge = (oldObj, newObj, genre = 'override') => {
|
|
|
|
let arrayMerge = (destArr, srcArr) => {
|
|
|
|
switch (genre) {
|
|
|
|
case 'concat':
|
|
|
|
return destArr.concat(srcArr);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return srcArr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return deepMerge(oldObj, newObj, {
|
|
|
|
arrayMerge: arrayMerge
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const clone = originData => deepClone(originData);
|
|
|
|
|
|
|
|
const updateItemInArray = (array, itemId, updateItemCallback) => {
|
|
|
|
const updatedArray = array.map(item => {
|
|
|
|
if (item.id !== itemId) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateItemCallback(item);
|
|
|
|
});
|
|
|
|
return updatedArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
const trim = s => {
|
|
|
|
return s.replace(/(^\s*)|(\s*$)/g, "");
|
|
|
|
};
|
|
|
|
|
|
|
|
const ArrayIndexOf = (arr, val) => {
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i] == val) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ArrayRemove = (arr, val) => {
|
|
|
|
const index = ArrayIndexOf(arr, val);
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
arr.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}; //坐标系之间的转换
|
|
|
|
|
|
|
|
|
|
|
|
const coordinateChange = (lng, lat) => {
|
|
|
|
//定义一些常量
|
|
|
|
const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
|
|
|
|
const PI = 3.1415926535897932384626;
|
|
|
|
const a = 6378245.0;
|
|
|
|
const ee = 0.00669342162296594323;
|
|
|
|
|
|
|
|
const transformlat = (lng, lat) => {
|
|
|
|
const lat1 = +lat;
|
|
|
|
const lng1 = +lng;
|
|
|
|
let ret = -100.0 + 2.0 * lng1 + 3.0 * lat1 + 0.2 * lat1 * lat1 + 0.1 * lng1 * lat1 + 0.2 * Math.sqrt(Math.abs(lng1));
|
|
|
|
ret += (20.0 * Math.sin(6.0 * lng1 * PI) + 20.0 * Math.sin(2.0 * lng1 * PI)) * 2.0 / 3.0;
|
|
|
|
ret += (20.0 * Math.sin(lat1 * PI) + 40.0 * Math.sin(lat1 / 3.0 * PI)) * 2.0 / 3.0;
|
|
|
|
ret += (160.0 * Math.sin(lat1 / 12.0 * PI) + 320 * Math.sin(lat1 * PI / 30.0)) * 2.0 / 3.0;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
const transformlng = (lng, lat) => {
|
|
|
|
const lat1 = +lat;
|
|
|
|
const lng1 = +lng;
|
|
|
|
let ret = 300.0 + lng1 + 2.0 * lat1 + 0.1 * lng1 * lng1 + 0.1 * lng1 * lat1 + 0.1 * Math.sqrt(Math.abs(lng1));
|
|
|
|
ret += (20.0 * Math.sin(6.0 * lng1 * PI) + 20.0 * Math.sin(2.0 * lng1 * PI)) * 2.0 / 3.0;
|
|
|
|
ret += (20.0 * Math.sin(lng1 * PI) + 40.0 * Math.sin(lng1 / 3.0 * PI)) * 2.0 / 3.0;
|
|
|
|
ret += (150.0 * Math.sin(lng1 / 12.0 * PI) + 300.0 * Math.sin(lng1 / 30.0 * PI)) * 2.0 / 3.0;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
const lat2 = +lat;
|
|
|
|
const lng2 = +lng;
|
|
|
|
let dlat = transformlat(lng2 - 105.0, lat2 - 35.0);
|
|
|
|
let dlng = transformlng(lng2 - 105.0, lat2 - 35.0);
|
|
|
|
let radlat = lat2 / 180.0 * PI;
|
|
|
|
let magic = Math.sin(radlat);
|
|
|
|
magic = 1 - ee * magic * magic;
|
|
|
|
const sqrtmagic = Math.sqrt(magic);
|
|
|
|
dlat = dlat * 180.0 / (a * (1 - ee) / (magic * sqrtmagic) * PI);
|
|
|
|
dlng = dlng * 180.0 / (a / sqrtmagic * Math.cos(radlat) * PI);
|
|
|
|
let mglat = lat2 + dlat;
|
|
|
|
let mglng = lng2 + dlng;
|
|
|
|
mglat = +mglat;
|
|
|
|
mglng = +mglng;
|
|
|
|
const z = Math.sqrt(mglng * mglng + mglat * mglat) + 0.00002 * Math.sin(mglat * x_PI);
|
|
|
|
const theta = Math.atan2(mglat, mglng) + 0.000003 * Math.cos(mglng * x_PI);
|
|
|
|
const bd_lng = Number((z * Math.cos(theta) + 0.0065).toString().match(/^\d+(?:\.\d{0,5})?/));
|
|
|
|
const bd_lat = Number((z * Math.sin(theta) + 0.0065).toString().match(/^\d+(?:\.\d{0,5})?/));
|
|
|
|
return [bd_lng, bd_lat];
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
formatDate,
|
|
|
|
// 格式化时间
|
|
|
|
smartDate,
|
|
|
|
// 美化时间
|
|
|
|
isObject,
|
|
|
|
// 是不是对象
|
|
|
|
isEmptyObject,
|
|
|
|
// 是不是空对象
|
|
|
|
assign,
|
|
|
|
// 深复制
|
|
|
|
merge,
|
|
|
|
// 拼接 override 覆盖 concat 连接
|
|
|
|
clone,
|
|
|
|
// 克隆
|
|
|
|
updateItemInArray,
|
|
|
|
// 更新数组里某一项
|
|
|
|
trim,
|
|
|
|
// 去除前后空格
|
|
|
|
ArrayIndexOf,
|
|
|
|
// 数组里查找元素位置
|
|
|
|
ArrayRemove,
|
|
|
|
// 删除数组里的元素
|
|
|
|
coordinateChange //坐标系之间的转换
|
|
|
|
|
|
|
|
}; |
|
|
\ No newline at end of file |
...
|
...
|
|