76 lines
2.1 KiB
JavaScript
76 lines
2.1 KiB
JavaScript
/**
|
|
* 根据屏幕宽度自适应计算字体大小(基于1920设计稿)
|
|
* @param {number} res - 设计稿上的原始尺寸
|
|
* @returns {number} 自适应后的尺寸
|
|
*/
|
|
export function fontSize(res) {
|
|
let clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
|
|
if (!clientWidth) return
|
|
let fontSize = clientWidth / 1920
|
|
return res * fontSize
|
|
}
|
|
|
|
/**
|
|
* 生成随机背景色
|
|
* @param {String} type 'hex' | 'rgb' 输出格式,默认 hex
|
|
* @returns {String} '#RRGGBB' 或 'rgb(r,g,b)'
|
|
* @example randomBgColor()
|
|
*/
|
|
export function randomBgColor(type = 'hex') {
|
|
if (type === 'rgb') {
|
|
const r = Math.floor(Math.random() * 256)
|
|
const g = Math.floor(Math.random() * 256)
|
|
const b = Math.floor(Math.random() * 256)
|
|
return `rgb(${r},${g},${b})`
|
|
}
|
|
// 默认 hex
|
|
return (
|
|
'#' +
|
|
Math.floor(Math.random() * 0xffffff)
|
|
.toString(16)
|
|
.padStart(6, '0')
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 根据 url 递归查找对应的 label
|
|
* @param {Array} tree - header 数组
|
|
* @param {String} path - 要匹配的 url
|
|
* @returns {String|undefined} 找到返回 label,否则 undefined
|
|
*/
|
|
export function findLabelByUrl(tree, path) {
|
|
for (const node of tree) {
|
|
if (node.url === path) return node.label
|
|
if (node.hasChildren && node.children) {
|
|
const child = findLabelByUrl(node.children, path)
|
|
if (child) return child
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get参数处理
|
|
* @param {*} params 参数
|
|
*/
|
|
export function tansParams(params) {
|
|
let result = ''
|
|
for (const propName of Object.keys(params)) {
|
|
const value = params[propName]
|
|
const part = `${encodeURIComponent(propName)}=`
|
|
if (value !== null && typeof value !== 'undefined') {
|
|
if (typeof value === 'object') {
|
|
for (const key of Object.keys(value)) {
|
|
if (value[key] !== null && typeof value[key] !== 'undefined') {
|
|
const params = `${propName}[${key}]`
|
|
const subPart = `${encodeURIComponent(params)}=`
|
|
result += `${subPart + encodeURIComponent(value[key])}&`
|
|
}
|
|
}
|
|
} else {
|
|
result += `${part + encodeURIComponent(value)}&`
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|