/**
* @description globelLocal操作
*/
import { objectTool } from 'mwutil'
class Local {
constructor (name) {
this.store = window.localStorage
this.storeName = name
}
// 设置一个local
set (key, value) {
//存储
let storeData = this.store.getItem(this.storeName)
if(!storeData){
this.init()
storeData = this.store.getItem(this.storeName)
}
storeData = JSON.parse(storeData)
storeData.data[key] = value
this.store.setItem(this.storeName, JSON.stringify(storeData))
return storeData.data
}
// 批量设置local
batchSet (obj) {
if (!objectTool.isObject(obj)) {
return
}
for (const key in obj) {
this.set(key, obj[key])
}
}
// 获取一个local
get (key) {
//读取
let result = this.getAll()
if (!result) {
return null
}
return result[key]
}
// 批量获取local
batchGet () {
let result = {}
const allLocal = this.getAll()
if (!allLocal) {
return result
}
for (var i = 0; i < arguments.length; i++) {
const item = arguments[i]
result[item] = allLocal[item]
}
return result
}
// 获取全部
getAll () {
let storeData = this.store.getItem(this.storeName)
if(!storeData){
return null
}
storeData = JSON.parse(storeData)
return storeData.data
}
// 删除一个
remove (key) {
//读取
let storeData = this.store.getItem(this.storeName)
if(!storeData){
return
}
storeData = JSON.parse(storeData)
delete storeData.data[key]
this.store.setItem(this.storeName,JSON.stringify(storeData))
return storeData.data
}
// 清除
clear () {
//清除对象
this.store.removeItem(this.storeName)
}
// 初始化
init () {
this.store.setItem(this.storeName,'{"data":{}}')
}
}
export default new Local('__global')
=============================================================================
使用封装:
// 批量获取
globalLocal.batchGet('a', 'b')
// 单个获取
globalLocal.get('token')
// 清空
globalLocal.clear()
// 批量设置
globalLocal.batchSet({
a: aValue,
b: bValue
}
// 单个设置
globalLocal.set(a, aValue)
// 获取全部
globalLocal.getAll()
// 删除一个
globalLocal.remove(a)