exerciseTypes.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 运动类型公共模块(全局统一一套 · 后台可配置)
  3. *
  4. * 背景:growth/exercise-checkin.vue 与 health/exercise-index.vue 各自写死一套
  5. * exerciseTypes,value 分歧(ball vs ball_game)、成员不齐(一个缺 strength、
  6. * 一个缺 gymnastics),后台无法配置。本模块统一为一套标准并集,并提供:
  7. * - DEFAULT_EXERCISE_TYPES:标准默认值(并集),离线/首屏/失败回退使用
  8. * - normalizeExerciseTypes(raw):把后台 sys_config 的 JSON 字符串/数组归一为有效列表
  9. * - normalizeExerciseValue(v):历史 value 映射(ball_game → ball),保证老记录 label 不丢
  10. * - resolveExerciseTypeLabel(v, types):value → label(兼容历史 value)
  11. * - extractFromPublicConfigResp(resp):解包 /api/config/public/value 的响应
  12. * - loadExerciseTypes(fetcher, store):拉取 + 缓存 + 回退(离线可用)
  13. *
  14. * 配置源:sys_config.config_key = 'exercise_types',值为 [{label,value}] JSON 数组。
  15. * 读取走公开接口 getPublicConfig('exercise_types');写由后台 ExerciseTypeConfig 页负责。
  16. */
  17. // 全局统一一套:并集 + 去重后的标准 value(ball_game 废弃 → ball)
  18. export const DEFAULT_EXERCISE_TYPES = [
  19. { label: '跑步', value: 'running' },
  20. { label: '游泳', value: 'swimming' },
  21. { label: '骑行', value: 'cycling' },
  22. { label: '跳绳', value: 'jump_rope' },
  23. { label: '球类', value: 'ball' },
  24. { label: '力量训练', value: 'strength' },
  25. { label: '体操', value: 'gymnastics' },
  26. { label: '其他', value: 'other' }
  27. ]
  28. // 历史 value 兼容映射:旧 health 页用 ball_game,统一后归一为 ball
  29. const LEGACY_VALUE_MAP = {
  30. ball_game: 'ball'
  31. }
  32. export const CACHE_KEY = 'exercise_types'
  33. const FALLBACK_LABEL = '其他'
  34. export function normalizeExerciseValue(value) {
  35. if (value == null) return value
  36. const v = String(value).trim()
  37. return Object.prototype.hasOwnProperty.call(LEGACY_VALUE_MAP, v) ? LEGACY_VALUE_MAP[v] : v
  38. }
  39. export function normalizeExerciseTypes(raw) {
  40. let arr = raw
  41. if (typeof raw === 'string') {
  42. try {
  43. arr = JSON.parse(raw)
  44. } catch (e) {
  45. return DEFAULT_EXERCISE_TYPES.slice()
  46. }
  47. }
  48. if (!Array.isArray(arr)) return DEFAULT_EXERCISE_TYPES.slice()
  49. const seen = {}
  50. const result = []
  51. arr.forEach((item) => {
  52. if (!item || typeof item !== 'object') return
  53. const value = item.value == null ? '' : String(item.value).trim()
  54. const label = item.label == null ? '' : String(item.label).trim()
  55. if (!value || !label) return
  56. const normalized = normalizeExerciseValue(value)
  57. if (seen[normalized]) return
  58. seen[normalized] = true
  59. result.push({ label, value: normalized })
  60. })
  61. return result.length ? result : DEFAULT_EXERCISE_TYPES.slice()
  62. }
  63. export function resolveExerciseTypeLabel(value, types) {
  64. const list = Array.isArray(types) && types.length ? types : DEFAULT_EXERCISE_TYPES
  65. const normalized = normalizeExerciseValue(value)
  66. const found = list.find((x) => x.value === normalized)
  67. if (found) return found.label
  68. // 老数据里若后台配置把 label 也配成历史词,再按原 value 找一次
  69. const rawFound = list.find((x) => x.value === value)
  70. return rawFound ? rawFound.label : FALLBACK_LABEL
  71. }
  72. export function extractFromPublicConfigResp(resp) {
  73. if (!resp || resp.code !== 200 || resp.data == null || resp.data === '') {
  74. return DEFAULT_EXERCISE_TYPES.slice()
  75. }
  76. return normalizeExerciseTypes(resp.data)
  77. }
  78. /**
  79. * 拉取 + 缓存 + 回退。
  80. * @param fetcher async (key) => resp(形如 {code:200,data:"<json>"});不传则用 getPublicConfig
  81. * @param store 简单 KV(小程序里可传 uni 缓存适配器);失败时读缓存,再失败回退默认
  82. */
  83. export async function loadExerciseTypes(fetcher, store) {
  84. const readCache = () => {
  85. if (!store) return null
  86. if (typeof store.getItem === 'function') return store.getItem(CACHE_KEY)
  87. return store[CACHE_KEY]
  88. }
  89. const writeCache = (val) => {
  90. if (!store) return
  91. if (typeof store.setItem === 'function') store.setItem(CACHE_KEY, val)
  92. else store[CACHE_KEY] = val
  93. }
  94. let data = null
  95. if (fetcher) {
  96. try {
  97. const resp = await fetcher(CACHE_KEY)
  98. const parsed = extractFromPublicConfigResp(resp)
  99. // parsed 为默认(说明响应无效或空)——不当作后台配置缓存,避免污染
  100. if (JSON.stringify(parsed) !== JSON.stringify(DEFAULT_EXERCISE_TYPES)) {
  101. data = parsed
  102. }
  103. } catch (e) {
  104. data = null
  105. }
  106. }
  107. if (!data) {
  108. const cached = readCache()
  109. if (cached) {
  110. data = normalizeExerciseTypes(cached)
  111. // 缓存里是默认值时不算有效配置
  112. if (JSON.stringify(data) === JSON.stringify(DEFAULT_EXERCISE_TYPES)) data = null
  113. }
  114. }
  115. if (!data) data = DEFAULT_EXERCISE_TYPES.slice()
  116. writeCache(JSON.stringify(data))
  117. return data
  118. }