common.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // 问候语:根据当前小时返回不同问候语
  2. export function greetings() {
  3. // 当前时间(用于计算问候语)
  4. const currentDate = new Date();
  5. const hours = currentDate.getHours();
  6. if (hours >= 6 && hours < 8) {
  7. return "晨起披衣出草堂,轩窗已自喜微凉🌅!";
  8. } else if (hours >= 8 && hours < 12) {
  9. return `上午好!`;
  10. } else if (hours >= 12 && hours < 14) {
  11. return `中午好!`;
  12. } else if (hours >= 14 && hours < 18) {
  13. return `下午好!`;
  14. } else if (hours >= 18 && hours < 24) {
  15. return `晚上好!`;
  16. } else {
  17. return "偷偷向银河要了一把碎星,只等你闭上眼睛撒入你的梦中,晚安🌛!";
  18. }
  19. }
  20. export function getRangeDate(startDate: string | number | Date, endDate: string | number | Date) {
  21. const targetArr = [];
  22. const start = new Date(startDate);
  23. const end = new Date(endDate);
  24. const startDateInfo = {
  25. year: start.getFullYear(),
  26. month: start.getMonth() + 1,
  27. day: start.getDate(),
  28. };
  29. const endDateInfo = {
  30. year: end.getFullYear(),
  31. month: end.getMonth() + 1,
  32. day: end.getDate(),
  33. };
  34. if (startDateInfo.year === endDateInfo.year) {
  35. //同年
  36. if (startDateInfo.month !== endDateInfo.month) {
  37. //同年,不同月份
  38. //获取开始时间所在月的月底日期
  39. const startMax = new Date(startDateInfo.year, startDateInfo.month, 0).getDate();
  40. const endNum = startMax - startDateInfo.day + endDateInfo.day;
  41. for (let i = startDateInfo.day; i <= startDateInfo.day + endNum; i++) {
  42. if (i > startMax) {
  43. targetArr.push(
  44. `${endDateInfo.year}-${
  45. endDateInfo.month < 10 ? "0" + endDateInfo.month : endDateInfo.month
  46. }-${i - startMax < 10 ? "0" + (i - startMax) : i - startMax}`
  47. );
  48. } else {
  49. targetArr.push(
  50. `${startDateInfo.year}-${
  51. startDateInfo.month < 10 ? "0" + startDateInfo.month : startDateInfo.month
  52. }-${i < 10 ? "0" + i : i}`
  53. );
  54. }
  55. }
  56. } else {
  57. //同年同月
  58. for (let i = startDateInfo.day; i <= endDateInfo.day; i++) {
  59. targetArr.push(
  60. `${startDateInfo.year}-${
  61. startDateInfo.month < 10 ? "0" + startDateInfo.month : startDateInfo.month
  62. }-${i < 10 ? "0" + i : i}`
  63. );
  64. }
  65. }
  66. } else {
  67. //不同年 【既然不同年那肯定也不同月】
  68. const startMax = new Date(startDateInfo.year, startDateInfo.month, 0).getDate();
  69. const endNum = startMax - startDateInfo.day + endDateInfo.day;
  70. for (let i = startDateInfo.day; i <= startDateInfo.day + endNum; i++) {
  71. if (i > startMax) {
  72. targetArr.push(
  73. `${endDateInfo.year}-${
  74. endDateInfo.month < 10 ? "0" + endDateInfo.month : endDateInfo.month
  75. }-${i - startMax < 10 ? "0" + (i - startMax) : i - startMax}`
  76. );
  77. } else {
  78. targetArr.push(
  79. `${startDateInfo.year}-${
  80. startDateInfo.month < 10 ? "0" + startDateInfo.month : startDateInfo.month
  81. }-${i < 10 ? "0" + i : i}`
  82. );
  83. }
  84. }
  85. }
  86. return targetArr;
  87. }
  88. export function listToTree(list: any[]) {
  89. const map: { [key: string | number]: any } = {};
  90. // 创建映射表,保留每个节点的 parent_id 等原始字段
  91. list.forEach((item) => {
  92. map[item.id] = { ...item };
  93. });
  94. const tree: any[] = [];
  95. list.forEach((item) => {
  96. const parentId = item.parent_id;
  97. if (parentId && map[parentId]) {
  98. // 将当前节点加入其父节点的 children 数组中
  99. if (!map[parentId].children) {
  100. map[parentId].children = [];
  101. }
  102. map[parentId].children.push(map[item.id]);
  103. } else if (parentId === null || parentId === undefined) {
  104. // 根节点
  105. tree.push(map[item.id]);
  106. }
  107. });
  108. return tree;
  109. }
  110. // 加载部门选项
  111. export function formatTree(nodes: any[]): any[] {
  112. return nodes.map((node) => {
  113. const formattedNode = {
  114. value: node.id,
  115. label: node.name,
  116. disabled: node.status === false || String(node.status) === "false",
  117. };
  118. if (node.children && node.children.length > 0) {
  119. Object.assign(formattedNode, { children: formatTree(node.children) });
  120. }
  121. return formattedNode;
  122. });
  123. }
  124. export function cloneDeep(obj: any) {
  125. return JSON.parse(JSON.stringify(obj));
  126. }
  127. export function isEmpty(obj: string | null | undefined) {
  128. if (obj === undefined || obj === null || obj === "") {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. // 验证是否为blob格式
  135. export function blobValidate(data: Blob): boolean {
  136. return data.type !== "application/json";
  137. }