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

314 lines
10 KiB
TypeScript
Raw Normal View History

2024-11-14 18:13:28 +08:00
import { isArray, isNumber } from '@/utils/is'
2024-11-14 11:17:22 +08:00
import { FieldNamesProps } from '@/components/ProTable/interface'
2024-08-21 14:52:36 +08:00
2024-11-14 11:17:22 +08:00
const mode = import.meta.env.VITE_ROUTER_MODE
2024-08-21 14:52:36 +08:00
/**
* @description localStorage
* @param {String} key Storage名称
* @returns {String}
*/
export function localGet(key: string) {
2024-11-14 11:17:22 +08:00
const value = window.localStorage.getItem(key)
try {
return JSON.parse(window.localStorage.getItem(key) as string)
} catch (error) {
return value
}
2024-08-21 14:52:36 +08:00
}
/**
* @description localStorage
* @param {String} key Storage名称
* @param {*} value Storage值
* @returns {void}
*/
export function localSet(key: string, value: any) {
2024-11-14 11:17:22 +08:00
window.localStorage.setItem(key, JSON.stringify(value))
2024-08-21 14:52:36 +08:00
}
/**
* @description localStorage
* @param {String} key Storage名称
* @returns {void}
*/
export function localRemove(key: string) {
2024-11-14 11:17:22 +08:00
window.localStorage.removeItem(key)
2024-08-21 14:52:36 +08:00
}
/**
* @description localStorage
* @returns {void}
*/
export function localClear() {
2024-11-14 11:17:22 +08:00
window.localStorage.clear()
2024-08-21 14:52:36 +08:00
}
/**
* @description
* @param {*} val
* @returns {String}
*/
export function isType(val: any) {
2024-11-14 11:17:22 +08:00
if (val === null) return 'null'
if (typeof val !== 'object') return typeof val
else return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase()
2024-08-21 14:52:36 +08:00
}
/**
* @description uuid
* @returns {String}
*/
export function generateUUID() {
2024-11-14 11:17:22 +08:00
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
2024-08-21 14:52:36 +08:00
}
/**
*
* @param {Object} a
* @param {Object} b
* @returns {Boolean} true false
*/
export function isObjectValueEqual(a: { [key: string]: any }, b: { [key: string]: any }) {
2024-11-14 11:17:22 +08:00
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
}
2024-08-21 14:52:36 +08:00
}
2024-11-14 11:17:22 +08:00
return true
2024-08-21 14:52:36 +08:00
}
/**
* @description
* @param {Number} min
* @param {Number} max
* @returns {Number}
*/
export function randomNum(min: number, max: number): number {
2024-11-14 11:17:22 +08:00
let num = Math.floor(Math.random() * (min - max) + max)
return num
2024-08-21 14:52:36 +08:00
}
/**
* @description
* @returns {String}
*/
export function getTimeState() {
2024-11-14 11:17:22 +08:00
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 `凌晨好 🌛`
2024-08-21 14:52:36 +08:00
}
/**
* @description
* @returns {String}
*/
export function getBrowserLang() {
2024-11-14 11:17:22 +08:00
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
2024-08-21 14:52:36 +08:00
}
/**
* @description url + params
* @returns {String}
*/
export function getUrlWithParams() {
2024-11-14 11:17:22 +08:00
const url = {
hash: location.hash.substring(1),
history: location.pathname + location.search,
}
return url[mode]
2024-08-21 14:52:36 +08:00
}
/**
* @description 使便
* @param {Array} menuList
* @returns {Array}
*/
export function getFlatMenuList(menuList: Menu.MenuOptions[]): Menu.MenuOptions[] {
2024-11-14 11:17:22 +08:00
let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList))
return newMenuList.flatMap(item => [item, ...(item.children ? getFlatMenuList(item.children) : [])])
2024-08-21 14:52:36 +08:00
}
/**
* @description 使 ( isHide == true )
* @param {Array} menuList
* @returns {Array}
* */
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
2024-11-14 11:17:22 +08:00
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
})
2024-08-21 14:52:36 +08:00
}
/**
* @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 } = {}) => {
2024-11-14 11:17:22 +08:00
for (const item of menuList) {
result[item.path] = [...parent, item]
if (item.children) getAllBreadcrumbList(item.children, result[item.path], result)
}
return result
}
2024-08-21 14:52:36 +08:00
/**
* @description 使 path (使)
* @param {Array} menuList
* @param {Array} menuPathArr ['**','**']
* @returns {Array}
*/
export function getMenuListPath(menuList: Menu.MenuOptions[], menuPathArr: string[] = []): string[] {
2024-11-14 11:17:22 +08:00
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
2024-08-21 14:52:36 +08:00
}
/**
* @description path (使)
* @param {Array} menuList
* @param {String} path 访
* @returns {Object | null}
*/
export function findMenuByPath(menuList: Menu.MenuOptions[], path: string): Menu.MenuOptions | null {
2024-11-14 11:17:22 +08:00
for (const item of menuList) {
if (item.path === path) return item
if (item.children) {
const res = findMenuByPath(item.children, path)
if (res) return res
}
2024-08-21 14:52:36 +08:00
}
2024-11-14 11:17:22 +08:00
return null
2024-08-21 14:52:36 +08:00
}
/**
* @description 使 name (使)
* @param {Array} menuList
* @param {Array} keepAliveNameArr name ['**','**']
* @returns {Array}
* */
export function getKeepAliveRouterName(menuList: Menu.MenuOptions[], keepAliveNameArr: string[] = []) {
2024-11-14 11:17:22 +08:00
menuList.forEach(item => {
item.meta.isKeepAlive && item.name && keepAliveNameArr.push(item.name)
item.children?.length && getKeepAliveRouterName(item.children, keepAliveNameArr)
})
return keepAliveNameArr
2024-08-21 14:52:36 +08:00
}
/**
* @description (el-table-column)
* @param {Number} row
* @param {Number} col
* @param {*} callValue
* @returns {String}
* */
export function formatTableColumn(row: number, col: number, callValue: any) {
2024-11-14 11:17:22 +08:00
// 如果当前值为数组,使用 / 拼接(根据需求自定义)
if (isArray(callValue)) return callValue.length ? callValue.join(' / ') : '/'
2024-11-14 18:13:28 +08:00
if (isNumber(callValue)) return callValue
return callValue ?? "/";
2024-08-21 14:52:36 +08:00
}
/**
* @description ProTable ||
* @param {*} callValue
* @returns {String}
* */
export function formatValue(callValue: any) {
2024-11-14 11:17:22 +08:00
// 如果当前值为数组,使用 / 拼接(根据需求自定义)
if (isArray(callValue)) return callValue.length ? callValue.join(' / ') : '/'
2024-11-14 18:13:28 +08:00
if (isNumber(callValue)) return callValue
return callValue ?? "/";
2024-08-21 14:52:36 +08:00
}
/**
* @description prop (列如: prop: user.name)
* @param {Object} row
* @param {String} prop prop
* @returns {*}
* */
export function handleRowAccordingToProp(row: { [key: string]: any }, prop: string) {
2024-11-14 11:17:22 +08:00
if (!prop.includes('.')) return row[prop] ?? '/'
prop.split('.').forEach(item => (row = row[item] ?? '/'))
return row
2024-08-21 14:52:36 +08:00
}
/**
* @description prop prop ==> prop
* @param {String} prop prop
* @returns {String}
* */
export function handleProp(prop: string) {
2024-11-14 11:17:22 +08:00
const propArr = prop.split('.')
if (propArr.length == 1) return prop
return propArr[propArr.length - 1]
2024-08-21 14:52:36 +08:00
}
2024-08-22 11:27:06 +08:00
/**
* @description label value key值
* @param {String} callValue
* @param {Array} enumData
* @param {Array} fieldNames label && value && children key
* @param {String} type tag
* @returns {String}
* */
2024-11-14 11:17:22 +08:00
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] : '/'
}
2024-08-22 11:27:06 +08:00
}
2024-08-21 14:52:36 +08:00
/**
* @description callValue enum
* */
export function findItemNested(enumData: any, callValue: any, value: string, children: string) {
2024-11-14 11:17:22 +08:00
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)
2024-08-21 14:52:36 +08:00
}