nankai-cms-website-gov/utils/util.js
2025-03-13 10:14:57 +08:00

121 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useDefaultStore } from '../stores'
// 获取Store
function getStore () {
return useDefaultStore()
}
/**
* 为对象、数组、字符串等数据去空
*
* @param data 数据
* @returns {string|null|*}
*/
export function trim (data) {
if (data == null) {
return null
}
if (typeof data === 'string') {
return data.trim()
}
if (data instanceof Array) {
for (const item of data) {
trim(item)
}
}
if (typeof data === 'object') {
for (const key in data) {
data[key] = trim(data[key])
}
}
return data
}
/**
* 根据编码表达式获取字典或数据标签
*
* @param codeExpress 编码表达式
* 语法1“字典编码.数据编码”如GENDER.MALE
* 语法2“字典编码”如GENDER
*/
export function getDictLabel (codeExpress) {
const dictMap = getStore().clientConfig.dictMap
if (dictMap == null) {
return ''
}
const codes = codeExpress.split('.')
const dictCode = codes[0]
const dataValue = codes[1]
const dict = dictMap[dictCode]
if (dict == null) {
return codeExpress
}
// 如果不存在数据编码,则直接返回字典名称
if (dataValue == null) {
return dict.name
}
const data = dict.dataList.find(d => d.value === dataValue)
if (data == null) {
return codeExpress
}
return data.label
}
/**
* 获取图片路径
*
* @param fileKey 文件key
* @returns {*}
*/
export function getImageURL (fileKey) {
if (fileKey.startsWith('http://') || fileKey.startsWith('https://')) {
return fileKey
}
return `https://nankaicms.adl66.com/Api/Client/resource/oss/image?f=${fileKey}`
}
/**
* 获取附件路径
*
* @param fileKey 文件key
* @param filename 文件名称
* @returns {*}
*/
export function getAttachURL (fileKey, filename) {
if (fileKey.startsWith('http://') || fileKey.startsWith('https://')) {
return fileKey
}
return `https://nankaicms.adl66.com/Api/Client/resource/oss/attach?f=${fileKey}&fn=${filename}`
}
/**
* 严格包装模板数据
* - 返回代理对象,获取不到模板数据时将抛出错误
*
* @param templateData 模板数据
* @param defaultValues 数据默认值
* @return Proxy
*/
export function strictPackage (templateData, defaultValues = {}) {
if (templateData == null) {
return null
}
if (templateData instanceof Array) {
return templateData
}
if (typeof templateData !== 'object') {
return templateData
}
return new Proxy(templateData, {
get (target, key) {
if (target[key] === undefined) {
if (defaultValues[key] != null) {
return defaultValues[key]
}
throw new Error(`缺少 ${key} 数据配置`)
}
return target[key]
}
})
}