第一次提交
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { ElMessage } from "element-plus";
|
||||
|
||||
/**
|
||||
* @description hex颜色转rgb颜色
|
||||
* @param {String} str 颜色值字符串
|
||||
* @returns {String} 返回处理后的颜色值
|
||||
*/
|
||||
export function hexToRgb(str: any) {
|
||||
let hexs: any = "";
|
||||
let reg = /^\#?[0-9A-Fa-f]{6}$/;
|
||||
if (!reg.test(str)) return ElMessage.warning("输入错误的hex");
|
||||
str = str.replace("#", "");
|
||||
hexs = str.match(/../g);
|
||||
for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16);
|
||||
return hexs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description rgb颜色转Hex颜色
|
||||
* @param {*} r 代表红色
|
||||
* @param {*} g 代表绿色
|
||||
* @param {*} b 代表蓝色
|
||||
* @returns {String} 返回处理后的颜色值
|
||||
*/
|
||||
export function rgbToHex(r: any, g: any, b: any) {
|
||||
let reg = /^\d{1,3}$/;
|
||||
if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ElMessage.warning("输入错误的rgb颜色值");
|
||||
let hexs = [r.toString(16), g.toString(16), b.toString(16)];
|
||||
for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`;
|
||||
return `#${hexs.join("")}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 加深颜色值
|
||||
* @param {String} color 颜色值字符串
|
||||
* @param {Number} level 加深的程度,限0-1之间
|
||||
* @returns {String} 返回处理后的颜色值
|
||||
*/
|
||||
export function getDarkColor(color: string, level: number) {
|
||||
let reg = /^\#?[0-9A-Fa-f]{6}$/;
|
||||
if (!reg.test(color)) return ElMessage.warning("输入错误的hex颜色值");
|
||||
let rgb = hexToRgb(color);
|
||||
for (let i = 0; i < 3; i++) rgb[i] = Math.round(20.5 * level + rgb[i] * (1 - level));
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 变浅颜色值
|
||||
* @param {String} color 颜色值字符串
|
||||
* @param {Number} level 加深的程度,限0-1之间
|
||||
* @returns {String} 返回处理后的颜色值
|
||||
*/
|
||||
export function getLightColor(color: string, level: number) {
|
||||
let reg = /^\#?[0-9A-Fa-f]{6}$/;
|
||||
if (!reg.test(color)) return ElMessage.warning("输入错误的hex颜色值");
|
||||
let rgb = hexToRgb(color);
|
||||
for (let i = 0; i < 3; i++) rgb[i] = Math.round(255 * level + rgb[i] * (1 - level));
|
||||
return rgbToHex(rgb[0], rgb[1], rgb[2]);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**一键复制 */
|
||||
// import { Message } from "element-ui";
|
||||
import {showMsg} from "@/utils/showMsg";
|
||||
function fallbackCopyToClipboard(text) {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
|
||||
try {
|
||||
document.execCommand("copy");
|
||||
// alert('复制成功(降级方案)!');
|
||||
showMsg("已复制到剪切板", "success");
|
||||
} catch (err) {
|
||||
console.error("降级复制失败:", err);
|
||||
// alert('复制失败,请手动复制!');
|
||||
showMsg("复制失败,请手动复制!", "error");
|
||||
}
|
||||
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
export default fallbackCopyToClipboard;
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @description 时间相关工具函数
|
||||
*/
|
||||
|
||||
/**
|
||||
* 格式化日期时间为指定格式
|
||||
* @param date 日期对象或字符串
|
||||
* @param format 格式字符串,默认为 'YYYY-MM-DD HH:mm:ss'
|
||||
* @returns 格式化后的日期时间字符串
|
||||
*/
|
||||
export function formatDateTime(date: Date | string | number, format: string = 'YYYY-MM-DD HH:mm:ss'): string {
|
||||
if (!date) return '';
|
||||
|
||||
const d = new Date(date);
|
||||
|
||||
// 检查日期是否有效
|
||||
if (isNaN(d.getTime())) return '';
|
||||
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(d.getDate()).padStart(2, '0');
|
||||
const hours = String(d.getHours()).padStart(2, '0');
|
||||
const minutes = String(d.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(d.getSeconds()).padStart(2, '0');
|
||||
|
||||
return format
|
||||
.replace('YYYY', String(year))
|
||||
.replace('MM', month)
|
||||
.replace('DD', day)
|
||||
.replace('HH', hours)
|
||||
.replace('mm', minutes)
|
||||
.replace('ss', seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化 sendTime 字段,确保为 'YYYY-MM-DD HH:mm:ss' 格式
|
||||
* @param sendTime sendTime 字段值
|
||||
* @returns 格式化后的 sendTime 字符串
|
||||
*/
|
||||
export function formatSendTime(sendTime: string | Date | number): string {
|
||||
if (!sendTime) return '';
|
||||
|
||||
// 如果已经是字符串格式,验证是否符合预期格式
|
||||
if (typeof sendTime === 'string') {
|
||||
// 检查是否已经是 'YYYY-MM-DD HH:mm:ss' 格式
|
||||
const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
||||
if (dateTimeRegex.test(sendTime)) {
|
||||
return sendTime;
|
||||
}
|
||||
|
||||
// 如果是其他格式的字符串,尝试解析
|
||||
const parsed = new Date(sendTime);
|
||||
if (!isNaN(parsed.getTime())) {
|
||||
return formatDateTime(parsed);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
// 如果是 Date 对象或时间戳
|
||||
return formatDateTime(sendTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 sendTime 格式是否正确
|
||||
* @param sendTime sendTime 字段值
|
||||
* @returns 是否为有效格式
|
||||
*/
|
||||
export function isValidSendTime(sendTime: string): boolean {
|
||||
if (!sendTime) return false;
|
||||
|
||||
const dateTimeRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
||||
if (!dateTimeRegex.test(sendTime)) return false;
|
||||
|
||||
// 进一步验证日期时间是否有效
|
||||
const date = new Date(sendTime);
|
||||
return !isNaN(date.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间的 sendTime 格式
|
||||
* @returns 当前时间的格式化字符串
|
||||
*/
|
||||
export function getCurrentSendTime(): string {
|
||||
return formatDateTime(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将日期时间转换为 sendTime 格式(用于表单提交前的处理)
|
||||
* @param dateTime 日期时间值
|
||||
* @returns sendTime 格式字符串
|
||||
*/
|
||||
export function toSendTimeFormat(dateTime: any): string {
|
||||
return formatSendTime(dateTime);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// ? 系统全局字典
|
||||
|
||||
/**
|
||||
* @description:用户性别
|
||||
*/
|
||||
export const genderType = [
|
||||
{ label: "男", value: 1 },
|
||||
{ label: "女", value: 2 }
|
||||
];
|
||||
|
||||
/**
|
||||
* @description:用户状态
|
||||
*/
|
||||
export const userStatus = [
|
||||
{ label: "启用", value: 1, tagType: "success" },
|
||||
{ label: "禁用", value: 0, tagType: "danger" }
|
||||
];
|
||||
@@ -0,0 +1,27 @@
|
||||
import { ElNotification } from "element-plus";
|
||||
|
||||
/**
|
||||
* @description 全局代码错误捕捉
|
||||
* */
|
||||
const errorHandler = (error: any) => {
|
||||
// 过滤 HTTP 请求错误
|
||||
if (error.status || error.status == 0) return false;
|
||||
let errorMap: { [key: string]: string } = {
|
||||
InternalError: "Javascript引擎内部错误",
|
||||
ReferenceError: "未找到对象",
|
||||
TypeError: "使用了错误的类型或对象",
|
||||
RangeError: "使用内置对象时,参数超范围",
|
||||
SyntaxError: "语法错误",
|
||||
EvalError: "错误的使用了Eval",
|
||||
URIError: "URI错误"
|
||||
};
|
||||
let errorName = errorMap[error.name] || `错误码:${error.code}` || "未知错误";
|
||||
ElNotification({
|
||||
title: errorName,
|
||||
message: error.message ? error.message : error,
|
||||
type: "error",
|
||||
duration: 3000
|
||||
});
|
||||
};
|
||||
|
||||
export default errorHandler;
|
||||
+470
@@ -0,0 +1,470 @@
|
||||
import { isArray } from "@/utils/is";
|
||||
import * as dateUtils from "@/utils/date";
|
||||
|
||||
const mode = import.meta.env.VITE_ROUTER_MODE;
|
||||
|
||||
/**
|
||||
* @description 获取localStorage
|
||||
* @param {String} key Storage名称
|
||||
* @returns {String}
|
||||
*/
|
||||
export function localGet(key: string) {
|
||||
const value = window.localStorage.getItem(key);
|
||||
try {
|
||||
return JSON.parse(window.localStorage.getItem(key) as string);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 存储localStorage
|
||||
* @param {String} key Storage名称
|
||||
* @param {*} value Storage值
|
||||
* @returns {void}
|
||||
*/
|
||||
export function localSet(key: string, value: any) {
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 清除localStorage
|
||||
* @param {String} key Storage名称
|
||||
* @returns {void}
|
||||
*/
|
||||
export function localRemove(key: string) {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 清除所有localStorage
|
||||
* @returns {void}
|
||||
*/
|
||||
export function localClear() {
|
||||
window.localStorage.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断数据类型
|
||||
* @param {*} val 需要判断类型的数据
|
||||
* @returns {String}
|
||||
*/
|
||||
export function isType(val: any) {
|
||||
if (val === null) return "null";
|
||||
if (typeof val !== "object") return typeof val;
|
||||
else return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 生成唯一 uuid
|
||||
* @returns {String}
|
||||
*/
|
||||
export function generateUUID() {
|
||||
let uuid = "";
|
||||
for (let i = 0; i < 32; i++) {
|
||||
let random = (Math.random() * 16) | 0;
|
||||
if (i === 8 || i === 12 || i === 16 || i === 20) uuid += "-";
|
||||
uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16);
|
||||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断两个对象是否相同
|
||||
* @param {Object} a 要比较的对象一
|
||||
* @param {Object} b 要比较的对象二
|
||||
* @returns {Boolean} 相同返回 true,反之 false
|
||||
*/
|
||||
export function isObjectValueEqual(a: { [key: string]: any }, b: { [key: string]: any }) {
|
||||
if (!a || !b) return false;
|
||||
let aProps = Object.getOwnPropertyNames(a);
|
||||
let bProps = Object.getOwnPropertyNames(b);
|
||||
if (aProps.length != bProps.length) return false;
|
||||
for (let i = 0; i < aProps.length; i++) {
|
||||
let propName = aProps[i];
|
||||
let propA = a[propName];
|
||||
let propB = b[propName];
|
||||
if (!b.hasOwnProperty(propName)) return false;
|
||||
if (propA instanceof Object) {
|
||||
if (!isObjectValueEqual(propA, propB)) return false;
|
||||
} else if (propA !== propB) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 生成随机数
|
||||
* @param {Number} min 最小值
|
||||
* @param {Number} max 最大值
|
||||
* @returns {Number}
|
||||
*/
|
||||
export function randomNum(min: number, max: number): number {
|
||||
let num = Math.floor(Math.random() * (min - max) + max);
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前时间对应的提示语
|
||||
* @returns {String}
|
||||
*/
|
||||
export function getTimeState() {
|
||||
let timeNow = new Date();
|
||||
let hours = timeNow.getHours();
|
||||
if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
|
||||
if (hours >= 10 && hours <= 14) return `中午好 🌞`;
|
||||
if (hours >= 14 && hours <= 18) return `下午好 🌞`;
|
||||
if (hours >= 18 && hours <= 24) return `晚上好 🌛`;
|
||||
if (hours >= 0 && hours <= 6) return `凌晨好 🌛`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取浏览器默认语言
|
||||
* @returns {String}
|
||||
*/
|
||||
export function getBrowserLang() {
|
||||
let browserLang = navigator.language ? navigator.language : navigator.browserLanguage;
|
||||
let defaultBrowserLang = "";
|
||||
if (["cn", "zh", "zh-cn"].includes(browserLang.toLowerCase())) {
|
||||
defaultBrowserLang = "zh";
|
||||
} else {
|
||||
defaultBrowserLang = "en";
|
||||
}
|
||||
return defaultBrowserLang;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取不同路由模式所对应的 url + params
|
||||
* @returns {String}
|
||||
*/
|
||||
export function getUrlWithParams() {
|
||||
const url = {
|
||||
hash: location.hash.substring(1),
|
||||
history: location.pathname + location.search
|
||||
};
|
||||
return url[mode];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 使用递归扁平化菜单,方便添加动态路由
|
||||
* @param {Array} menuList 菜单列表
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function getFlatMenuList(menuList: Menu.MenuOptions[]): Menu.MenuOptions[] {
|
||||
let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
|
||||
return newMenuList.flatMap(item => [item, ...(item.children ? getFlatMenuList(item.children) : [])]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 使用递归过滤出需要渲染在左侧菜单的列表 (需剔除 isHide == true 的菜单)
|
||||
* @param {Array} menuList 菜单列表
|
||||
* @returns {Array}
|
||||
* */
|
||||
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
|
||||
let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
|
||||
return newMenuList.filter(item => {
|
||||
item.children?.length && (item.children = getShowMenuList(item.children));
|
||||
return !item.meta?.isHide;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 使用递归找出所有面包屑存储到 pinia/vuex 中
|
||||
* @param {Array} menuList 菜单列表
|
||||
* @param {Array} parent 父级菜单
|
||||
* @param {Object} result 处理后的结果
|
||||
* @returns {Object}
|
||||
*/
|
||||
export const getAllBreadcrumbList = (menuList: Menu.MenuOptions[], parent = [], result: { [key: string]: any } = {}) => {
|
||||
for (const item of menuList) {
|
||||
result[item.path] = [...parent, item];
|
||||
if (item.children) getAllBreadcrumbList(item.children, result[item.path], result);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 使用递归处理路由菜单 path,生成一维数组 (第一版本地路由鉴权会用到,该函数暂未使用)
|
||||
* @param {Array} menuList 所有菜单列表
|
||||
* @param {Array} menuPathArr 菜单地址的一维数组 ['**','**']
|
||||
* @returns {Array}
|
||||
*/
|
||||
export function getMenuListPath(menuList: Menu.MenuOptions[], menuPathArr: string[] = []): string[] {
|
||||
for (const item of menuList) {
|
||||
if (typeof item === "object" && item.path) menuPathArr.push(item.path);
|
||||
if (item.children?.length) getMenuListPath(item.children, menuPathArr);
|
||||
}
|
||||
return menuPathArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 递归查询当前 path 所对应的菜单对象 (该函数暂未使用)
|
||||
* @param {Array} menuList 菜单列表
|
||||
* @param {String} path 当前访问地址
|
||||
* @returns {Object | null}
|
||||
*/
|
||||
export function findMenuByPath(menuList: Menu.MenuOptions[], path: string): Menu.MenuOptions | null {
|
||||
for (const item of menuList) {
|
||||
if (item.path === path) return item;
|
||||
if (item.children) {
|
||||
const res = findMenuByPath(item.children, path);
|
||||
if (res) return res;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 使用递归过滤需要缓存的菜单 name (该函数暂未使用)
|
||||
* @param {Array} menuList 所有菜单列表
|
||||
* @param {Array} keepAliveNameArr 缓存的菜单 name ['**','**']
|
||||
* @returns {Array}
|
||||
* */
|
||||
export function getKeepAliveRouterName(menuList: Menu.MenuOptions[], keepAliveNameArr: string[] = []) {
|
||||
menuList.forEach(item => {
|
||||
item.meta.isKeepAlive && item.name && keepAliveNameArr.push(item.name);
|
||||
item.children?.length && getKeepAliveRouterName(item.children, keepAliveNameArr);
|
||||
});
|
||||
return keepAliveNameArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 格式化表格单元格默认值 (el-table-column)
|
||||
* @param {Number} row 行
|
||||
* @param {Number} col 列
|
||||
* @param {*} callValue 当前单元格值
|
||||
* @returns {String}
|
||||
* */
|
||||
export function formatTableColumn(row: number, col: number, callValue: any) {
|
||||
// 如果当前值为数组,使用 / 拼接(根据需求自定义)
|
||||
if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
|
||||
return callValue ?? "--";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 处理 ProTable 值为数组 || 无数据
|
||||
* @param {*} callValue 需要处理的值
|
||||
* @returns {String}
|
||||
* */
|
||||
export function formatValue(callValue: any) {
|
||||
// 如果当前值为数组,使用 / 拼接(根据需求自定义)
|
||||
if (isArray(callValue)) return callValue.length ? callValue.join(" / ") : "--";
|
||||
return callValue ?? "--";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 处理 prop 为多级嵌套的情况,返回的数据 (列如: prop: user.name)
|
||||
* @param {Object} row 当前行数据
|
||||
* @param {String} prop 当前 prop
|
||||
* @returns {*}
|
||||
* */
|
||||
export function handleRowAccordingToProp(row: { [key: string]: any }, prop: string) {
|
||||
if (!prop.includes(".")) return row[prop] ?? "--";
|
||||
prop.split(".").forEach(item => (row = row[item] ?? "--"));
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 处理 prop,当 prop 为多级嵌套时 ==> 返回最后一级 prop
|
||||
* @param {String} prop 当前 prop
|
||||
* @returns {String}
|
||||
* */
|
||||
export function handleProp(prop: string) {
|
||||
const propArr = prop.split(".");
|
||||
if (propArr.length == 1) return prop;
|
||||
return propArr[propArr.length - 1];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description 递归查找 callValue 对应的 enum 值
|
||||
* */
|
||||
export function findItemNested(enumData: any, callValue: any, value: string, children: string) {
|
||||
return enumData.reduce((accumulator: any, current: any) => {
|
||||
if (accumulator) return accumulator;
|
||||
if (current[value] === callValue) return current;
|
||||
if (current[children]) return findItemNested(current[children], callValue, value, children);
|
||||
}, null);
|
||||
}
|
||||
|
||||
export function checkType(data: unknown): string {
|
||||
const type = Object.prototype.toString.call(data);
|
||||
return type.slice(8, -1).toLowerCase();
|
||||
}
|
||||
|
||||
export function isEmpty(data: unknown): boolean {
|
||||
const dataType = checkType(data);
|
||||
switch (dataType) {
|
||||
case "array":
|
||||
return !(data as Array<unknown>).length;
|
||||
case "object":
|
||||
return !Object.keys(data as object).length;
|
||||
case "map":
|
||||
case "set":
|
||||
return !(data as Map<unknown, unknown> | Set<unknown>).size;
|
||||
case "boolean":
|
||||
case "number":
|
||||
case "symbol":
|
||||
case "function":
|
||||
return false;
|
||||
default:
|
||||
return !data;
|
||||
}
|
||||
}
|
||||
|
||||
import type { FilesType } from "../components/FilesCard/types.d.ts";
|
||||
|
||||
/* 公共 相关 开始 */
|
||||
/* 公共 相关 结束 */
|
||||
|
||||
/* FileCard 组件相关 开始 */
|
||||
// 更据文件后缀名获取文件类型
|
||||
export function getFileType(fileExtension: string): {
|
||||
lowerCase: FilesType;
|
||||
upperCase: string;
|
||||
} {
|
||||
// 去除后缀名开头的点,并转换为小写
|
||||
const cleanExtension = fileExtension.replace(".", "").toLowerCase();
|
||||
if (!cleanExtension) {
|
||||
return { lowerCase: "unknown", upperCase: "Unknown" };
|
||||
}
|
||||
const imageExtensions = ["png", "jpg", "jpeg", "gif", "bmp", "svg", "webp"];
|
||||
const wordExtensions = ["doc", "docx"];
|
||||
const excelExtensions = ["xls", "xlsx"];
|
||||
const pptExtensions = ["ppt", "pptx"];
|
||||
const audioExtensions = ["mp3", "wav", "ogg", "flac"];
|
||||
const videoExtensions = ["mp4", "avi", "mov", "mkv"];
|
||||
const codeExtensions = ["js", "ts", "html", "css", "py", "java", "c", "cpp", "json", "php"];
|
||||
const databaseExtensions = ["sql", "db", "sqlite"];
|
||||
const zipExtensions = ["zip", "rar", "7z"];
|
||||
const markExtensions = ["md", "mdx"];
|
||||
|
||||
if (imageExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "image", upperCase: "Image" };
|
||||
}
|
||||
if (wordExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "word", upperCase: "Word" };
|
||||
}
|
||||
if (excelExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "excel", upperCase: "Excel" };
|
||||
}
|
||||
if (pptExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "ppt", upperCase: "Ppt" };
|
||||
}
|
||||
if (cleanExtension === "pdf") {
|
||||
return { lowerCase: "pdf", upperCase: "Pdf" };
|
||||
}
|
||||
if (cleanExtension === "txt") {
|
||||
return { lowerCase: "txt", upperCase: "Txt" };
|
||||
}
|
||||
if (markExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "mark", upperCase: "Markdown" };
|
||||
}
|
||||
if (audioExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "audio", upperCase: "Audio" };
|
||||
}
|
||||
if (videoExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "video", upperCase: "Video" };
|
||||
}
|
||||
if (codeExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "code", upperCase: "Code" };
|
||||
}
|
||||
if (databaseExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "database", upperCase: "Database" };
|
||||
}
|
||||
if (cleanExtension === "lnk") {
|
||||
return { lowerCase: "link", upperCase: "Link" };
|
||||
}
|
||||
if (zipExtensions.includes(cleanExtension)) {
|
||||
return { lowerCase: "zip", upperCase: "Zip" };
|
||||
}
|
||||
if (cleanExtension === "obj" || cleanExtension === "fbx" || cleanExtension === "glb") {
|
||||
return { lowerCase: "three", upperCase: "3D" };
|
||||
}
|
||||
return { lowerCase: "file", upperCase: "File" };
|
||||
}
|
||||
|
||||
// 获取文件大小
|
||||
export function getSize(size: number) {
|
||||
let retSize = size;
|
||||
const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
|
||||
let unitIndex = 0;
|
||||
|
||||
while (retSize >= 1024 && unitIndex < units.length - 1) {
|
||||
retSize /= 1024;
|
||||
unitIndex++;
|
||||
}
|
||||
|
||||
return `${retSize.toFixed(0)} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
// 通过文件流,生成图片预览
|
||||
// Follow code is copy from `antd/components/upload/utils.ts`:
|
||||
export function isImageFileType(type: string): boolean {
|
||||
return type.indexOf("image/") === 0;
|
||||
}
|
||||
const MEASURE_SIZE = 200;
|
||||
export function previewImage(file: File | Blob): Promise<string> {
|
||||
return new Promise<string>(resolve => {
|
||||
if (!file || !file.type || !isImageFileType(file.type)) {
|
||||
resolve("");
|
||||
return;
|
||||
}
|
||||
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
const { width, height } = img;
|
||||
|
||||
const ratio = width / height;
|
||||
const MEASURE_SIZE_WIDTH = ratio > 1 ? MEASURE_SIZE : MEASURE_SIZE * ratio;
|
||||
const MEASURE_SIZE_HEIGHT = ratio > 1 ? MEASURE_SIZE / ratio : MEASURE_SIZE;
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = MEASURE_SIZE_WIDTH;
|
||||
canvas.height = MEASURE_SIZE_HEIGHT;
|
||||
canvas.style.cssText = `position: fixed; left: 0; top: 0; width: ${MEASURE_SIZE_WIDTH}px; height: ${MEASURE_SIZE_HEIGHT}px; z-index: 9999; display: none;`;
|
||||
document.body.appendChild<HTMLCanvasElement>(canvas);
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
ctx!.drawImage(img, 0, 0, MEASURE_SIZE_WIDTH, MEASURE_SIZE_HEIGHT);
|
||||
const dataURL = canvas.toDataURL();
|
||||
document.body.removeChild(canvas);
|
||||
window.URL.revokeObjectURL(img.src);
|
||||
resolve(dataURL);
|
||||
};
|
||||
img.crossOrigin = "anonymous";
|
||||
if (file.type.startsWith("image/svg+xml")) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
if (reader.result && typeof reader.result === "string") {
|
||||
img.src = reader.result;
|
||||
}
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
} else if (file.type.startsWith("image/gif")) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
if (reader.result) {
|
||||
resolve(reader.result as string);
|
||||
}
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
img.src = window.URL.createObjectURL(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* FileCard 组件相关 结束 */
|
||||
|
||||
/* 日期时间工具函数 开始 */
|
||||
export const {
|
||||
formatDateTime,
|
||||
formatSendTime,
|
||||
isValidSendTime,
|
||||
getCurrentSendTime,
|
||||
toSendTimeFormat
|
||||
} = dateUtils;
|
||||
/* 日期时间工具函数 结束 */
|
||||
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* @description: 判断值是否未某个类型
|
||||
*/
|
||||
export function is(val: unknown, type: string) {
|
||||
return Object.prototype.toString.call(val) === `[object ${type}]`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为函数
|
||||
*/
|
||||
export function isFunction<T = Function>(val: unknown): val is T {
|
||||
return is(val, "Function");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否已定义
|
||||
*/
|
||||
export const isDef = <T = unknown>(val?: T): val is T => {
|
||||
return typeof val !== "undefined";
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否未定义
|
||||
*/
|
||||
export const isUnDef = <T = unknown>(val?: T): val is T => {
|
||||
return !isDef(val);
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否为对象
|
||||
*/
|
||||
export const isObject = (val: any): val is Record<any, any> => {
|
||||
return val !== null && is(val, "Object");
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否为时间
|
||||
*/
|
||||
export function isDate(val: unknown): val is Date {
|
||||
return is(val, "Date");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为数值
|
||||
*/
|
||||
export function isNumber(val: unknown): val is number {
|
||||
return is(val, "Number");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为AsyncFunction
|
||||
*/
|
||||
export function isAsyncFunction<T = any>(val: unknown): val is Promise<T> {
|
||||
return is(val, "AsyncFunction");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为promise
|
||||
*/
|
||||
export function isPromise<T = any>(val: unknown): val is Promise<T> {
|
||||
return is(val, "Promise") && isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为字符串
|
||||
*/
|
||||
export function isString(val: unknown): val is string {
|
||||
return is(val, "String");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为boolean类型
|
||||
*/
|
||||
export function isBoolean(val: unknown): val is boolean {
|
||||
return is(val, "Boolean");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为数组
|
||||
*/
|
||||
export function isArray(val: any): val is Array<any> {
|
||||
return val && Array.isArray(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否客户端
|
||||
*/
|
||||
export const isClient = () => {
|
||||
return typeof window !== "undefined";
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否为浏览器
|
||||
*/
|
||||
export const isWindow = (val: any): val is Window => {
|
||||
return typeof window !== "undefined" && is(val, "Window");
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否为 element 元素
|
||||
*/
|
||||
export const isElement = (val: unknown): val is Element => {
|
||||
return isObject(val) && !!val.tagName;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 是否为 null
|
||||
*/
|
||||
export function isNull(val: unknown): val is null {
|
||||
return val === null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为 null || undefined
|
||||
*/
|
||||
export function isNullOrUnDef(val: unknown): val is null | undefined {
|
||||
return isUnDef(val) || isNull(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 是否为 16 进制颜色
|
||||
*/
|
||||
export const isHexColor = (str: string) => {
|
||||
return /^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(str);
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ElLoading } from "element-plus";
|
||||
|
||||
const loadingStack = [];
|
||||
|
||||
const showLoading = (text='数据加载中...') => {
|
||||
const loadingInstance = ElLoading.service({
|
||||
lock: true,
|
||||
text: `${text}...`,
|
||||
background: "rgba(228, 228, 228, 0.57)",
|
||||
});
|
||||
loadingStack.push(loadingInstance);
|
||||
return loadingInstance;
|
||||
};
|
||||
|
||||
const hiddenLoading = () => {
|
||||
if (loadingStack.length > 0) {
|
||||
const loadingInstance = loadingStack.pop();
|
||||
loadingInstance.close();
|
||||
}
|
||||
};
|
||||
|
||||
// 强制关闭所有 Loading(可选)
|
||||
const hiddenAllLoading = () => {
|
||||
while (loadingStack.length > 0) {
|
||||
const loadingInstance = loadingStack.pop();
|
||||
loadingInstance.close();
|
||||
}
|
||||
};
|
||||
|
||||
export { showLoading, hiddenLoading, hiddenAllLoading };
|
||||
@@ -0,0 +1,13 @@
|
||||
// 显示错误
|
||||
import { ElNotification } from "element-plus";
|
||||
export function showMsg(t: string, type = "error", isCatch = true, reason?: string, duration = 2000) {
|
||||
ElNotification({
|
||||
title: t,
|
||||
type,
|
||||
duration
|
||||
});
|
||||
if (isCatch && type != "success") {
|
||||
throw Error(reason || t);
|
||||
}
|
||||
// if (isCatch && type != 'success') { return reason || t; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @description Loading Svg
|
||||
*/
|
||||
export const loadingSvg = `
|
||||
<path class="path" d="
|
||||
M 30 15
|
||||
L 28 17
|
||||
M 25.61 25.61
|
||||
A 15 15, 0, 0, 1, 15 30
|
||||
A 15 15, 0, 1, 1, 27.99 7.5
|
||||
L 15 15
|
||||
" style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
|
||||
`;
|
||||
@@ -0,0 +1,3 @@
|
||||
export const toBeian = () => {
|
||||
window.open("https://beian.miit.gov.cn/", "_blank");
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { Ref } from "vue";
|
||||
import { computed } from "vue";
|
||||
|
||||
export default function useFileNameParser(name: Ref<string | undefined>) {
|
||||
const namePrefix = computed(() => {
|
||||
const nameStr = name.value || "";
|
||||
const lastDotIndex = nameStr.lastIndexOf(".");
|
||||
return lastDotIndex === -1 ? nameStr : nameStr.slice(0, lastDotIndex);
|
||||
});
|
||||
|
||||
const nameSuffix = computed(() => {
|
||||
const nameStr = name.value || "";
|
||||
const lastDotIndex = nameStr.lastIndexOf(".");
|
||||
if (lastDotIndex === -1 && nameStr.length - lastDotIndex > 10) {
|
||||
// 文件名长度超过10个字符 显示.file
|
||||
return ".file";
|
||||
}
|
||||
return lastDotIndex === -1 ? "" : nameStr.slice(lastDotIndex);
|
||||
});
|
||||
|
||||
return {
|
||||
namePrefix,
|
||||
nameSuffix
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user