oak-frontend-base/es/features/localStorage.js

111 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { unset } from 'oak-domain/lib/utils/lodash';
import { LOCAL_STORAGE_KEYS } from '../constant/constant';
import { Feature } from '../types/Feature';
export class LocalStorage extends Feature {
keys;
constructor() {
super();
if (process.env.NODE_ENV === 'development') {
// development环境下debugStore的数据也默认存放在localStorage中
this.keys = {
[LOCAL_STORAGE_KEYS.debugStore]: true,
[LOCAL_STORAGE_KEYS.debugStoreStat]: true,
};
}
else {
this.keys = {};
}
}
setKey(key) {
if (!this.keys[key]) {
this.keys[key] = true;
}
}
unsetKey(key) {
if (this.keys[key]) {
unset(this.keys, key);
}
}
async save(key, item) {
this.setKey(key);
switch (process.env.OAK_PLATFORM) {
case 'wechatMp': {
wx.setStorageSync(key, item);
break;
}
case 'web': {
localStorage.setItem(key, JSON.stringify(item));
break;
}
default: {
throw new Error('尚未支持');
}
}
}
async load(key) {
this.setKey(key);
switch (process.env.OAK_PLATFORM) {
case 'wechatMp': {
return wx.getStorageSync(key);
}
case 'web': {
const data = localStorage.getItem(key);
if (data) {
return JSON.parse(data);
}
return undefined;
}
default: {
throw new Error('尚未支持');
}
}
}
async clear() {
this.keys = {};
switch (process.env.OAK_PLATFORM) {
case 'wechatMp': {
wx.clearStorageSync();
break;
}
case 'web': {
localStorage.clear();
break;
}
default: {
throw new Error('尚未支持');
}
}
}
async remove(key) {
this.unsetKey(key);
switch (process.env.OAK_PLATFORM) {
case 'wechatMp': {
wx.removeStorageSync(key);
break;
}
case 'web': {
localStorage.removeItem(key);
break;
}
default: {
throw new Error('尚未支持');
}
}
}
async loadAll() {
const data = {};
for (const k in this.keys) {
Object.assign(data, {
[k]: await this.load(k),
});
}
return data;
}
async resetAll(data) {
this.clear();
for (const k in data) {
await this.save(k, data[k]);
}
}
}