helpers.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { map, zipObject } from 'lodash-es';
  2. import dayjs from 'dayjs';
  3. import weekday from 'dayjs/plugin/weekday';
  4. import 'dayjs/locale/zh-cn';
  5. import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
  6. dayjs.extend(isSameOrBefore);
  7. // config dayjs
  8. dayjs.extend(weekday);
  9. dayjs.locale('zh-cn');
  10. export function formatterDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  11. if (!date) {
  12. return '-';
  13. }
  14. return dayjs(date).format(format);
  15. }
  16. export function formatterDateWithTypeAndTime(type = 'year', num = 1) {
  17. return dayjs().subtract(num, type);
  18. }
  19. // range : date year month quarter week
  20. // type : start end
  21. export function rangeDate(
  22. type = 'start',
  23. range = 'date',
  24. format = 'YYYY-MM-DD HH:mm:ss'
  25. ) {
  26. let result = '';
  27. if (type === 'start') {
  28. result = dayjs()
  29. .startOf(range)
  30. .set('hour', type === 'start' ? 0 : 23)
  31. .set('minute', type === 'start' ? 0 : 59)
  32. .set('second', type === 'start' ? 0 : 59)
  33. .format(format);
  34. } else {
  35. result = dayjs()
  36. .endOf(range)
  37. .set('hour', type === 'start' ? 0 : 23)
  38. .set('minute', type === 'start' ? 0 : 59)
  39. .set('second', type === 'start' ? 0 : 59)
  40. .format(format);
  41. }
  42. return result;
  43. }
  44. export function diffDateMinutes(startDate, endDate) {
  45. const isNormalDiffDate = dayjs(startDate).isSameOrBefore(dayjs(endDate));
  46. if (isNormalDiffDate) {
  47. return dayjs(endDate).diff(startDate, 'minute');
  48. } else {
  49. return '';
  50. }
  51. }
  52. export function subtractDate(num, type = 'year', format = 'YYYY-MM-DD', date) {
  53. // type:day week month quarter year hour minute
  54. return dayjs(date).subtract(num, type).format(format);
  55. }
  56. export function getTypeStartDay(type = 'year', format = 'YYYY-MM-DD') {
  57. return dayjs().startOf(type).add(1, 'day').format(format);
  58. }
  59. export function addDate(startDate, num, type = 'year', format = 'YYYY-MM-DD') {
  60. // type:day week month quarter year hour minute
  61. return dayjs(startDate).add(num, type).format(format);
  62. }
  63. // 消息中心跳转
  64. export function generateQueryString(obj) {
  65. const queryString = Object.entries(obj)
  66. .map(
  67. ([key, value]) =>
  68. `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
  69. )
  70. .join('&');
  71. return queryString;
  72. }
  73. export function normalize(dataList, key = 'id') {
  74. return zipObject(map(dataList, key), dataList);
  75. }
  76. function transformLat(x, y) {
  77. const pi = 3.1415926535897932384626;
  78. 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));
  79. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  80. ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  81. ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  82. return ret;
  83. }
  84. function transformLng(x, y) {
  85. const pi = 3.1415926535897932384626;
  86. let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  87. ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  88. ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  89. ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  90. return ret;
  91. }
  92. // 2. wgs84转gcj02的核心算法(无需调用API,本地计算)
  93. export function wgs84togcj02(lng, lat) {
  94. const pi = 3.1415926535897932384626;
  95. const a = 6378245.0;
  96. const ee = 0.00669342162296594323;
  97. let dLat = transformLat(lng - 105.0, lat - 35.0);
  98. let dLng = transformLng(lng - 105.0, lat - 35.0);
  99. const radLat = lat / 180.0 * pi;
  100. let magic = Math.sin(radLat);
  101. magic = 1 - ee * magic * magic;
  102. const sqrtMagic = Math.sqrt(magic);
  103. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  104. dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  105. const mgLat = lat + dLat;
  106. const mgLng = lng + dLng;
  107. return { lng: mgLng, lat: mgLat };
  108. }
  109. export function convertToGrayscale(url) {
  110. return new Promise((resolve) => {
  111. const img = new Image();
  112. img.crossOrigin = 'Anonymous';
  113. img.onload = () => {
  114. const canvas = document.createElement('canvas');
  115. canvas.width = img.width;
  116. canvas.height = img.height;
  117. const ctx = canvas.getContext('2d');
  118. ctx.drawImage(img, 0, 0);
  119. const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  120. const data = imageData.data;
  121. for (let i = 0; i < data.length; i += 4) {
  122. const gray =
  123. 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
  124. data[i] = data[i + 1] = data[i + 2] = gray;
  125. }
  126. ctx.putImageData(imageData, 0, 0);
  127. resolve(canvas.toDataURL());
  128. };
  129. img.src = url;
  130. });
  131. }
  132. export function numToFixed(val, count = 2) {
  133. if (!Number(val)) {
  134. return 0;
  135. }
  136. return Number(val).toFixed(count);
  137. }