instructions
H5 provides LocalStorage capability of LocalStorage, but it is not very convenient to use directly. So I’ve encapsulated the following types to be used in almost the same way as the others.
- BaseStorage: The base class of the storage class.
- LocalValue: A numeric type that stores float, int, string, etc
- LocalList: A list type equivalent to an array
- LocalMap: indicates the key-value type.
- StorageHelper: Used to invoke the LocalStorage class provided by each engine. Use various forms of encryption algorithms to read and write data.
The class diagram
The key code
import StorageHelper from "./StorageHelper"; Export default abstract class BaseStorage {// Protected value: any; // Save key protected key: string; // Initial value protected initValue: any; // Whether there is data protected dataFlag: Boolean = true; constructor(key, initValue) { this.key = key; this.initValue = initValue; this.dataFlag = this.loadValue(); } /** * isHaveData() {return this.dataflag; } protected abstract loadValue(): boolean; / / save protected saveValue () {StorageHelper. SetJsonBase64 (value). This key, this.; } / / get the data protected getStorage () {return StorageHelper. GetJsonBase64 (enclosing key)} / / get the value getValue () {return. This value; } setValue(value, save: Boolean = true) {if (this.value! = value) { this.value = value; if (save) { this.saveValue(); }}}}Copy the code
import BaseStorage from "./BaseStorage"; import { isNull } from "./Define"; export default class LocalList extends BaseStorage { protected value: any[]; get(index: number) { if (index >= 0 && index <= this.value.length - 1) { return this.value[index]; } else { this.set(index, this.initValue) return this.initValue; } } protected loadValue() { let localValue = this.getStorage(); if (! isNull(localValue)) { this.setValue(localValue) return true; } else { this.value = [] return false } } size() { return this.value.length; } set(index: number, value: any, save: boolean = true) { if (this.value[index] ! = value) { this.value[index] = value; if (save) { this.saveValue(); } } } remove(index) { if (! isNull(this.value[index])) { delete this.value[index] this.saveValue(); }}}Copy the code
import { isNull } from "./Define"; import BaseStorage from "./BaseStorage"; export default class LocalMap extends BaseStorage { protected value: Object; protected count: number = 0; protected loadValue() { let localValue = this.getStorage(); if (! isNull(localValue)) { this.setValue(localValue) for (const key in localValue) { if (localValue.hasOwnProperty(key)) { this.count++; } } return true } else { this.value = {} return false } } size() { return this.count; } get(key: any) { let data = this.value[key]; if (isNull(data)) { data = this.initValue; } this.set(key, data) return data; } has(key: any) { return ! isNull(this.value[key]) } updateValue(key, value) { this.set(key, this.get(key) + value) } set(key: any, value) { if (! this.value[key]) { this.count++; } if (this.value[key] ! = value) { this.value[key] = value this.saveValue(); } } remove(key) { if (this.value[key]) { this.count--; delete this.value[key] this.saveValue(); }}}Copy the code
import BaseStorage from "./BaseStorage";
import { isNull } from "./Define";
export default class LocalValue extends BaseStorage {
protected loadValue() {
let localValue = this.getStorage();
if (isNull(localValue)) {
this.setValue(this.initValue);
return true
} else {
this.value = localValue;
return false
}
}
updateValue(value: number) {
let data = this.value + value;
if (data < 0) {
data = 0;
}
this.setValue(data)
}
}
Copy the code
import Base64 from "./Base64" export default class StorageHelper { static get(key) { return Laya.LocalStorage.getItem(key); } static set(key, value) { Laya.LocalStorage.setItem(key, value); } static clear() { Laya.LocalStorage.clear(); } static remove(key) { Laya.LocalStorage.removeItem(key); } static setJson(key, value) { this.set(key, JSON.stringify(value)); } static getJson(key) { let value = this.get(key); if (! value) { return null; }; return JSON.parse(value); } static getJsonBase64(key) { let localValue = this.get(key); if (! localValue) { return null; }; let string = Base64.decode(localValue); if (string) { try { let value = JSON.parse(string); return value; } catch (error) { } } return {}; } static setJsonBase64(key, value) { this.set(key, Base64.encode(JSON.stringify(value))); } static setBase64(key, value) { this.set(key, Base64.encode(value)); } static getBase64(key) { let localValue = this.get(key); if (! localValue) { return ''; }; let value = Base64.decode(localValue); return value; }}Copy the code
use
Export default class Player{// private _gold: LocalValue; init(){ this._gold = new LocalValue(this.playerName + 'gold', 100) } setGold(num: number) { this._gold.setValue(Math.floor(num)) } getGold() { return this._gold.getValue(); }}Copy the code
If you are interested in this content, you can follow my public account to view other content.
Welcome to scan the public account “Smile Game”, browse more content.