import { map, zipObject } from 'lodash-es'; import dayjs from 'dayjs'; import weekday from 'dayjs/plugin/weekday'; import 'dayjs/locale/zh-cn'; import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'; dayjs.extend(isSameOrBefore); // config dayjs dayjs.extend(weekday); dayjs.locale('zh-cn'); export function formatterDate(date, format = 'YYYY-MM-DD HH:mm:ss') { if (!date) { return '-'; } return dayjs(date).format(format); } export function formatterDateWithTypeAndTime(type = 'year', num = 1) { return dayjs().subtract(num, type); } // range : date year month quarter week // type : start end export function rangeDate( type = 'start', range = 'date', format = 'YYYY-MM-DD HH:mm:ss' ) { let result = ''; if (type === 'start') { result = dayjs() .startOf(range) .set('hour', type === 'start' ? 0 : 23) .set('minute', type === 'start' ? 0 : 59) .set('second', type === 'start' ? 0 : 59) .format(format); } else { result = dayjs() .endOf(range) .set('hour', type === 'start' ? 0 : 23) .set('minute', type === 'start' ? 0 : 59) .set('second', type === 'start' ? 0 : 59) .format(format); } return result; } export function diffDateMinutes(startDate, endDate) { const isNormalDiffDate = dayjs(startDate).isSameOrBefore(dayjs(endDate)); if (isNormalDiffDate) { return dayjs(endDate).diff(startDate, 'minute'); } else { return ''; } } export function subtractDate(num, type = 'year', format = 'YYYY-MM-DD', date) { // type:day week month quarter year hour minute return dayjs(date).subtract(num, type).format(format); } export function getTypeStartDay(type = 'year', format = 'YYYY-MM-DD') { return dayjs().startOf(type).add(1, 'day').format(format); } export function addDate(startDate, num, type = 'year', format = 'YYYY-MM-DD') { // type:day week month quarter year hour minute return dayjs(startDate).add(num, type).format(format); } // 消息中心跳转 export function generateQueryString(obj) { const queryString = Object.entries(obj) .map( ([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}` ) .join('&'); return queryString; } export function normalize(dataList, key = 'id') { return zipObject(map(dataList, key), dataList); } function transformLat(x, y) { const pi = 3.1415926535897932384626; let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0; return ret; } function transformLng(x, y) { const pi = 3.1415926535897932384626; let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0; return ret; } // 2. wgs84转gcj02的核心算法(无需调用API,本地计算) export function wgs84togcj02(lng, lat) { const pi = 3.1415926535897932384626; const a = 6378245.0; const ee = 0.00669342162296594323; let dLat = transformLat(lng - 105.0, lat - 35.0); let dLng = transformLng(lng - 105.0, lat - 35.0); const radLat = lat / 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); const mgLat = lat + dLat; const mgLng = lng + dLng; return { lng: mgLng, lat: mgLat }; } export function convertToGrayscale(url) { return new Promise((resolve) => { const img = new Image(); img.crossOrigin = 'Anonymous'; img.onload = () => { const canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); const data = imageData.data; for (let i = 0; i < data.length; i += 4) { const gray = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]; data[i] = data[i + 1] = data[i + 2] = gray; } ctx.putImageData(imageData, 0, 0); resolve(canvas.toDataURL()); }; img.src = url; }); } export function numToFixed(val, count = 2) { if (!Number(val)) { return 0; } return Number(val).toFixed(count); }