您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-js 本地数据存储封装

2017-04-14 17:59 423 查看

封装代码

/**
* 文件使用说明:用户存储相关处理js文件(基于cocos2d-js 3.6.1)。
*
* 1、可通过customStorage.setEncrypted设置是否采用加密模式,默认加密(加密函数引用了CryptoJS v3.1.2库文件,此封装依赖于此库,若开启加密则必须引用其代码文件,当然也可以自己替换成其他加密)。
* 2、可使用customStorage.clearData删除指定key的存档。
* 3、可使用customStorage.clearAllData删除所有存档。
* 4、可使用customStorage.Bool|Int|String新建存储对象,并用get()获取,set(value)存储。
*/

/**
* 游戏数据IO持久化存储静态类
*/
var customStorage = customStorage ||
{
/**
* 设置是否加密(默认加密)
* @param value
*/
setEncrypted:function(value)
{
this._private._encrypted = (value==false?false:true);
},
/**
* 读取指定数据
* @param key 数据Key
* @returns
*/
readData:function(key)
{
var readValue = cc.sys.localStorage.getItem(this._private._getSaveKey(key));

if(readValue!=null)
{
var realValue = this._private._getRealValue(readValue, key);

cc.log("read data %s=%s",key,realValue);

return realValue;
}

return null;
},
/**
* 保存指定数据
* @param key 数据Key
* @param data 数据
*/
saveData:function(key,data)
{
cc.log("save data %s=%s",key,data);

cc.sys.localStorage.setItem(this._private._getSaveKey(key),this._private._getSaveValue(data,key));
},
/**
* 清除指定数据
* @param key 数据Key
*/
clearData:function(key)
{
if(this._encrypted)
{
cc.sys.localStorage.removeItem(CryptoJS.HmacMD5(key,key));
}
else
{
cc.sys.localStorage.removeItem(key);
}
},
/**
* 清除所有数据
*/
clearAllData:function()
{
cc.sys.localStorage.clear();
},
// 数据处理相关方法和标识变量
_private :
{
// 是否加密标识,默认加密
_encrypted:true,
// 获取数据存储时使用的数据Key
_getSaveKey:function(key)
{
if(this._encrypted)
{
return "" + CryptoJS.HmacMD5(""+key,""+key);
}
else
{
return "" + key;
}
},
// 获取数据存储时使用的数据
_getSaveValue:function(data,key)
{
if(this._encrypted)
{
return "" + CryptoJS.AES.encrypt(""+data,""+key);
}
else
{
return "" + data;
}
},
// 获取数据读取后解密的数据
_getRealValue:function(data,key)
{
if(this._encrypted)
{
return CryptoJS.AES.decrypt(""+data,""+key).toString(CryptoJS.enc.Utf8);
}
else
{
return data;
}
}
}
};

/**
* 数据存取解析相关静态方法集合类(包含Bool,Int,String三种基本类型数据,其他类型可通过此三种扩展获得)
*/
customStorage.parseSets = customStorage.parseSets ||
{
/**
* Bool类型数据存取相关函数
*/
boolParse:
{
read:function(key,defaultVal)
{
var readVal = customStorage.readData(key);
if(readVal==null/* ||readVal=="" */)
{
customStorage.saveData(key, defaultVal);
return defaultVal;
}
else
{
return readVal=="false"?false:true;
}
},
save:function(value)
{
return value;
}
},
/**
* Int类型数据存取相关函数
*/
intParse:
{
read:function(key,defaultVal)
{
var readVal = customStorage.readData(key);
if(readVal==null/* ||readVal=="" */)
{
customStorage.saveData(key, defaultVal);
return defaultVal;
}
else
{
return parseInt(readVal);
}
},
save:function(value)
{
return value;
}
},
/**
* String类型数据存取相关函数
*/
stringParse:
{
read:function(key,defaultVal)
{
var readVal = customStorage.readData(key);
if(readVal==null/* ||readVal=="" */)
{
customStorage.saveData(key, defaultVal);
return defaultVal;
}
else
{
return readVal;
}
},
save:function(value)
{
return value;
}
}
};

/**
* 游戏存档数据原型基础类
*/
customStorage.commonDataProto = function(key,defaultVal,parseFunction)
{
this._private = this._private ||
{
_saveKey:key,
_saveParse:parseFunction,
_saveData:parseFunction.read(key,defaultVal)
};

this.get = function()
{
return this._private._saveData;
};

this.set = function(data)
{
this._private._saveData = data;
customStorage.saveData(this._private._saveKey, ""+this._private._saveParse.save(this._private._saveData));
};

return this;
};

/**
* 游戏存档数据Bool类
*/
customStorage.Bool = function(key,defaultVal)
{
this._private = new customStorage.commonDataProto("b_"+key, defaultVal, customStorage.parseSets.boolParse);

this.get = function()
{
return this._private.get();
};

this.set = function(data)
{
this._private.set(data);
};
return this;
};

/**
* 游戏存档数据Int类
*/
customStorage.Int = function(key,defaultVal)
{
this._private = new customStorage.commonDataProto("i_"+key, defaultVal, customStorage.parseSets.intParse);

this.get = function()
{
retur
a5c4
n this._private.get();
};

this.set = function(data)
{
this._private.set(data);
};
return this;
};

/**
* 游戏存档数据String类
*/
customStorage.String = function(key,defaultVal)
{
this._private = new customStorage.commonDataProto("s_"+key, defaultVal, customStorage.parseSets.stringParse);

this.get = function()
{
return this._private.get();
};

this.set = function(data)
{
this._private.set(data);
};
return this;
};


使用示例

首先定义存储字段

/**
* 用户数据声明文件,处理与用户数据相关的内容
*/
var customLocalData = customLocalData ||
{
init:function()
{
// 设定存储是否加密,不调用默认加密
customStorage.setEncrypted(false);

// 此处之所以讲此字段定义在init函数内,是为了延迟数据的初始化,可以控制初始化时机。
// 没有像init函数一样写成规范的成员,是为了清晰易修改

// 声音存储
customLocalData.SoundOn = new customStorage.Bool("SoundOn",true);

// 分数存储
customLocalData.Score = new customStorage.Int("Score",0);

// 昵称存储
customLocalData.NickName = new customStorage.String("NickName","");
},
destory:function()
{
customLocalData = undefined;
}
};


游戏启动后调用初始化函数,调用后会自动从本地存储加载存档,在初始化后存储就可以正常使用了

// 存档初始化
customLocalData.init();

// 声音开关读取
var soundOn = customLocalData.SoundOn.get();

// 声音开关改变
customLocalData.SoundOn.set(false);

// 分数读取
var score = customLocalData.Score.get();

// 分数改变
customLocalData.Score.set(100);

// 昵称读取
var nickName = customLocalData.NickName.get();

// 分数改变
customLocalData.NickName.set("Nick_001");


依赖库下载

CryptoJS下载后将“CryptoJS-master.zip\CryptoJS-master\rollups\”下的aes.js和hmac-md5.js引入项目即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-js 本地存储