123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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);
- }
- // 下载文件
- export function downloadFileURL(url, name) {
- const link = document.createElement("a");
- link.style.display = "none";
- // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
- link.href = url;
- const fileName = name || url.split("/").pop();
- link.setAttribute("download", fileName);
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
- export function objTypeOf(obj) {
- const toString = Object.prototype.toString;
- const map = {
- "[object Boolean]": "boolean",
- "[object Number]": "number",
- "[object String]": "string",
- "[object Function]": "function",
- "[object Array]": "array",
- "[object Date]": "date",
- "[object RegExp]": "regExp",
- "[object Undefined]": "undefined",
- "[object Null]": "null",
- "[object Object]": "object",
- };
- return map[toString.call(obj)];
- }
- export function formatDate(format = "YYYY/MM/DD HH:mm:ss", date = new Date()) {
- if (objTypeOf(date) !== "date") return;
- const options = {
- "Y+": date.getFullYear(),
- "M+": date.getMonth() + 1,
- "D+": date.getDate(),
- "H+": date.getHours(),
- "m+": date.getMinutes(),
- "s+": date.getSeconds(),
- };
- Object.entries(options).map(([key, val]) => {
- if (new RegExp("(" + key + ")").test(format)) {
- const zeros = key === "Y+" ? "0000" : "00";
- const value = (zeros + val).substr(("" + val).length);
- format = format.replace(RegExp.$1, value);
- }
- });
- return format;
- }
- export function deepCopy(obj) {
- return JSON.parse(JSON.stringify(obj));
- }
|