import { validatenull } from '@/util/validate'
import website from '@/const/website'
const keyName = website.key + '-'
export const setStore = (params = {}) => {
let {
name,
content,
type
} = params
name = keyName + name
const obj = {
dataType: typeof (content),
content: content,
type: type,
datetime: new Date().getTime()
}
if (type) window.sessionStorage.setItem(name, JSON.stringify(obj))
else window.localStorage.setItem(name, JSON.stringify(obj))
}
export const getStore = (params = {}) => {
let {
name,
debug
} = params
name = keyName + name
let obj = {}
let content
obj = window.sessionStorage.getItem(name)
if (validatenull(obj)) obj = window.localStorage.getItem(name)
if (validatenull(obj)) return
try {
obj = JSON.parse(obj)
} catch (e) {
return obj
}
if (debug) {
return obj
}
if (obj.dataType === 'string') {
content = obj.content
} else if (obj.dataType === 'number') {
content = Number(obj.content)
} else if (obj.dataType === 'boolean') {
content = eval(obj.content)
} else if (obj.dataType === 'object') {
content = obj.content
}
return content
}
export const removeStore = (params = {}) => {
let {
name,
type
} = params
name = keyName + name
if (type) {
window.sessionStorage.removeItem(name)
} else {
window.localStorage.removeItem(name)
}
}
export const getAllStore = (params = {}) => {
const list = []
const {
type
} = params
if (type) {
for (let i = 0; i <= window.sessionStorage.length; i++) {
list.push({
name: window.sessionStorage.key(i),
content: getStore({
name: window.sessionStorage.key(i),
type: 'session'
})
})
}
} else {
for (let i = 0; i <= window.localStorage.length; i++) {
list.push({
name: window.localStorage.key(i),
content: getStore({
name: window.localStorage.key(i)
})
})
}
}
return list
}
export const clearStore = (params = {}) => {
const { type } = params
if (type) {
window.sessionStorage.clear()
} else {
window.localStorage.clear()
}
}
state: {
tagList: getStore({
name: 'tagList'
}) || [],
tag: getStore({
name: 'tag'
}) || tagWel,
tagWel: tagWel,
activeTagList: getStore({
name: 'activeTagList'
}) || {},
activeTag: getStore({
name: 'activeTag'
}) || {},
menuId: getStore({
name: 'menuId'
}) || -1,
},
ADD_TAG: (state, action) => {
state.tag = action
setStore({
name: 'tag',
content: state.tag,
type: 'session'
})
if (state.tagList.some(ele => diff(ele, action))) return
state.tagList.push(action)
setFistTag(state.tagList)
setStore({
name: 'tagList',
content: state.tagList,
type: 'session'
})
},