utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { YYYYMMDDHHmmss } from "@/constant/constants";
  2. import moment from "moment";
  3. import queryString from "query-string";
  4. export function dateFormatForAPI(date) {
  5. return moment(date).format(YYYYMMDDHHmmss);
  6. }
  7. export function formatEmptyToNull(obj) {
  8. Object.keys(obj).forEach((key) => {
  9. obj[key] = obj[key] === "" || obj[key] === undefined ? null : obj[key];
  10. });
  11. return obj;
  12. }
  13. // 错误上报:本地打印,百度统计,阿里云日志
  14. export function errorLog(message, { stack = "", code = "" }) {
  15. console.error({ message, stack, code });
  16. window._hmt.push([
  17. "_trackEvent",
  18. "message: " + message,
  19. stack && "stack: " + stack,
  20. code && "code: " + code,
  21. ]);
  22. }
  23. const CryptoJS = require("crypto-js");
  24. export function AESString(content) {
  25. const KEY = "1234567890123456";
  26. const IV = "1234567890123456";
  27. // console.log(content);
  28. var key = CryptoJS.enc.Utf8.parse(KEY);
  29. var iv = CryptoJS.enc.Utf8.parse(IV);
  30. var encrypted = CryptoJS.AES.encrypt(content, key, { iv: iv });
  31. return encrypted.toString();
  32. }
  33. export function object2QueryString(obj) {
  34. return queryString.stringify(obj);
  35. }
  36. function toDataURL(url) {
  37. return fetch(url)
  38. .then((response) => {
  39. return response.blob();
  40. })
  41. .then((blob) => {
  42. return URL.createObjectURL(blob);
  43. });
  44. }
  45. // 下载文件
  46. export async function downloadFileURL(url, name) {
  47. const link = document.createElement("a");
  48. link.style.display = "none";
  49. const fileName = name || url.split("/").pop();
  50. link.setAttribute("download", fileName);
  51. // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
  52. if ([".txt"].some((v) => fileName.endsWith(v))) {
  53. // const urlObj = new URL(url);
  54. // link.href = await toDataURL(url.replace(urlObj.origin));
  55. link.href = await toDataURL(url);
  56. } else {
  57. link.href = url;
  58. }
  59. document.body.appendChild(link);
  60. link.click();
  61. document.body.removeChild(link);
  62. }
  63. export function objTypeOf(obj) {
  64. const toString = Object.prototype.toString;
  65. const map = {
  66. "[object Boolean]": "boolean",
  67. "[object Number]": "number",
  68. "[object String]": "string",
  69. "[object Function]": "function",
  70. "[object Array]": "array",
  71. "[object Date]": "date",
  72. "[object RegExp]": "regExp",
  73. "[object Undefined]": "undefined",
  74. "[object Null]": "null",
  75. "[object Object]": "object",
  76. };
  77. return map[toString.call(obj)];
  78. }
  79. export function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
  80. if (objTypeOf(date) !== "date") return;
  81. const options = {
  82. "Y+": date.getFullYear(),
  83. "M+": date.getMonth() + 1,
  84. "D+": date.getDate(),
  85. "H+": date.getHours(),
  86. "m+": date.getMinutes(),
  87. "s+": date.getSeconds(),
  88. };
  89. Object.entries(options).map(([key, val]) => {
  90. if (new RegExp("(" + key + ")").test(format)) {
  91. const zeros = key === "Y+" ? "0000" : "00";
  92. const value = (zeros + val).substr(("" + val).length);
  93. format = format.replace(RegExp.$1, value);
  94. }
  95. });
  96. return format;
  97. }
  98. /**
  99. * 获取时间长度文字
  100. * @param {Number} timeNumber 时间数值,单位:毫秒
  101. */
  102. export function timeNumberToText(timeNumber) {
  103. const DAY_TIME = 24 * 60 * 60 * 1000;
  104. const HOUR_TIME = 60 * 60 * 1000;
  105. const MINUTE_TIME = 60 * 1000;
  106. const SECOND_TIME = 1000;
  107. let [day, hour, minute, second] = [0, 0, 0, 0];
  108. let residueTime = timeNumber;
  109. if (residueTime >= DAY_TIME) {
  110. day = Math.floor(residueTime / DAY_TIME);
  111. residueTime -= day * DAY_TIME;
  112. day += "天";
  113. }
  114. if (residueTime >= HOUR_TIME) {
  115. hour = Math.floor(residueTime / HOUR_TIME);
  116. residueTime -= hour * HOUR_TIME;
  117. hour += "小时";
  118. }
  119. if (residueTime >= MINUTE_TIME) {
  120. minute = Math.floor(residueTime / MINUTE_TIME);
  121. residueTime -= minute * MINUTE_TIME;
  122. minute += "分钟";
  123. }
  124. if (residueTime >= SECOND_TIME) {
  125. second = Math.round(residueTime / SECOND_TIME);
  126. second += "秒";
  127. }
  128. return [day, hour, minute, second].filter((item) => !!item).join("");
  129. }
  130. export function deepCopy(obj) {
  131. return JSON.parse(JSON.stringify(obj));
  132. }