wip: vite版重构中

This commit is contained in:
kailong321200875
2021-10-16 09:40:39 +08:00
parent 41ca05dce2
commit a8163874dc
165 changed files with 15146 additions and 145 deletions

208
src/utils/index.ts Normal file
View File

@@ -0,0 +1,208 @@
import { AxiosResponse } from 'axios'
/**
* 对象数组深拷贝
* @param {Array,Object} source 需要深拷贝的对象数组
* @param {Array} noClone 不需要深拷贝的属性集合
*/
export function deepClone(source: any, noClone: string[] = []): any {
if (!source && typeof source !== 'object') {
throw new Error('error arguments deepClone')
}
const targetObj: any = source.constructor === Array ? [] : {}
Object.keys(source).forEach((keys: string) => {
if (source[keys] && typeof source[keys] === 'object' && noClone.indexOf(keys) === -1) {
targetObj[keys] = deepClone(source[keys], noClone)
} else {
targetObj[keys] = source[keys]
}
})
return targetObj
}
/**
* 查找数组对象的某个下标
* @param {Array} ary 查找的数组
* @param {Functon} fn 判断的方法
*/
// eslint-disable-next-line
export function findIndex(ary: Array<any>, fn: Fn): number {
if (ary.findIndex) {
return ary.findIndex(fn)
}
let index = -1
ary.some((item: any, i: number, ary: Array<any>) => {
const ret: any = fn(item, i, ary)
if (ret) {
index = i
return ret
}
})
return index
}
/**
* 生成随机字符串
*/
export function toAnyString() {
const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
const r: number = (Math.random() * 16) | 0
const v: number = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString()
})
return str
}
/**
* 截取URL参数
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url: string) {
const search: string = url.split('?')[1]
if (!search) {
return {}
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
)
}
/**
* @param {String|Number} value 要验证的字符串或数值
* @param {*} validList 用来验证的列表
*/
export function oneOf(value: string | number, validList: string[] | number[]): boolean {
for (let i = 0; i < validList.length; i++) {
if (value === validList[i]) {
return true
}
}
return false
}
/**
* @param {date} time 需要转换的时间
* @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
*/
export function formatTime(time: any, fmt: string) {
if (!time) return ''
else {
const date = new Date(time)
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
S: date.getMilliseconds()
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (const k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
)
}
}
return fmt
}
}
/**
* 导出
* @response {objec} 接收从接口返回的response数据
*/
export function exportFile(response: AxiosResponse) {
const fileName = decodeURI(
response.headers['content-disposition']
? response.headers['content-disposition'].split(';')[1].split('=')[1]
: 'test'
)
const blob = new Blob([response.data], {
type: response.headers['content-type']
})
if (typeof (window.navigator as any).msSaveBlob !== 'undefined') {
;(window.navigator as any).msSaveBlob(blob, fileName)
} else {
const blobURL = window.URL.createObjectURL(blob) // 将blob对象转为一个URL
const tempLink = document.createElement('a') // 创建一个a标签
tempLink.style.display = 'none'
tempLink.href = blobURL
tempLink.setAttribute('download', fileName) // 给a标签添加下载属性
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink) // 将a标签添加到body当中
tempLink.click() // 启动下载
document.body.removeChild(tempLink) // 下载完毕删除a标签
window.URL.revokeObjectURL(blobURL)
}
}
/**
* 对比两个数组是否一致
* @a {array}
* @b {array}
*/
export function valueEquals(a: any[], b: any[]): boolean {
// see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
if (a === b) return true
if (!(a instanceof Array)) return false
if (!(b instanceof Array)) return false
if (a.length !== b.length) return false
for (let i = 0; i !== a.length; ++i) {
if (a[i] !== b[i]) return false
}
return true
}
/**
* IE浏览器版本
*/
export function IEVersion() {
const userAgent = navigator.userAgent // 取得浏览器的userAgent字符串
const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 // 判断是否IE<11浏览器
const isEdge = userAgent.indexOf('Edge') > -1 && !isIE // 判断是否IE的Edge浏览器
const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1
if (isIE) {
const reIE = new RegExp('MSIE (\\d+\\.\\d+);')
reIE.test(userAgent)
const fIEVersion = parseFloat(RegExp['$1'])
if (fIEVersion === 7) {
return 7
} else if (fIEVersion === 8) {
return 8
} else if (fIEVersion === 9) {
return 9
} else if (fIEVersion === 10) {
return 10
} else {
return 6 // IE版本<=7
}
} else if (isEdge) {
return 'edge' // edge
} else if (isIE11) {
return 11 // IE11
} else {
return -1 // 不是ie浏览器
}
}
/**
* 驼峰转下划线
* @val {string} 需要转换的字符串
*/
export function humpToUnderline(val: string) {
return val.replace(/([A-Z])/g, '_$1').toLowerCase()
}