LocalStorageManager.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** @format */
  2. import {Log} from '../utils/LogUtils'
  3. import {ConstValue} from '../data/ConstValue'
  4. export class LocalStorageManager {
  5. // 所有服务器通用数据标记
  6. public readonly ONLY_TAG = 'ONLY_TAG:'
  7. // 正式环境数据标记
  8. public readonly RELEASE_TAG = 'RELEASE_TAG:'
  9. private userID
  10. private serverID
  11. public getRealKey(curKey: string) {
  12. if (!curKey.includes(this.ONLY_TAG) && this.serverID && this.userID) {
  13. curKey += `_[${this.serverID}]|[${this.userID}]`
  14. }
  15. if (ConstValue.DEBUG === true && !curKey.includes(this.RELEASE_TAG)) {
  16. curKey += '_debug'
  17. }
  18. return curKey
  19. }
  20. init(userID: string, serverID: string) {
  21. this.userID = userID
  22. this.serverID = serverID
  23. }
  24. public get(k: string) {
  25. const realKey = this.getRealKey(k)
  26. const value = cc.sys.localStorage.getItem(realKey)
  27. return value
  28. }
  29. public getDefault(k: string, d: any) {
  30. let value = this.get(k)
  31. if (value !== d && (value === null || value === '' || value === 'null' || value === 'undefined')) {
  32. value = d
  33. this.set(k, value)
  34. }
  35. return value
  36. }
  37. public getNumber(k: string, d?: number) {
  38. let value
  39. if (d === undefined) {
  40. value = this.get(k)
  41. } else if (typeof d === 'number') {
  42. value = this.getDefault(k, d)
  43. } else {
  44. Log.error('getNumber', 'default is not number')
  45. return undefined
  46. }
  47. if (value === null || value === '' || value === 'undefined') {
  48. return undefined
  49. }
  50. return Number(value)
  51. }
  52. public getNumberByID(k: string, id: string, d?: number) {
  53. return this.getNumber(`${k}|ONLY_ID|${id}`, d)
  54. }
  55. public getString(k: string, d?: string) {
  56. let value
  57. if (d === undefined) {
  58. value = this.get(k)
  59. } else if (typeof d === 'string') {
  60. value = this.getDefault(k, d)
  61. } else {
  62. Log.error('getString', 'default is not string')
  63. return undefined
  64. }
  65. return value
  66. }
  67. public getBool(k: string, d?: boolean) {
  68. let value
  69. if (d === undefined) {
  70. value = this.get(k)
  71. } else if (typeof d === 'boolean') {
  72. value = this.getDefault(k, d.toString())
  73. } else {
  74. Log.error('getBool', 'default is not boolean')
  75. return undefined
  76. }
  77. if (value === 'true') {
  78. value = true
  79. } else if (value === 'false') {
  80. value = false
  81. } else if (value === null || value === '' || value === 'undefined') {
  82. return undefined
  83. } else {
  84. Log.warn('getBool', 'key:', k, 'value is not boolean:', value)
  85. return undefined
  86. }
  87. return value
  88. }
  89. public getObject(k: string, d?: object) {
  90. let value
  91. if (d === undefined) {
  92. value = this.get(k)
  93. } else if (typeof d === 'object') {
  94. value = this.getDefault(k, JSON.stringify(d))
  95. } else {
  96. Log.error('getObject', 'default is not object')
  97. return undefined
  98. }
  99. if (value === null || value === '' || value === 'undefined') {
  100. return undefined
  101. }
  102. return JSON.parse(value)
  103. }
  104. public set(k: string, v: any) {
  105. let value
  106. if (typeof v === 'number' || typeof v === 'boolean') {
  107. value = v.toString()
  108. } else if (typeof v === 'object') {
  109. value = JSON.stringify(v)
  110. } else if (typeof v === 'string') {
  111. value = v
  112. } else {
  113. return
  114. }
  115. this.setAyn(k, value)
  116. }
  117. public setByID(k: string, id: string, v: any) {
  118. this.set(`${k}|ONLY_ID|${id}`, v)
  119. }
  120. public setAyn(k: string, v: string) {
  121. const realKey = this.getRealKey(k)
  122. switch (cc.sys.platform) {
  123. case cc.sys.WECHAT_GAME:
  124. // @ts-ignore
  125. wx.setStorage({key: realKey, data: v})
  126. break
  127. default:
  128. cc.sys.localStorage.setItem(realKey, v)
  129. break
  130. }
  131. }
  132. public delete(k: string) {
  133. const realKey = this.getRealKey(k)
  134. cc.sys.localStorage.removeItem(realKey)
  135. }
  136. public clear() {
  137. cc.sys.localStorage.clear()
  138. }
  139. }