tab-config.js
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* 打卡详情页标签页配置
*
* @description 定义标签页的结构和默认配置,提供标签页标题设置的工具函数
* @module checkin/tab-config
* @author Claude Code
* @created 2026-02-09
*/
/**
* 标签页配置数组
*
* @description 定义三个标签页的配置:介绍、故事、体验
* @type {Array<Object>}
*/
export const TAB_CONFIGS = [
{
/**
* 标签页唯一标识符
* @type {string}
*/
key: 'introduction',
/**
* 从接口数据中读取标题的字段名
* @type {string}
*/
title_key: 'introduction_text',
/**
* 从接口数据中读取内容的字段名
* @type {string}
*/
content_key: 'introduction',
/**
* 默认标题(当接口未返回标题时使用)
* @type {string}
*/
default_title: '敬老月优惠',
/**
* DOM 元素 ID
* @type {string}
*/
id: 'introduction'
},
{
key: 'story',
title_key: 'story_text',
content_key: 'story',
default_title: '敬老月优惠',
id: 'story'
},
{
key: 'experience',
title_key: 'experience_text',
content_key: 'experience',
default_title: '敬老月优惠',
id: 'experience'
}
]
/**
* 设置标签页标题
*
* @description 将接口返回的标题设置到 page_details,如果没有则使用默认值。
* 该函数会创建一个新的对象,不会修改原始的 page_details 对象。
*
* @param {Object} page_details - 页面详情对象,包含接口返回的数据
* @param {Array<Object>} tab_configs - 标签页配置数组,默认使用 TAB_CONFIGS
* @returns {Object} 更新后的 page_details 对象
*
* @example
* const page_details = { introduction_text: '景点介绍' }
* const updated = setTabTitles(page_details)
* // updated.introduction_text === '景点介绍'
* // updated.story_text === '故事' (使用默认值)
*/
export function setTabTitles(page_details, tab_configs = TAB_CONFIGS) {
const updated = { ...page_details }
tab_configs.forEach(config => {
// 优先使用接口返回的标题,否则使用默认值
updated[config.title_key] = updated[config.title_key] || config.default_title
})
return updated
}
/**
* 从接口数据更新标签页配置
*
* @description 如果接口返回了标签页标题,则更新配置中的 default_title。
* 这样在接口未返回某个标题时,可以使用接口返回的其他标题作为后备值。
*
* @param {Object} apiData - 接口返回的详情数据
* @param {Array<Object>} tab_configs - 标签页配置数组,默认使用 TAB_CONFIGS
* @returns {Array<Object>} 更新后的标签页配置数组
*
* @example
* const apiData = { introduction_text: '新介绍', story_text: '新故事' }
* const updatedConfigs = updateTabConfigsFromAPI(apiData)
* // updatedConfigs[0].default_title === '新介绍'
* // updatedConfigs[1].default_title === '新故事'
* // updatedConfigs[2].default_title === '体验' (保持原值)
*/
export function updateTabConfigsFromAPI(apiData, tab_configs = TAB_CONFIGS) {
return tab_configs.map(config => {
const updated = { ...config }
// 如果接口返回了该标签页的标题,更新默认标题
if (apiData && apiData[config.title_key]) {
updated.default_title = apiData[config.title_key]
}
return updated
})
}
/**
* 获取可见的标签页配置
*
* @description 根据页面内容返回有内容的标签页配置
*
* @param {Object} page_details - 页面详情对象
* @param {Array<Object>} tab_configs - 标签页配置数组
* @returns {Array<Object>} 可见的标签页配置数组
*
* @example
* const page_details = { introduction: '...', story: null, experience: '...' }
* const visibleConfigs = getVisibleTabConfigs(page_details)
* // visibleConfigs.length === 2 (只包含 introduction 和 experience)
*/
export function getVisibleTabConfigs(page_details, tab_configs = TAB_CONFIGS) {
return tab_configs.filter(config => page_details[config.content_key])
}