LocalStorageMgr.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Tool from "./Tool";
  2. import UserInfoMgr from "./user/UserInfoMgr";
  3. class LocalStorageMgr{
  4. constructor() {
  5. console.log('LocalStorageMgr初始化');
  6. }
  7. getDailyNum(key){
  8. let jsonStr = cc.sys.localStorage.getItem(key);
  9. if(jsonStr){
  10. let jsonObj = JSON.parse(jsonStr);
  11. let tempKey = key + UserInfoMgr.getUserId() + (new Date()).getDate();
  12. return jsonObj[tempKey] || 0;
  13. }else{
  14. return 0;
  15. }
  16. }
  17. setDailyNum(key, num){
  18. let jsonStr = cc.sys.localStorage.getItem(key);
  19. let jsonObj = {};
  20. if(jsonStr){
  21. jsonObj = JSON.parse(jsonStr);
  22. }
  23. let tempKey = key + UserInfoMgr.getUserId() + (new Date()).getDate();
  24. jsonObj[tempKey] = jsonObj[tempKey] || 0;
  25. jsonObj[tempKey] = num;
  26. let newJsonObj = {};
  27. newJsonObj[tempKey] = jsonObj[tempKey];
  28. cc.sys.localStorage.setItem(key,JSON.stringify(newJsonObj));
  29. }
  30. getLocalValue(key, defValue=0){
  31. let vaule = cc.sys.localStorage.getItem(key);
  32. if(Tool.IsEmpty(vaule)) {
  33. return defValue;
  34. }
  35. return vaule;
  36. }
  37. setLocalValue(key, data){
  38. cc.sys.localStorage.setItem(key, data);
  39. }
  40. }
  41. let instance = null;
  42. LocalStorageMgr.getInstance = function(){
  43. if(!instance){
  44. instance = new LocalStorageMgr();
  45. }
  46. return instance;
  47. }
  48. export default LocalStorageMgr.getInstance();