树形数据筛选 子节点存在展示父节点
getFilterArr (arr, str) {
const data = arr.filter(item => {
if (item.children) {
item.children = this.getFilterArr(item.children, str)
if (item.children.length) return true
}
return !!item.name.includes(str);
})
return data
}
保留小数点的方法 四舍五入
scoreToFixed(num, n) {
const changeNum = (Math.round(num * Math.pow(10, n)) / Math.pow(10, n) + Math.pow(10, -(n + 1))).toString().slice(0, -1);
return parseFloat(changeNum).toFixed(n);
},
防抖函数的全部封装
let timeout
export function Debounce (func, wait = 2000, immediate = false) {
if (timeout !== null) clearTimeout(timeout)
if (immediate) {
const callNow = !timeout
timeout = setTimeout(function () {
timeout = null
}, wait)
if (callNow) typeof func === 'function' && func()
} else {
timeout = setTimeout(function () {
typeof func === 'function' && func()
}, wait)
}
}
## loadsh引入再vue中使用
<template>
<div>
<el-button @click="handelClick">点击防抖事件</el-button>
</div>
</template>
<script>
import _ from 'lodash'
export default {
methods: {
getRemote: _.debounce(function () {
console.log('this', this)
console.log('我呗打印了')
}, 1000),
handelClick () {
this.getRemote()
}
}
}
</script>
节流函数的封装使用
/**
* 节流
* @param {Function} func 要执行的回调函数
* @param {Number} wait 延时的时间
* @param {Boolean} immediate 是否立即执行
* @return null
*/
// eslint-disable-next-line no-unused-vars
let timer, flag
export function throttle (func, wait = 300, immediate = true) {
if (immediate) {
if (!flag) {
flag = true
// 如果是立即执行,则在wait毫秒内开始时执行
typeof func === 'function' && func()
timer = setTimeout(() => {
flag = false
}, wait)
}
} else {
if (!flag) {
flag = true
// 如果是非立即执行,则在wait毫秒内的结束处执行
timer = setTimeout(() => {
flag = false
typeof func === 'function' && func()
}, wait)
}
}
}
vue中调用
<template>
<div>
<el-button @click="handelDebounce">防抖</el-button>
</div>
</template>
<script>
import { Debounce, throttle } from '@/utils/common.js'
export default {
data () {
return {
}
},
methods: {
handelDebounce () {
Debounce(this.handelLog)
throttle(this.handelLog, 2000, false)
},
handelLog () {
console.log(22222)
}
}
}
</script>
<style>
</style>
https://blog.csdn.net/mysunli/article/details/111379328 //外网实现的地址
路由参数加密解密,防止用户改路由参数进行页面操作
import CryptoJS from 'crypto-js'
const key = CryptoJS.enc.Utf8.parse('1234123412ABCDEF')
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412')
export function Encrypt (word) {
let srcs = CryptoJS.enc.Utf8.parse(word)
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.ciphertext.toString().toUpperCase()
}
export function Decrypt (word) {
let str = null
if (word) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(word)
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
let decrypt = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
str = decryptedStr.toString()
}
return str
}
export default {
Decrypt,
Encrypt
}