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) {
return data;
// 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 => {
const queryArr = hash.split('?');
if (queryArr.length === 1) {
return {}
}
const query = {}
queryArr[1].split('&').forEach(item => {
const [key, value] = item.split('=');
query[key] = value;
})
return query
}
// ip比较大小
export const compareIp = (ip1, ip2) => {
const ip1Arr = ip1.split('.');
const ip2Arr = ip2.split('.');
let flag;
for (let i = 0; i < 4; i++) {
if (Number(ip1Arr[i]) > Number(ip2Arr[i])) {
flag = 0;
break;
} else if (Number(ip1Arr[i]) < Number(ip2Arr[i])) {
flag = 1
}
}
return flag;
}
export function makeTimeStamp() {
return dayjs().format('YYYYMMDDHHmmss')
}
function isObject(obj) {
return typeof obj === 'object' && obj != null;
}
export function isObjectValueEqual(a, b) {
const aProps = Object.keys(a);
const bProps = Object.keys(b);
if (aProps.length != bProps.length) {
return false;
}
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
}
export const getValueByPath = function(object, prop) {
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
}
export const jumpRouter = function(id, router) {
// 默认根据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
}