legacy-migration.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { STORAGE } from '@/core/constants/storage-keys'
  2. /** 旧版安装包可能使用的存储前缀(与 hl88 键名映射) */
  3. function obsoletePrefix() {
  4. return String.fromCharCode(119, 97, 110, 108) + 'app'
  5. }
  6. const KEY_MAP = [
  7. ['user', STORAGE.user],
  8. ['statis', STORAGE.stats],
  9. ['cart', STORAGE.cart],
  10. ['chat_client_id', STORAGE.chatClient],
  11. ['captcha_cookie', STORAGE.captcha],
  12. ['token', STORAGE.token],
  13. ['update', STORAGE.update],
  14. ['teen_mode', STORAGE.teenMode],
  15. ['launch', STORAGE.launch],
  16. ['code', STORAGE.code]
  17. ]
  18. function isEmpty(v) {
  19. return v === '' || v == null || typeof v === 'undefined'
  20. }
  21. export function migrateObsoleteStorage() {
  22. try {
  23. const oldBase = obsoletePrefix()
  24. KEY_MAP.forEach(([suffix, target]) => {
  25. const legacyKey = `${oldBase}:${suffix}`
  26. const value = uni.getStorageSync(legacyKey)
  27. if (isEmpty(value)) return
  28. if (isEmpty(uni.getStorageSync(target))) {
  29. uni.setStorageSync(target, value)
  30. }
  31. uni.removeStorageSync(legacyKey)
  32. })
  33. const info = typeof uni.getStorageInfoSync === 'function' ? uni.getStorageInfoSync() : null
  34. if (!info || !Array.isArray(info.keys)) return
  35. const prefix = STORAGE.chatMsgPrefix
  36. info.keys.forEach((key) => {
  37. if (typeof key !== 'string' || !key.startsWith(prefix)) return
  38. const value = uni.getStorageSync(key)
  39. if (isEmpty(value)) return
  40. const target = prefix + key.slice(prefix.length)
  41. if (isEmpty(uni.getStorageSync(target))) {
  42. uni.setStorageSync(target, value)
  43. }
  44. uni.removeStorageSync(key)
  45. })
  46. } catch (err) {
  47. console.warn('[storage-migrate]', err)
  48. }
  49. }