| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /* ==============================================================
- * utils/indicator-favorite.js - 关注指标收藏(本地持久化)
- * ⚠️ 当前为本地持久化(小程序 Storage),后续若需跨端同步再接后端。
- * 本文件注释即契约:不要在此接后端,等有跨端同步需求时再改。
- * 隔离维度:登录用户 userId + 家庭 familyId + 成员 memberId + 报告类型 sourceType。
- * 同一存储 key 只对应一个「用户×家庭×成员×报告类型」视角,避免串号
- * (不同账号/不同成员/不同报告类型之间的收藏互不干扰)。
- * 持久化结构:key → number[](definitionId 列表)
- * ============================================================== */
- const KEY_PREFIX = 'cfc:indicator:fav'
- /**
- * 拼装存储 key(维度隔离;缺失维度用占位符,避免不同账号/成员互相覆盖)
- * @param {{userId?:*, familyId?:*, memberId?:*, sourceType?:*}} scope
- * @returns {string}
- */
- export function buildFavoriteKey(scope) {
- var s = scope || {}
- var userId = (s.userId === undefined || s.userId === null || s.userId === '') ? 'anon' : String(s.userId)
- var familyId = (s.familyId === undefined || s.familyId === null || s.familyId === '') ? 'nofamily' : String(s.familyId)
- var memberId = (s.memberId === undefined || s.memberId === null || s.memberId === '') ? 'nomember' : String(s.memberId)
- var sourceType = (s.sourceType === undefined || s.sourceType === null || s.sourceType === '') ? 'all' : String(s.sourceType)
- return KEY_PREFIX + ':' + userId + ':' + familyId + ':' + memberId + ':' + sourceType
- }
- /** 数字数组去重、丢弃非法值 */
- function normalizeIds(val) {
- if (val === undefined || val === null || val === '') return []
- if (typeof val === 'string') {
- try { val = JSON.parse(val) } catch (e) { return [] }
- }
- if (!Array.isArray(val)) return []
- var out = []
- var seen = {}
- for (var i = 0; i < val.length; i++) {
- var n = Number(val[i])
- if (isNaN(n)) continue
- if (seen[n]) continue
- seen[n] = true
- out.push(n)
- }
- return out
- }
- /**
- * 读取某视角的收藏指标 definitionId 列表
- * @param {Object} scope
- * @returns {number[]}
- */
- export function readFavorites(scope) {
- var key = buildFavoriteKey(scope)
- var val
- try {
- val = uni.getStorageSync(key)
- } catch (e) {
- return []
- }
- return normalizeIds(val)
- }
- /**
- * 写入某视角的收藏列表
- * @param {Object} scope
- * @param {number[]} ids
- * @returns {number[]} 写入后的规范化列表
- */
- export function writeFavorites(scope, ids) {
- var key = buildFavoriteKey(scope)
- var arr = normalizeIds(ids)
- try {
- uni.setStorageSync(key, arr)
- } catch (e) {
- // 存储失败静默(收藏是增强功能,不影响主流程)
- console.warn('[indicator-favorite] 写入失败', key, e)
- }
- return arr
- }
- /**
- * 切换某指标的收藏状态(加入/取消收藏)
- * @param {Object} scope
- * @param {number|string} definitionId
- * @returns {{ids:number[], added:boolean}} added=true 表示新加入收藏
- */
- export function toggleFavorite(scope, definitionId) {
- var id = Number(definitionId)
- var ids = readFavorites(scope)
- if (isNaN(id)) return { ids: ids, added: false }
- var idx = ids.indexOf(id)
- if (idx >= 0) {
- ids.splice(idx, 1)
- writeFavorites(scope, ids)
- return { ids: ids, added: false }
- }
- ids.push(id)
- writeFavorites(scope, ids)
- return { ids: ids, added: true }
- }
|