You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
254 lines
8.1 KiB
254 lines
8.1 KiB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
|
return new (P || (P = Promise))(function (resolve, reject) { |
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
|
step((generator = generator.apply(thisArg, _arguments || [])).next()); |
|
}); |
|
}; |
|
import { jsx as _jsx } from "react/jsx-runtime"; |
|
import { ceil, compact, fill, isArray, isEmpty, isObject, some, truncate, zipObject } from "lodash"; |
|
import moment from "moment"; |
|
import React from "react"; |
|
import WORD_IMG from "./icon/word.svg"; |
|
import PDF_IMG from "./icon/pdf.svg"; |
|
import PPT_IMG from "./icon/ppt.svg"; |
|
import EXCEL_IMG from "./icon/excel.svg"; |
|
import OTHER_IMG from "./icon/other.svg"; |
|
import VIDEO_IMG from "./icon/video.svg"; |
|
import { clearStorage } from "../lib"; |
|
import { Modal } from "antd"; |
|
import axios from "axios"; |
|
import { getToken } from "./store"; |
|
// 退出登录 |
|
export function logoutOut(navigate) { |
|
clearStorage(); |
|
const loca = window.location; |
|
if (process.env.NODE_ENV === "development") { |
|
navigate && navigate("/"); |
|
} |
|
else { |
|
const REACT_APP_PATH_NAME = process.env.REACT_APP_PATH_NAME; |
|
loca.href = REACT_APP_PATH_NAME + "?logoutType=manual"; |
|
} |
|
} |
|
//手机号加密 |
|
export function phoneEncryption(phone) { |
|
const tel = phone.split(""); |
|
return fill(tel, "*", 3, tel.length - 4); |
|
} |
|
export function idEncryption(id) { |
|
if (id) { |
|
return id.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2"); |
|
} |
|
else { |
|
return id; |
|
} |
|
} |
|
//字符串转对象 |
|
export function jsonString(str) { |
|
return __awaiter(this, void 0, void 0, function* () { |
|
try { |
|
JSON.parse(str); |
|
} |
|
catch (e) { |
|
return []; |
|
} |
|
const data = JSON.parse(str); |
|
if (isArray(data) && some(data, item => item.uid)) { |
|
return yield formatImageUrl(data); |
|
} |
|
return JSON.parse(str); |
|
}); |
|
} |
|
// message |
|
export function themeMessage(result, callback, content) { |
|
Modal[result.type]({ |
|
content: content || result.message, |
|
onOk() { |
|
if (result.type === "success") { |
|
callback && callback(); |
|
} |
|
} |
|
}); |
|
} |
|
export function replaceString(str) { |
|
return str ? str.replace(/(\r\n)|(\n)/g, "<br>") : ""; |
|
} |
|
export function dateFormatter(str) { |
|
if (!str) { |
|
return ""; |
|
} |
|
return moment(str).format("YYYY-MM-DD HH:mm"); |
|
} |
|
export function truncateText(text) { |
|
if (!text) { |
|
return "-"; |
|
} |
|
return (_jsx("div", Object.assign({ title: text }, { children: truncate(text, { |
|
length: 24, |
|
separator: "..." |
|
}) }))); |
|
} |
|
export const extname = (url) => { |
|
var _a; |
|
const l = url ? url === null || url === void 0 ? void 0 : url.split("?") : []; |
|
return l[0] ? (_a = l[0]) === null || _a === void 0 ? void 0 : _a.split(".").pop().toLowerCase() : ""; |
|
}; |
|
export function isImageUrl(url) { |
|
const fileExtension = extname(url); |
|
if (/^data:image\//.test(url) || |
|
/(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico)$/i.test(fileExtension)) { |
|
return true; |
|
} |
|
} |
|
export function getPreviewIcon(url) { |
|
const fileExtension = extname(url); |
|
if (isImageUrl(url)) { |
|
return url; |
|
} |
|
switch (fileExtension) { |
|
case "doc": |
|
case "docx": |
|
case "pages": |
|
return WORD_IMG; |
|
case "csv": |
|
case "xls": |
|
case "xlsx": |
|
case "numbers": |
|
return EXCEL_IMG; |
|
case "ppt": |
|
return PPT_IMG; |
|
case "pdf": |
|
return PDF_IMG; |
|
case "mp4": |
|
return VIDEO_IMG; |
|
default: |
|
return OTHER_IMG; |
|
} |
|
} |
|
//图片url请求接口并将文件流转为地址 |
|
function getFileUrl(api, path) { |
|
return axios.get(`${api}${path}`, { |
|
responseType: "blob", |
|
withCredentials: false, |
|
headers: { |
|
token: getToken() |
|
} |
|
}).then((response) => { |
|
return URL.createObjectURL(response.data); |
|
}).catch(e => { |
|
console.log(e); |
|
}); |
|
} |
|
function getImageData(item) { |
|
return __awaiter(this, void 0, void 0, function* () { |
|
const api = process.env.REACT_APP_API_URL; |
|
if (isImageUrl(item.name)) { |
|
const url = yield getFileUrl(api === null || api === void 0 ? void 0 : api.replace("/base", ""), `${item === null || item === void 0 ? void 0 : item.thumbUrl}`); |
|
return { |
|
uid: item.uid, |
|
name: item.name, |
|
status: item.status, |
|
url: item.url, |
|
thumbUrl: url, |
|
init: item |
|
}; |
|
} |
|
else { |
|
return Object.assign(Object.assign({}, item), { thumbUrl: getPreviewIcon(item.name) }); |
|
} |
|
}); |
|
} |
|
//格式化图片地址 |
|
export function formatImageUrl(str) { |
|
if (!str) { |
|
return []; |
|
} |
|
if (Array.isArray(str)) { |
|
return Promise.all(str.map((item) => { |
|
return getImageData(item); |
|
})); |
|
} |
|
if (typeof str === "string") { |
|
try { |
|
JSON.parse(str); |
|
} |
|
catch (e) { |
|
return []; |
|
} |
|
const data = JSON.parse(str); |
|
const api = process.env.REACT_APP_API_URL; |
|
if (Array.isArray(data)) { |
|
return data.map((item) => { |
|
if (isImageUrl(item.name)) { |
|
return getFileUrl(api === null || api === void 0 ? void 0 : api.replace("/base", ""), `${item === null || item === void 0 ? void 0 : item.thumbUrl}`).then((url) => { |
|
return { |
|
uid: item.uid, |
|
name: item.name, |
|
status: item.status, |
|
thumbUrl: url, |
|
url: item.url |
|
}; |
|
}); |
|
} |
|
else { |
|
const url = getPreviewIcon(item.name); |
|
return { |
|
uid: item.uid, |
|
name: item.name, |
|
status: item.status, |
|
thumbUrl: url, |
|
url: item.url |
|
}; |
|
} |
|
}); |
|
} |
|
if (isObject(data)) { |
|
if (isImageUrl(data.name)) { |
|
return getFileUrl(api === null || api === void 0 ? void 0 : api.replace("/base", ""), `${data === null || data === void 0 ? void 0 : data.thumbUrl}`).then((response) => { |
|
return [ |
|
{ |
|
uid: data.uid, |
|
name: data.name, |
|
status: data.status, |
|
thumbUrl: URL.createObjectURL(response.data), |
|
url: data.url |
|
} |
|
]; |
|
}); |
|
} |
|
else { |
|
return [ |
|
{ |
|
uid: data.uid, |
|
name: data.name, |
|
status: data.status, |
|
thumbUrl: getPreviewIcon(data.name), |
|
url: data.url |
|
} |
|
]; |
|
} |
|
} |
|
} |
|
return []; |
|
} |
|
export function transformDate(value, dataLabel) { |
|
const data = compact(value); |
|
const times = isEmpty(data) |
|
? [] |
|
: data.map((item) => moment(item).format("YYYY-MM-DD")); |
|
return zipObject(dataLabel, isEmpty(times) |
|
? [null, null] |
|
: [`${times[0]} 00:00:00`, `${times[1]} 23:59:59`]); |
|
} |
|
export function toFixed(num) { |
|
if (!num || num == null) { |
|
return "0.00"; |
|
} |
|
return ceil(Number(num), 3).toFixed(2); |
|
} |
|
export function numberWithStatistic(x) { |
|
return x === null || x === void 0 ? void 0 : x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); |
|
}
|
|
|