index.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. function isSameOrigin(url: string): boolean {
  2. try {
  3. return new URL(url).origin === window.location.origin;
  4. } catch {
  5. return false;
  6. }
  7. }
  8. /**
  9. * 获取全局的样式
  10. * @returns 样式
  11. */
  12. export const getClassRules = (): string => {
  13. let rules = "";
  14. const { styleSheets } = document;
  15. for (let i = 0; i < styleSheets.length; i++) {
  16. const sheet = styleSheets[i];
  17. // 这里是为了过滤掉不同源 css 脚本,防止报错终止导出
  18. const ownerNode = sheet.ownerNode;
  19. // 处理同源或者内部css
  20. if (
  21. ownerNode instanceof HTMLStyleElement ||
  22. (ownerNode instanceof HTMLLinkElement && isSameOrigin(ownerNode.href))
  23. ) {
  24. try {
  25. for (let j = 0; j < sheet.cssRules.length; j++) {
  26. rules += sheet.cssRules[j].cssText;
  27. }
  28. } catch (error) {
  29. console.log(
  30. "CSS scripts from different sources have been filtered out"
  31. );
  32. }
  33. }
  34. }
  35. return rules;
  36. };
  37. /**
  38. * base64 转 file
  39. * @param base64String
  40. * @param fileName
  41. * @param fileType
  42. * @returns
  43. */
  44. export function base64ToFile(
  45. base64String: string,
  46. fileName: string,
  47. fileType: string
  48. ): File {
  49. // 移除Base64字符串中的前缀(如"data:image/png;base64,")
  50. const base64Data = base64String.split(",")[1];
  51. // 解码Base64字符串
  52. const byteCharacters = atob(base64Data);
  53. // 创建一个Uint8Array来存储二进制数据
  54. const byteArrays = new Uint8Array(byteCharacters.length);
  55. for (let i = 0; i < byteCharacters.length; i++) {
  56. byteArrays[i] = byteCharacters.charCodeAt(i);
  57. }
  58. // 创建Blob对象
  59. const blob = new Blob([byteArrays], { type: fileType });
  60. // 创建File对象
  61. const file = new File([blob], fileName, { type: fileType });
  62. return file;
  63. }
  64. /* uuid */
  65. export function uuid() {
  66. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  67. var r = (Math.random() * 16) | 0,
  68. v = c === "x" ? r : (r & 0x3) | 0x8;
  69. return v.toString(16);
  70. });
  71. }
  72. /**
  73. * 获取日期格式化分组
  74. * @param dateVal 日期
  75. * @returns
  76. */
  77. export function getDateGroupString(dateVal: Date | string): string {
  78. const normalizeDate = (date: Date | string): Date => {
  79. const d = new Date(date);
  80. d.setHours(0, 0, 0, 0);
  81. return d;
  82. };
  83. const now = normalizeDate(new Date()); // 当前日期归一化
  84. const itemDate = normalizeDate(new Date(dateVal));
  85. const diffTime = now.getTime() - itemDate.getTime();
  86. const diffDays = Math.floor(diffTime / (1000 * 3600 * 24));
  87. if (diffDays === 0) {
  88. return "今日";
  89. } else if (diffDays === 1) {
  90. return "昨日";
  91. } else if (diffDays <= 6) {
  92. return "7日内";
  93. } else if (diffDays <= 29) {
  94. return "30日内";
  95. } else {
  96. return "更久~";
  97. }
  98. }