87 lines
2.0 KiB
JavaScript
87 lines
2.0 KiB
JavaScript
/**
|
|
* Created by y.x on 18/09/06.
|
|
*/
|
|
|
|
module.exports = {
|
|
set: function(key, value){
|
|
try{
|
|
cc.sys.localStorage.setItem(key,value);
|
|
return true;
|
|
}catch(err){
|
|
console.log(err);
|
|
}
|
|
return false;
|
|
},
|
|
|
|
get: function(key){
|
|
return cc.sys.localStorage.getItem(key);
|
|
},
|
|
setjson: function(key, value){
|
|
this.set(key,JSON.stringify(value));
|
|
},
|
|
getjson:function(key){
|
|
let str = this.get(key);
|
|
if(str){
|
|
try{
|
|
return JSON.parse(str);
|
|
}catch(err){
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
remove: function(key){
|
|
cc.sys.localStorage.removeItem(key);
|
|
},
|
|
|
|
hasItem: function(key){
|
|
var result = cc.sys.localStorage.getItem(key);
|
|
if (cc.sys.platform == cc.sys.WECHAT_GAME) {
|
|
if(result == '' && typeof(result) != 'number'){
|
|
return false;
|
|
}
|
|
return true;
|
|
}else{
|
|
if(result == null || result == undefined){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// if(result){
|
|
// return true;
|
|
// }
|
|
// else{
|
|
// return false;
|
|
// }
|
|
},
|
|
|
|
setData: function(key,value){
|
|
var data = {};
|
|
data[key] = value;
|
|
cc.log("storage====set== " + JSON.stringify(data));
|
|
return this.set(key, JSON.stringify(data));
|
|
//cc.sys.localStorage.setItem(key,JSON.stringify(data));
|
|
},
|
|
|
|
getStringData: function(key){
|
|
cc.log("storage====get== start" + key);
|
|
var result = cc.sys.localStorage.getItem(key);
|
|
if (!result)
|
|
{
|
|
return "0";
|
|
}
|
|
var data = null;
|
|
try{
|
|
data = JSON.parse(result);
|
|
}catch(err){
|
|
console.log(err);
|
|
}
|
|
|
|
cc.log("storage====get== " + data);
|
|
if(!data){
|
|
return "0";
|
|
}
|
|
cc.log("storage====get== " + data[key]);
|
|
return data[key];
|
|
}
|
|
}; |