123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { YYYYMMDDHHmmss } from "@/constant/constants";
- import moment from "moment";
- import queryString from "query-string";
- export function dateFormatForAPI(date) {
- return moment(date).format(YYYYMMDDHHmmss);
- }
- export function formatEmptyToNull(obj) {
- Object.keys(obj).forEach((key) => {
- obj[key] = obj[key] === "" || obj[key] === undefined ? null : obj[key];
- });
- return obj;
- }
- // 错误上报:本地打印,百度统计,阿里云日志
- export function errorLog(message, { stack = "", code = "" }) {
- console.error({ message, stack, code });
- window._hmt.push([
- "_trackEvent",
- "message: " + message,
- stack && "stack: " + stack,
- code && "code: " + code,
- ]);
- }
- const CryptoJS = require("crypto-js");
- export function AESString(content) {
- const KEY = "1234567890123456";
- const IV = "1234567890123456";
- // console.log(content);
- var key = CryptoJS.enc.Utf8.parse(KEY);
- var iv = CryptoJS.enc.Utf8.parse(IV);
- var encrypted = CryptoJS.AES.encrypt(content, key, { iv: iv });
- return encrypted.toString();
- }
- export function object2QueryString(obj) {
- return queryString.stringify(obj);
- }
- function toDataURL(url) {
- return fetch(url)
- .then((response) => {
- return response.blob();
- })
- .then((blob) => {
- return URL.createObjectURL(blob);
- });
- }
- // 下载文件
- export async function downloadFileURL(url, name) {
- const link = document.createElement("a");
- link.style.display = "none";
- const fileName = name || url.split("/").pop();
- link.setAttribute("download", fileName);
- // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
- if ([".txt"].some((v) => fileName.endsWith(v))) {
- // const urlObj = new URL(url);
- // link.href = await toDataURL(url.replace(urlObj.origin));
- link.href = await toDataURL(url);
- } else {
- link.href = url;
- }
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
|