indicator-favorite.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* ==============================================================
  2. * utils/indicator-favorite.js - 关注指标收藏(本地持久化)
  3. * ⚠️ 当前为本地持久化(小程序 Storage),后续若需跨端同步再接后端。
  4. * 本文件注释即契约:不要在此接后端,等有跨端同步需求时再改。
  5. * 隔离维度:登录用户 userId + 家庭 familyId + 成员 memberId + 报告类型 sourceType。
  6. * 同一存储 key 只对应一个「用户×家庭×成员×报告类型」视角,避免串号
  7. * (不同账号/不同成员/不同报告类型之间的收藏互不干扰)。
  8. * 持久化结构:key → number[](definitionId 列表)
  9. * ============================================================== */
  10. const KEY_PREFIX = 'cfc:indicator:fav'
  11. /**
  12. * 拼装存储 key(维度隔离;缺失维度用占位符,避免不同账号/成员互相覆盖)
  13. * @param {{userId?:*, familyId?:*, memberId?:*, sourceType?:*}} scope
  14. * @returns {string}
  15. */
  16. export function buildFavoriteKey(scope) {
  17. var s = scope || {}
  18. var userId = (s.userId === undefined || s.userId === null || s.userId === '') ? 'anon' : String(s.userId)
  19. var familyId = (s.familyId === undefined || s.familyId === null || s.familyId === '') ? 'nofamily' : String(s.familyId)
  20. var memberId = (s.memberId === undefined || s.memberId === null || s.memberId === '') ? 'nomember' : String(s.memberId)
  21. var sourceType = (s.sourceType === undefined || s.sourceType === null || s.sourceType === '') ? 'all' : String(s.sourceType)
  22. return KEY_PREFIX + ':' + userId + ':' + familyId + ':' + memberId + ':' + sourceType
  23. }
  24. /** 数字数组去重、丢弃非法值 */
  25. function normalizeIds(val) {
  26. if (val === undefined || val === null || val === '') return []
  27. if (typeof val === 'string') {
  28. try { val = JSON.parse(val) } catch (e) { return [] }
  29. }
  30. if (!Array.isArray(val)) return []
  31. var out = []
  32. var seen = {}
  33. for (var i = 0; i < val.length; i++) {
  34. var n = Number(val[i])
  35. if (isNaN(n)) continue
  36. if (seen[n]) continue
  37. seen[n] = true
  38. out.push(n)
  39. }
  40. return out
  41. }
  42. /**
  43. * 读取某视角的收藏指标 definitionId 列表
  44. * @param {Object} scope
  45. * @returns {number[]}
  46. */
  47. export function readFavorites(scope) {
  48. var key = buildFavoriteKey(scope)
  49. var val
  50. try {
  51. val = uni.getStorageSync(key)
  52. } catch (e) {
  53. return []
  54. }
  55. return normalizeIds(val)
  56. }
  57. /**
  58. * 写入某视角的收藏列表
  59. * @param {Object} scope
  60. * @param {number[]} ids
  61. * @returns {number[]} 写入后的规范化列表
  62. */
  63. export function writeFavorites(scope, ids) {
  64. var key = buildFavoriteKey(scope)
  65. var arr = normalizeIds(ids)
  66. try {
  67. uni.setStorageSync(key, arr)
  68. } catch (e) {
  69. // 存储失败静默(收藏是增强功能,不影响主流程)
  70. console.warn('[indicator-favorite] 写入失败', key, e)
  71. }
  72. return arr
  73. }
  74. /**
  75. * 切换某指标的收藏状态(加入/取消收藏)
  76. * @param {Object} scope
  77. * @param {number|string} definitionId
  78. * @returns {{ids:number[], added:boolean}} added=true 表示新加入收藏
  79. */
  80. export function toggleFavorite(scope, definitionId) {
  81. var id = Number(definitionId)
  82. var ids = readFavorites(scope)
  83. if (isNaN(id)) return { ids: ids, added: false }
  84. var idx = ids.indexOf(id)
  85. if (idx >= 0) {
  86. ids.splice(idx, 1)
  87. writeFavorites(scope, ids)
  88. return { ids: ids, added: false }
  89. }
  90. ids.push(id)
  91. writeFavorites(scope, ids)
  92. return { ids: ids, added: true }
  93. }