Files
pqs-9100_client/frontend/src/styles/index.ts

312 lines
9.8 KiB
TypeScript
Raw Normal View History

2024-11-14 11:34:25 +08:00
import { isArray } from "@/utils/is";
import { FieldNamesProps } from "@/components/ProTable/interface";
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) {
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 label value key值
* @param {String} callValue
* @param {Array} enumData
* @param {Array} fieldNames label && value && children key
* @param {String} type tag
* @returns {String}
* */
export function filterEnum(callValue: any, enumData?: any, fieldNames?: FieldNamesProps, type?: "tag") {
const value = fieldNames?.value ?? "value";
const label = fieldNames?.label ?? "label";
const children = fieldNames?.children ?? "children";
let filterData: { [key: string]: any } = {};
// 判断 enumData 是否为数组
if (Array.isArray(enumData)) filterData = findItemNested(enumData, callValue, value, children);
// 判断是否输出的结果为 tag 类型
if (type == "tag") {
return filterData?.tagType ? filterData.tagType : "";
} else {
return filterData ? filterData[label] : "--";
}
}
/**
* @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);
}