/** * 运动类型公共模块(全局统一一套 · 后台可配置) * * 背景:growth/exercise-checkin.vue 与 health/exercise-index.vue 各自写死一套 * exerciseTypes,value 分歧(ball vs ball_game)、成员不齐(一个缺 strength、 * 一个缺 gymnastics),后台无法配置。本模块统一为一套标准并集,并提供: * - DEFAULT_EXERCISE_TYPES:标准默认值(并集),离线/首屏/失败回退使用 * - normalizeExerciseTypes(raw):把后台 sys_config 的 JSON 字符串/数组归一为有效列表 * - normalizeExerciseValue(v):历史 value 映射(ball_game → ball),保证老记录 label 不丢 * - resolveExerciseTypeLabel(v, types):value → label(兼容历史 value) * - extractFromPublicConfigResp(resp):解包 /api/config/public/value 的响应 * - loadExerciseTypes(fetcher, store):拉取 + 缓存 + 回退(离线可用) * * 配置源:sys_config.config_key = 'exercise_types',值为 [{label,value}] JSON 数组。 * 读取走公开接口 getPublicConfig('exercise_types');写由后台 ExerciseTypeConfig 页负责。 */ // 全局统一一套:并集 + 去重后的标准 value(ball_game 废弃 → ball) export const DEFAULT_EXERCISE_TYPES = [ { label: '跑步', value: 'running' }, { label: '游泳', value: 'swimming' }, { label: '骑行', value: 'cycling' }, { label: '跳绳', value: 'jump_rope' }, { label: '球类', value: 'ball' }, { label: '力量训练', value: 'strength' }, { label: '体操', value: 'gymnastics' }, { label: '其他', value: 'other' } ] // 历史 value 兼容映射:旧 health 页用 ball_game,统一后归一为 ball const LEGACY_VALUE_MAP = { ball_game: 'ball' } export const CACHE_KEY = 'exercise_types' const FALLBACK_LABEL = '其他' export function normalizeExerciseValue(value) { if (value == null) return value const v = String(value).trim() return Object.prototype.hasOwnProperty.call(LEGACY_VALUE_MAP, v) ? LEGACY_VALUE_MAP[v] : v } export function normalizeExerciseTypes(raw) { let arr = raw if (typeof raw === 'string') { try { arr = JSON.parse(raw) } catch (e) { return DEFAULT_EXERCISE_TYPES.slice() } } if (!Array.isArray(arr)) return DEFAULT_EXERCISE_TYPES.slice() const seen = {} const result = [] arr.forEach((item) => { if (!item || typeof item !== 'object') return const value = item.value == null ? '' : String(item.value).trim() const label = item.label == null ? '' : String(item.label).trim() if (!value || !label) return const normalized = normalizeExerciseValue(value) if (seen[normalized]) return seen[normalized] = true result.push({ label, value: normalized }) }) return result.length ? result : DEFAULT_EXERCISE_TYPES.slice() } export function resolveExerciseTypeLabel(value, types) { const list = Array.isArray(types) && types.length ? types : DEFAULT_EXERCISE_TYPES const normalized = normalizeExerciseValue(value) const found = list.find((x) => x.value === normalized) if (found) return found.label // 老数据里若后台配置把 label 也配成历史词,再按原 value 找一次 const rawFound = list.find((x) => x.value === value) return rawFound ? rawFound.label : FALLBACK_LABEL } export function extractFromPublicConfigResp(resp) { if (!resp || resp.code !== 200 || resp.data == null || resp.data === '') { return DEFAULT_EXERCISE_TYPES.slice() } return normalizeExerciseTypes(resp.data) } /** * 拉取 + 缓存 + 回退。 * @param fetcher async (key) => resp(形如 {code:200,data:""});不传则用 getPublicConfig * @param store 简单 KV(小程序里可传 uni 缓存适配器);失败时读缓存,再失败回退默认 */ export async function loadExerciseTypes(fetcher, store) { const readCache = () => { if (!store) return null if (typeof store.getItem === 'function') return store.getItem(CACHE_KEY) return store[CACHE_KEY] } const writeCache = (val) => { if (!store) return if (typeof store.setItem === 'function') store.setItem(CACHE_KEY, val) else store[CACHE_KEY] = val } let data = null if (fetcher) { try { const resp = await fetcher(CACHE_KEY) const parsed = extractFromPublicConfigResp(resp) // parsed 为默认(说明响应无效或空)——不当作后台配置缓存,避免污染 if (JSON.stringify(parsed) !== JSON.stringify(DEFAULT_EXERCISE_TYPES)) { data = parsed } } catch (e) { data = null } } if (!data) { const cached = readCache() if (cached) { data = normalizeExerciseTypes(cached) // 缓存里是默认值时不算有效配置 if (JSON.stringify(data) === JSON.stringify(DEFAULT_EXERCISE_TYPES)) data = null } } if (!data) data = DEFAULT_EXERCISE_TYPES.slice() writeCache(JSON.stringify(data)) return data }