38、
arraySortByKey(list, key) {
function charCompare(charA, charB) {
if (charA === undefined || charA === null || charA === '' || charA === ' ' || charA === ' ') {
return -1
}
if (charB === undefined || charB === null || charB === '' || charB === ' ' || charB === ' ') {
return 1
}
if ((notChinese(charA) && notChinese(charB)) || (!notChinese(charA) && !notChinese(charB))) {
return charA.localeCompare(charB)
} else {
if (notChinese(charA)) {
return -1
} else {
return 1
}
}
}
function notChinese(char) {
const charCode = char.charCodeAt(0)
return charCode >= 0 && charCode <= 128
}
if (list === undefined || list === null) return []
list.sort((a, b) => {
let strA = a[key]
let strB = b[key]
if (strA === undefined || strA === null || strA === '' || strA === ' ' || strA === ' ') {
return -1
}
if (strB === undefined || strB === null || strB === '' || strB === ' ' || strB === ' ') {
return 1
}
if ((strA.split('').every(char => notChinese(char)) && strB.split('').every(char => notChinese(char))) ||
(strA.split('').every(char => !notChinese(char)) && strB.split('').every(char => !notChinese(char)))) {
return strA.localeCompare(strB)
} else {
const charAry = strA.split('')
for (const i in charAry) {
if ((charCompare(strA[i], strB[i]) !== 0)) {
return charCompare(strA[i], strB[i])
}
}
return -1
}
})
return list
},
39、节流、防抖
throttle(fn, delay = 1000) {
let that = this
const begin = this.begin || 0
this.begin = Date.now()
return function (...args) {
let current = Date.now()
if (current - begin > delay) {
fn.apply(that, args)
}
}
},
debounce(fn, delay = 1000) {
let that = this
let timer = this.timer
return function (...args) {
clearTimeout(timer)
that.timer = setTimeout(function () {
fn.apply(that, args);
}, delay)
}
},
40、 加密
encryption(publicKey, password) {
let encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey)
let psw = encryptor.encrypt(password)
return psw
},
41、blob转json
blobtoJson(blobData) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = function (event) {
const message = JSON.parse(reader.result)
resolve(message)
}
reader.readAsText(blobData)
})
},
42、获取地址栏参数,用于解析token
/**
* 获取地址栏参数,用于解析token
* @returns {Object}
*/
getUrlParms: function () {
var args = {}
// 获取查询串
var query = location.search.split('?')[1]
if (query) {
// 在逗号处断开
var pairs = query.split("&")
var len = pairs.length
for (var i = 0
// 查找name=value
var pos = pairs[i].indexOf('=')
// 如果没有找到就跳过
if (pos === -1) {
continue
}
// 存为属性
args[pairs[i].substring(0, pos)] = unescape(pairs[i].substring(pos + 1))
}
}
return args
},
43、跳转到login页
toLogin: function () {
if(window.location.hash === '#/login') {
return
}
window.location.href = "#/login"
if(!!window.ActiveXObject || "ActiveXObject" in window) {
window.location.reload()
}
},