說明
h5提供了LocalStorage本地存盤能力,但是如果直接使用不是很方便,所以我封裝了以下幾種型別,達到與其他型別幾乎相同的使用方式,
- BaseStorage: 存盤類的基類,
- LocalValue :數值型別,存盤float,int,string等
- LocalList :串列型別相當于陣列
- LocalMap :key-value型別,
- StorageHelper: 用于呼叫各個引擎提供的LocalStorage類,使用各種形式的加密演算法,對資料進行讀寫,
類圖

關鍵代碼
import StorageHelper from "./StorageHelper";
export default abstract class BaseStorage {
//存檔的值
protected value: any;
//存檔的key
protected key: string;
//初始值
protected initValue: any;
//是否有資料
protected dataFlag: boolean = true;
constructor(key, initValue) {
this.key = key;
this.initValue = https://www.cnblogs.com/cgw0827/p/initValue;
this.dataFlag = this.loadValue();
}
/**
* 是否已經有存檔資料
*/
isHaveData() {
return this.dataFlag;
}
protected abstract loadValue(): boolean;
//保存
protected saveValue() {
StorageHelper.setJsonBase64(this.key, this.value);
}
//獲取資料
protected getStorage() {
return StorageHelper.getJsonBase64(this.key)
}
//獲取值
getValue() {
return this.value;
}
//設定值
setValue(value, save: boolean = true) {
if (this.value != value) {
this.value = value;
if (save) {
this.saveValue();
}
}
}
}
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 = https://www.cnblogs.com/cgw0827/p/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();
}
}
}
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 = https://www.cnblogs.com/cgw0827/p/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();
}
}
}
import BaseStorage from "./BaseStorage";
import { isNull } from "./Define";
export default class LocalValue extends BaseStorage {
protected loadValue() {
let localValue = https://www.cnblogs.com/cgw0827/p/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)
}
}
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 = https://www.cnblogs.com/cgw0827/p/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 = https://www.cnblogs.com/cgw0827/p/Base64.decode(localValue);
return value;
}
}
使用方式
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();
}
}
如果對此內容感興趣可關注我的公眾號查看其他內容,

歡迎掃碼關注公眾號《微笑游戲》,瀏覽更多內容,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/27736.html
標籤:其他
