utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // 下载文件
  37. export function downloadFileURL(url, name) {
  38. const link = document.createElement("a");
  39. link.style.display = "none";
  40. // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
  41. link.href = url;
  42. const fileName = name || url.split("/").pop();
  43. link.setAttribute("download", fileName);
  44. document.body.appendChild(link);
  45. link.click();
  46. document.body.removeChild(link);
  47. }
  48. export function objTypeOf(obj) {
  49. const toString = Object.prototype.toString;
  50. const map = {
  51. "[object Boolean]": "boolean",
  52. "[object Number]": "number",
  53. "[object String]": "string",
  54. "[object Function]": "function",
  55. "[object Array]": "array",
  56. "[object Date]": "date",
  57. "[object RegExp]": "regExp",
  58. "[object Undefined]": "undefined",
  59. "[object Null]": "null",
  60. "[object Object]": "object",
  61. };
  62. return map[toString.call(obj)];
  63. }
  64. export function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
  65. if (objTypeOf(date) !== "date") return;
  66. const options = {
  67. "Y+": date.getFullYear(),
  68. "M+": date.getMonth() + 1,
  69. "D+": date.getDate(),
  70. "H+": date.getHours(),
  71. "m+": date.getMinutes(),
  72. "s+": date.getSeconds(),
  73. };
  74. Object.entries(options).map(([key, val]) => {
  75. if (new RegExp("(" + key + ")").test(format)) {
  76. const zeros = key === "Y+" ? "0000" : "00";
  77. const value = (zeros + val).substr(("" + val).length);
  78. format = format.replace(RegExp.$1, value);
  79. }
  80. });
  81. return format;
  82. }
  83. export function deepCopy(obj) {
  84. return JSON.parse(JSON.stringify(obj));
  85. }