工具ts文件
util.ts
export function timeFix() {
const time = new Date()
const hour = time.getHours()
return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}
export function isIE() {
const bw = window.navigator.userAgent
const compare = (s: string) => bw.indexOf(s) >= 0
const ie11 = (() => 'ActiveXObject' in window)()
return compare('MSIE') || ie11
}
export const _debounce = (fn: () => void, delay = 200) => {
let timer: NodeJS.Timeout = null
return () => {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null
fn()
}, delay)
}
}
export const _throttle = (fn: () => void, interval = 200) => {
let last: number
let timer: NodeJS.Timeout = null
return function () {
let now = +new Date()
if (last && now - last < interval) {
clearTimeout(timer)
timer = setTimeout(function () {
last = now
fn()
}, interval)
} else {
last = now
fn()
}
}
}
export const patDown = (array: any = [], key: any = 'children', _response: any = []) => {
array.forEach((e: { [x: string]: any }) => {
_response.push(e)
e[key] && e[key].length > 0 && patDown(e[key], key, _response)
})
return _response
}
export const patDown2 = (
array: any = [],
conditions: any = { sonK: 'children', k: 'value', v: '123' },
_response: any = []
) => {
_response = array
for (let k = 0; k < _response.length; k++) {
const e = _response[k]
if (e[conditions.sonK].length === 0 && e[conditions.k] !== conditions.v) {
_response.splice(k, 1)
k--
}
e[conditions.sonK] && e[conditions.sonK].length > 0 && patDown2(e[conditions.sonK], conditions, _response)
}
return _response
}
export const editorAll = (data = [], callback, children = 'children') => {
data.forEach((e, i) => {
callback(e, data, i)
if (e[children] && e[children].length > 0) editorAll(e[children], callback, children)
})
}
export const inlineTreeData = (data: Array<any>) => {
let count = data.length
data.forEach((e) => (e.children && e.children.length > 0 ? inlineTreeData(e.children) : --count))
!count && data.forEach((item) => (item['class'] = 'inlineTreeNodeSon'))
return data
}
export const testPassword = (value: string) => {
let type = true
const reg = new RegExp(
/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[_!@#\$%\^&\*`~()\-\+=])[0-9a-zA-Z_!@#\$%\^&\*`~()\-\+=]{8,16}$/
)
if (reg.test(value)) {
const arr = value.split('')
for (let i = 0; i < arr.length - 2; i++) {
if (arr[i] === arr[i + 1] && arr[i] === arr[i + 2]) {
console.log('不合规', arr[i], arr[i + 1], arr[i + 2])
type = false
break
}
}
} else {
type = false
}
return type
}
export const fileSizeConversion = (e: string | number) => {
const num = parseInt(`${e}`)
if (num > 1024 * 1024) {
return (num / 1024 / 1024).toFixed(2) + ' MB'
} else if (num > 1024) {
return (num / 1024).toFixed(2) + ' KB'
} else {
return num + ' B'
}
}
export const base64Svg2Png = (svg: string) => {
return new Promise((resolve, reject) => {
if (svg.indexOf('svg+xml') !== -1) return resolve(svg)
const img = new Image();
img.src = svg;
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
let ImgBase64 = canvas.toDataURL('image/png');
resolve(ImgBase64);
}
})
}
export const downloadFileInIframe = (url = '') => {
const iframe = document.createElement("iframe");
iframe.src = url;
iframe.style.cssText = "width: 0; height: 0;border:0;";
console.log(iframe);
document.body.appendChild(iframe);
setTimeout(() => {
iframe.remove();
}, 200);
}