StorageMgr.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * 本地数据处理
  3. */
  4. import GameLog from "./GameLogMgr";
  5. export default class StorageMgr {
  6. /**
  7. * 读取本地数据
  8. * @param key
  9. */
  10. public static read(key: string) {
  11. if (key != null) {
  12. let result = cc.sys.localStorage.getItem(key);
  13. if (result) {
  14. result = JSON.parse(result);
  15. }
  16. GameLog.log('storage read', key, result);
  17. return result;
  18. }
  19. }
  20. /**
  21. * 写入本地数据
  22. * @param key
  23. * @param value
  24. */
  25. public static save(key: string, value: any) {
  26. try {
  27. GameLog.log('storage save', key, value);
  28. if (key != null) {
  29. return cc.sys.localStorage.setItem(key, JSON.stringify(value));
  30. }
  31. } catch (error) {
  32. GameLog.error(error);
  33. }
  34. }
  35. /**
  36. * 清空本地数据
  37. */
  38. public static clear() {
  39. return cc.sys.localStorage.clear();
  40. }
  41. /**
  42. * 删除本地数据
  43. * @param key
  44. */
  45. public static rm(key: string) {
  46. if (key != null) {
  47. return cc.sys.localStorage.removeItem(key);
  48. }
  49. }
  50. }