// 处理数字转时分秒事件
export const handleDuration = (value, { s = true ,m = true ,h = true } = {s: true, m: true,h: true} ) => {
value = Number(value)
let secondTime = parseInt(value, 0);// 秒
let minuteTime = 0;// 分
let hourTime = 0;// 小时
if(secondTime > 60) {//如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60, 0);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60, 0);
//如果分钟大于60,将分钟转换成小时
if(minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60, 0);
//获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60, 0);
}
}
let result = ""
if (s) {
result = "" + parseInt(secondTime, 0) + "秒"
}
if(minuteTime > 0 && m) {
result = "" + parseInt(minuteTime, 0) + "分钟" + result;
}
if(hourTime > 0 && h) {
result = "" + parseInt(hourTime, 0) + "小时" + result;
}
return result;
}
// 检查类型函数
export const checkType = data => Object.prototype.toString.call(data).slice(8, -1)
// 深复制数据函数
export const deepClone = target => {
let targetType = Object.prototype.toString.call(target).slice(8, -1)
let result
if (targetType === 'Object') {
result = {}
} else if (targetType === 'Array') {
result = []
} else {
return target
}
for (let i in target) {
let value = target[i]
let valueType = Object.prototype.toString.call(value).slice(8, -1)
if (valueType === 'Object' || valueType === 'Array') {
result[i] = deepClone(value)
} else {
result[i] = value
}
}
return result
}
// 时间格式化的函数
export const parseTime = function (time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}/{m}/{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if (('' + time).length === 10) time = parseInt(time, 0) * 1000
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') return ['日','一', '二', '三', '四', '五', '六'][value - 0]
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return timeStr
}
// 获得url 中的参数
export function GetParam (url, id) {
url = url+ "";
let reg = new RegExp("(\?|\&)" + id + "=([^\&]+)", "i");
let result = url.match(reg);
if (result && result[2]) {
return result[2];
} else {return ''}
}
// 将腾讯/高德地图经纬度转换为百度地图经纬度
export const aMapTransBMap = (lng, lat) => {
const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
const x = lng;
const y = lat;
const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
return {
lng: z * Math.cos(theta) + 0.0065,
lat: z * Math.sin(theta) + 0.006,
}
}
// 将百度地图经纬度转换为腾讯/高德地图经纬度
export const bMapTransAMap = (lng, lat) => {
const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
const x = lng - 0.0065;
const y = lat - 0.006;
const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
return {
lng: z * Math.cos(theta),
lat: z * Math.sin(theta),
}
}
// 处理大数字是否格式化处理问题
export const numberFormat = (number, decimals, dec_point, thousands_sep) => {
number = (`${number}`).replace(/[^0-9+-Ee.]/g, '');
const n = !isFinite(+number) ? 0 : +number;
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
const sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
const dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
let s = '';
const toFixedFix = function (n, prec) {
const k = Math.pow(10, prec);
return `${Math.round(n * k) / k}`;
};
s = (prec ? toFixedFix(n, prec) : `${Math.round(n)}`).split('.');
const re = /(-?d+)(d{3})/;
while (re.test(s[0])) {
s[0] = s[0].replace(re, `$1${sep}$2`);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}