LocalData.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import Plat from "../utils/Palt";
  2. import GameUtil from "../utils/util/GameUtil";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass("LocalManager")
  5. export default class LocalData {
  6. //#region 基本函数
  7. public static str_datas: { [key: string]: string } = {};
  8. public static getItemToStr(key, val?: string): string {
  9. if (this.str_datas[key] == null) {
  10. let str = cc.sys.localStorage.getItem(key);
  11. if (str == null || str == "") {
  12. str = "";
  13. if (val) {
  14. str = val;
  15. }
  16. this.str_datas[key] = str;
  17. }
  18. else {
  19. this.str_datas[key] = str;
  20. }
  21. }
  22. return this.str_datas[key];
  23. }
  24. public static setItemToStr(key: string, val: string) {
  25. this.str_datas[key] = val;
  26. cc.sys.localStorage.setItem(key, val);
  27. }
  28. public static int_datas: { [key: string]: number } = {};
  29. public static getItemToNumber(key, val?: number): number {
  30. if (this.int_datas[key] == null) {
  31. let str = cc.sys.localStorage.getItem(key);
  32. if (str == null || str == "") {
  33. let v = 0;
  34. if (val) {
  35. v = val;
  36. }
  37. this.int_datas[key] = v;
  38. }
  39. else {
  40. this.int_datas[key] = parseInt(str);
  41. }
  42. }
  43. return this.int_datas[key];
  44. }
  45. public static setItemToNumber(key: string, val: number): any {
  46. this.int_datas[key] = val;
  47. cc.sys.localStorage.setItem(key, val + "");
  48. }
  49. //#endregion
  50. public static yx: boolean = true;
  51. public static yy: boolean = true;
  52. public static set lv(curLevel) {
  53. Plat.setRank(curLevel)
  54. this.setItemToNumber("curLevel", curLevel)
  55. }
  56. public static get lv() {
  57. return this.getItemToNumber("curLevel", 1);
  58. }
  59. public static set openid(id) {
  60. this.setItemToStr("openid", id);
  61. }
  62. public static get openid() {
  63. return this.getItemToStr("openid");
  64. }
  65. private static _userinfo: any = null;
  66. public static set userinfo(info) {
  67. this._userinfo = info;
  68. this.setItemToStr("userinfo", JSON.stringify(info));
  69. }
  70. public static get userinfo() {
  71. if (this._userinfo == null) {
  72. let str = this.getItemToStr("userinfo");
  73. if (str == "") {
  74. this._userinfo = null;
  75. }
  76. else {
  77. this._userinfo = JSON.parse(str);
  78. }
  79. }
  80. return this._userinfo;
  81. }
  82. public static set userid(id) {
  83. this.setItemToStr("userid", id);
  84. }
  85. public static get userid() {
  86. let str = this.getItemToStr("userid");
  87. if (str == "") {
  88. str = "" + Date.now() + GameUtil.randomRange(0, 10000000);
  89. this.setItemToStr("userid", str);
  90. }
  91. return str;
  92. }
  93. public static get catSkinList(): number[] {
  94. let skinListStr = localStorage.getItem("local_data_list");
  95. if (!skinListStr) {
  96. skinListStr = "{}"
  97. }
  98. const skinList = JSON.parse(skinListStr)
  99. return skinList
  100. }
  101. public static set catSkinList(v: number[]) {
  102. }
  103. }