1. 存储localStorage
export const setStore = (name, content) => {
if (!name) return
content = JSON.stringify(content)
window.localStorage.setItem(name, content)
}
2. 获取localStorage
export const getStore = (name) => {
if (!name) return
const obj = window.localStorage.getItem(name)
if (obj && obj != 'undefined' && obj != 'null') {
return JSON.parse(obj)
}
}
3. 删除localStorage
export const removeStore = (name) => {
if (!name) return
window.localStorage.removeItem(name)
}
4. 格式化日期
export const dateFtt = (timestamp, fmt='yyyy-MM-dd') => {
let date = new Date(timestamp)
var 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 (var 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
}
5. 判断是否是移动设备
export const isMobile = () => {
return (
navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/i) ||
navigator.userAgent.match(/Windows Phone/i)
)
}
6. 下载
function download(url, fileName = '') {
let fileUrl = url
const a = document.createElement('a')
a.href = fileUrl
console.log('fileUrl', fileUrl)
a.target = '_blank'
a.download = fileName
console.log('a', a)
a.click()
}
7. 通过深度连接打开应用程序
function openExe(url) {
let isDone = true
let timeout = null
const iframe = document.createElement('iframe')
const btn = document.createElement('button')
let p = new Promise((resolve, reject) => {
btn.onclick = function () {
launchApplication(
url,
function () {
resolve('success')
},
function () {
reject('fail')
}
)
}
})
iframe.style.display = 'none'
btn.style.position = 'fixed'
btn.style.top = '-9999px'
document.body.appendChild(iframe)
document.body.appendChild(btn)
btn.click()
function done() {
isDone = true
btn.onblur = null
iframe.onerror = null
clearTimeout(timeout)
}
function launchApplication(url, success, fail) {
if (!isDone) return
isDone = false
btn.focus()
btn.onblur = function () {
if (document.activeElement && document.activeElement !== btn) {
btn.focus()
} else {
console.log('openExe-success')
done()
success()
}
}
iframe.onerror = function () {
done()
fail()
}
try {
iframe.src = url
} catch (e) {
done()
fail()
return
}
timeout = setTimeout(function () {
done()
fail()
}, 5000)
}
return p
}
8. 分钟数转换
function (time, iss) {
if (iss) {
if (time < 60) {
return `${time}秒`
} else if (time > 60 && time < 3600) {
return `${Math.floor(time / 60)}分钟${Math.ceil(time % 60)}秒`
} else if (time > 3600) {
return `${Math.floor(time / 3600)}小时${Math.floor((time % 3600) / 60)}分钟${Math.ceil((time % 3600) % 60)}秒`
}
} else {
if (time < 60) {
return `${time}分钟`
} else if (time > 60 && time < 60 * 24) {
return `${Math.floor(time / 60)}小时${Math.ceil(time % 60)}分钟`
} else if (time > 60 * 24 && time < 60 * 24 * 365) {
return `${Math.floor(time / (60 * 24))}天${Math.floor((time % (60 * 24)) / 60)}小时${(Math.ceil(time % (60 * 24)) % 60) %
60}分钟`
}
}
}
9. 数组去重
function unique(arr) {
return Array.from(new Set(arr))
}
10.函数防抖
function debounce(func, wait, immediate) {
let timer
return function () {
let context = this,
args = arguments
if (timer) clearTimeout(timer)
if (immediate) {
let callNow = !timer
timer = setTimeout(() => {
timer = null
}, wait)
if (callNow) func.apply(context, args)
} else {
timer = setTimeout(() => {
func.apply(context, args)
}, wait)
}
}
}
11.函数节流
function throttle(func, wait) {
let previous = 0
return function () {
let now = Date.now()
let context = this
let args = arguments
if (now - previous > wait) {
func.apply(context, args)
previous = now
}
}
}
12.数组转树形数据
function listToTree(data, parentIdKey = 'parentId', idKey = 'id', childrenKey = 'children') {
let result = [];
let map = {};
data.forEach((item) => {
map[item[idKey]] = item;
});
data.forEach((item) => {
let parent = map[item[parentIdKey]];
if (parent) {
(parent[childrenKey] || (parent[childrenKey] = [])).push(item);
} else {
result.push(item);
}
});
return result;
}