series-unlock-cache.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * 合辑分集解锁状态本地缓存:
  3. * 解锁成功后写入,再次打开合辑弹层 / 进入合辑页时合并,避免重复解锁。
  4. */
  5. const STORE_KEY = 'hl88app:series_unlocked_map'
  6. const MEM_KEY = '__series_unlocked_map__'
  7. function readMap() {
  8. try {
  9. const app = getApp()
  10. if (app && app.globalData && app.globalData[MEM_KEY]) {
  11. return { ...app.globalData[MEM_KEY] }
  12. }
  13. } catch (_) {}
  14. try {
  15. const disk = uni.getStorageSync(STORE_KEY)
  16. if (disk && typeof disk === 'object') return { ...disk }
  17. } catch (_) {}
  18. return {}
  19. }
  20. function writeMap(map) {
  21. const next = map && typeof map === 'object' ? map : {}
  22. try {
  23. const app = getApp()
  24. if (app) {
  25. app.globalData = app.globalData || {}
  26. app.globalData[MEM_KEY] = next
  27. }
  28. } catch (_) {}
  29. try {
  30. uni.setStorageSync(STORE_KEY, next)
  31. } catch (_) {}
  32. }
  33. function itemKey(itemOrId) {
  34. if (itemOrId == null) return ''
  35. if (typeof itemOrId === 'object') {
  36. const id =
  37. itemOrId.series_item_id != null && itemOrId.series_item_id !== ''
  38. ? itemOrId.series_item_id
  39. : itemOrId.id
  40. return id != null ? String(id) : ''
  41. }
  42. return String(itemOrId)
  43. }
  44. /** 标记某合辑分集已解锁(可附带播放地址) */
  45. export function markSeriesItemUnlocked(seriesItemId, extra = {}) {
  46. const key = itemKey(seriesItemId)
  47. if (!key) return
  48. const map = readMap()
  49. map[key] = {
  50. at: Date.now(),
  51. url: (extra && extra.url) || (map[key] && map[key].url) || '',
  52. videoId: (extra && extra.videoId) || (map[key] && map[key].videoId) || ''
  53. }
  54. writeMap(map)
  55. }
  56. export function getSeriesUnlockRecord(seriesItemId) {
  57. const key = itemKey(seriesItemId)
  58. if (!key) return null
  59. const map = readMap()
  60. return map[key] || null
  61. }
  62. export function isSeriesItemUnlockedCached(seriesItemId) {
  63. return !!getSeriesUnlockRecord(seriesItemId)
  64. }
  65. /** 合并单条:已缓存解锁 → is_can_play=1,并尽量补 url */
  66. export function applyUnlockCacheToItem(item) {
  67. if (!item || typeof item !== 'object') return item
  68. const rec = getSeriesUnlockRecord(item)
  69. if (!rec) return item
  70. const next = { ...item, is_can_play: 1 }
  71. if (rec.url) {
  72. next.video = { ...(item.video || {}), url: rec.url }
  73. }
  74. return next
  75. }
  76. /** 合并整个 series_list */
  77. export function applyUnlockCacheToSeriesList(list) {
  78. if (!Array.isArray(list)) return []
  79. return list.map((row) => applyUnlockCacheToItem(row))
  80. }