cmc-web/packages/common/utils/index.js

193 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
/**
* Created by HaijunZhang on 2019/7/23.
*/
import Clipboard from 'clipboard'
import { getToken } from 'utils/auth'
import dayjs from './day'
export function wrapperParams(data) {
2024-08-21 01:17:14 +00:00
return data
2024-08-20 12:11:31 +00:00
// return { params: JSON.stringify(data) }
}
export const formatEqParams = params => {
return { page: 1, rows: 9999, params: JSON.stringify([{ param: params, sign: 'EQ' }]) }
}
export const copyText = (text, event, successCallback, errorCallback) => {
const clipboard = new Clipboard(event.target, {
text: () => text
})
clipboard.on('success', () => {
successCallback && successCallback()
clipboard.destroy()
})
clipboard.on('error', () => {
errorCallback && errorCallback()
clipboard.destroy()
})
clipboard.onClick(event)
}
// export const downloadFile = (url, params = {}) => {
// request
// .get(url, {
// // headers: { 'Content-Type': params.fileFormat },
// responseType: 'blob',
// // params: wrapperParams(params),
// options: {
// isBlob: true
// }
// })
// .then(data => {
// var ele = document.createElement('a') // 创建下载链接
// ele.download = 'filename' // 设置下载的名称
// ele.style.display = 'none' // 隐藏的可下载链接
// // 字符内容转变成blob地址
// const blob = new Blob([data])
// ele.href = URL.createObjectURL(blob)
// // 绑定点击时间
// document.body.appendChild(ele)
// ele.click()
// // 然后移除
// document.body.removeChild(ele)
// })
// }
export const downloadFile = (url, params = {}) => {
let str = ''
Object.keys(params).forEach(item => {
str += `&${item}=${params[item]}`
})
window.location.href = encodeURI(`/api${url}?token=${getToken()}${str}`)
}
export const getQuery = hash => {
2024-08-21 01:17:14 +00:00
const queryArr = hash.split('?')
2024-08-20 12:11:31 +00:00
if (queryArr.length === 1) {
return {}
}
const query = {}
queryArr[1].split('&').forEach(item => {
2024-08-21 01:17:14 +00:00
const [key, value] = item.split('=')
query[key] = value
2024-08-20 12:11:31 +00:00
})
return query
}
// ip比较大小
export const compareIp = (ip1, ip2) => {
2024-08-21 01:17:14 +00:00
const ip1Arr = ip1.split('.')
const ip2Arr = ip2.split('.')
let flag
2024-08-20 12:11:31 +00:00
for (let i = 0; i < 4; i++) {
if (Number(ip1Arr[i]) > Number(ip2Arr[i])) {
2024-08-21 01:17:14 +00:00
flag = 0
break
2024-08-20 12:11:31 +00:00
} else if (Number(ip1Arr[i]) < Number(ip2Arr[i])) {
flag = 1
}
}
2024-08-21 01:17:14 +00:00
return flag
2024-08-20 12:11:31 +00:00
}
export function makeTimeStamp() {
return dayjs().format('YYYYMMDDHHmmss')
}
function isObject(obj) {
2024-08-21 01:17:14 +00:00
return typeof obj === 'object' && obj != null
2024-08-20 12:11:31 +00:00
}
export function isObjectValueEqual(a, b) {
2024-08-21 01:17:14 +00:00
const aProps = Object.keys(a)
const bProps = Object.keys(b)
2024-08-20 12:11:31 +00:00
if (aProps.length != bProps.length) {
2024-08-21 01:17:14 +00:00
return false
2024-08-20 12:11:31 +00:00
}
for (let i = 0; i < aProps.length; i++) {
const propName = aProps[i]
const propA = a[propName]
const propB = b[propName]
if (isObject(propA) && isObject(propB)) {
return isObjectValueEqual(propA, propB)
} else if (propA !== propB) {
return false
}
}
return true
}
2024-08-21 01:17:14 +00:00
export const getValueByPath = function (object, prop) {
2024-08-20 12:11:31 +00:00
prop = prop || ''
const paths = prop.split('.')
let current = object
let result = null
for (let i = 0, j = paths.length; i < j; i++) {
const path = paths[i]
if (!current) break
if (i === j - 1) {
result = current[path]
break
}
current = current[path]
}
return result
}
2024-08-21 01:17:14 +00:00
export const jumpRouter = function (id, router) {
2024-08-20 12:11:31 +00:00
// 默认根据name跳转
this.$router.push({
name: router,
params: {
id
}
})
}
export function tagsList(list) {
return list.map(item => {
item.id = item.name
const tree = []
item.jsTrees.map(item1 => {
item1.jsTrees.map(item2 => {
item2.jsTrees.map(item3 => {
item3.id = item3.name
item3.jsTrees.map(item4 => {
// const ids = item4.jsTrees.map(item5 => item5.id).join(',')
item4.id = item4.jsTrees[0].id
item4.trees = JSON.parse(JSON.stringify(item4.jsTrees))
item4.jsTrees = null
})
})
tree.push(...item2.jsTrees)
})
})
item.jsTrees = tree
return item
})
}
export function selectTagList(list, select) {
const isDeep = select.some(item => item instanceof Array)
let idList = []
if (isDeep) {
// 多选,二元数组
idList = select.map(item => {
return item[2]
})
} else {
// 单选,一元数组
idList = [select[2]]
}
const arr = []
list.map(item => {
item.jsTrees.map(item1 => {
item1.jsTrees.map(item2 => {
if (idList.indexOf(item2.id) > -1) {
// arr.push(item2)
arr.push(item2.trees)
// item2.trees.map(item3 => {
// arr.push(item3)
// })
}
})
})
})
return arr
}