StorageUtil.ts 953 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * 本地存储工具类
  3. * @author 冯聪
  4. */
  5. export default class StorageUtil {
  6. /**
  7. * 获取存储
  8. * @param key key值
  9. * @param decrypt 是否需要解密
  10. */
  11. public getStorage(key: string, decrypt = true): any {
  12. if (cc.sys.localStorage.getItem(key)) {
  13. if(decrypt){
  14. }
  15. return JSON.parse(cc.sys.localStorage.getItem(key));
  16. }
  17. else {
  18. return null;
  19. }
  20. }
  21. /**
  22. * 存储
  23. * @param key key值
  24. * @param value 要存储的value值
  25. * @param encrypt 是否需要加密
  26. */
  27. public setStorage(key: string, value: any, encrypt: boolean = true) {
  28. if (encrypt) {
  29. value = mk.encrypt.encrypt(value, g.storageData.storageKey, g.appData.appId);
  30. }
  31. cc.sys.localStorage.setItem(key, JSON.stringify(value));
  32. }
  33. }