LocalStorageUtil.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Singleton } from "./Singleton";
  2. export class LocalStorageUtil extends Singleton {
  3. /**音乐 */
  4. public lst_music: string = "Bird_music";
  5. /**音效 */
  6. public lst_effect: string = "Bird_effect";
  7. /**倒计时时间*/
  8. public lst_CountTime: string = "BirdCountTime";
  9. /**倒计时秒数*/
  10. public lst_CountSecond: string = "BirdCountSecond";
  11. /**体力 */
  12. public lst_Tili: string = "BirdTili";
  13. /** 最后进入关卡*/
  14. public lst_playLevel: string = "Bird_playLevel";
  15. /** 通关进度*/
  16. public lst_passProgress: string = "Bird_passProgress";
  17. public list_foodLevel: string = "list_foodLevel";
  18. public list_kitchenLevel: string = "list_kitchenLevel";
  19. public list_missionComplete: string = "list_missionComplete";
  20. public list_missionReceive: string = "list_missionReceive";
  21. // 通关场景进度
  22. public Bird_passScene: string = "Bird_passScene";
  23. /** 通关进度*/
  24. public lst_InfiniteLevelDate: string = "Bird_infiniteLevelDate";
  25. public str_guideStep: string = "str_guideStep";
  26. /** 道具数量*/
  27. public lst_PropNum: string = "lst_PropNum";
  28. /** 金币*/
  29. public lst_Coin: string = "lst_UserCoin";
  30. public lst_UserDiamond: string = "lst_UserDiamond";
  31. /** 用户皮肤组*/
  32. public lst_getSkin: string = "Bird_getSkin";
  33. /** 用户当前使用皮肤*/
  34. public lst_dressOnSkin: string = "Bird_dressOnSkin";
  35. /** 新手引导标志*/
  36. public lst_guide: string = "Bird_guide";
  37. // /** 免费视频次数 */
  38. // public lst_free_watch_times: string = "sausage_free_watch_times";
  39. // /** 每日签到数据 */
  40. // public lst_daily_signin: string = "sausage_daily_signin";
  41. private secretkey: string = 'ccxh_ws111'; // 加密密钥
  42. private isSecret: boolean = false;
  43. private set(key: string, value: any) {
  44. try {
  45. if (key) {
  46. if (this.isSecret) {
  47. let dataString = JSON.stringify(value);
  48. value = window["encryptjs"].encrypt(dataString, this.secretkey, 256);
  49. }
  50. cc.sys.localStorage.setItem(key, value);
  51. }
  52. } catch (e) {
  53. console.error("设置数据出错!!!");
  54. }
  55. }
  56. //不存在key时返回null
  57. private get(key: string): any {
  58. try {
  59. if (key) {
  60. let value = cc.sys.localStorage.getItem(key);
  61. if (value) {
  62. if (this.isSecret)
  63. return JSON.parse(window["encryptjs"].decrypt(value, this.secretkey, 256));
  64. else
  65. return value;
  66. }
  67. }
  68. return null;
  69. } catch (e) {
  70. console.error("获取数据出错!!!");
  71. return null;
  72. }
  73. }
  74. /**存string */
  75. public setString(key: string, value: string) {
  76. this.set(key, value);
  77. }
  78. /**取string */
  79. public getString(key: string): string {
  80. let value = this.get(key);
  81. return value;
  82. }
  83. /**存boolean */
  84. public setBoolean(key: string, value: boolean) {
  85. let temp = value ? "1" : "0";
  86. this.set(key, temp);
  87. }
  88. /**取boolean */
  89. public getBoolean(key: string): boolean {
  90. let value = this.get(key);
  91. if (value) {
  92. if (value == "1")
  93. return true;
  94. else
  95. return false;
  96. }
  97. return null;
  98. }
  99. /**存number */
  100. public setNumber(key: string, value: number, encode?: boolean) {
  101. if (value != null && value != undefined) {
  102. // if (value == Math.floor(value) && encode) { //整数
  103. // value = EncryptUtil.enValue(value);
  104. // }
  105. this.set(key, value.toString());
  106. }
  107. }
  108. /**取number */
  109. public getNumber(key: string, decode?: boolean): number {
  110. let temp = this.get(key);
  111. if (temp) {
  112. if ((temp as string).indexOf(".") >= 0) {
  113. return parseFloat(temp);
  114. } else {
  115. let value = parseInt(temp);
  116. // return EncryptUtil.desValue(value);
  117. }
  118. }
  119. return temp;
  120. }
  121. /**存json */
  122. public setJsonObj(key: string, value: Object) {
  123. if (value != null && value != undefined) {
  124. this.set(key, JSON.stringify(value));
  125. }
  126. }
  127. /**取json */
  128. public getJsonObj(key: string): Object {
  129. let temp = this.get(key);
  130. if (temp != null) {
  131. return JSON.parse(temp);
  132. }
  133. return temp;
  134. }
  135. /**清空本地存在数据 */
  136. public clearAll() {
  137. cc.sys.localStorage.clear();
  138. }
  139. /**删除数据key */
  140. public deleteKey(key: string) {
  141. let temp = this.get(key);
  142. if (temp != null) {
  143. cc.sys.localStorage.removeItem(key);
  144. }
  145. }
  146. }