hookehuyr

refactor(ActivityHistoryPage): 提取活动数据处理逻辑到独立函数

将活动数据映射、日期设置和响应处理逻辑提取为独立函数
增加缓存用户参数获取函数,提高代码可读性和复用性
...@@ -190,73 +190,92 @@ const activityInfo = ref({}) ...@@ -190,73 +190,92 @@ const activityInfo = ref({})
190 // 记录日期 190 // 记录日期
191 const recordDate = ref('') 191 const recordDate = ref('')
192 192
193 -onMounted(async () => { 193 +/**
194 - // 获取补充信息 194 + * 映射活动数据
195 - fetchSupplementInfo() 195 + * @param campaign_list 活动列表
196 - 196 + * @returns 映射后的活动列表
197 - // 从缓存获取用户信息 197 + */
198 - const cachedUserInfo = localStorage.getItem('cached_user_info') 198 +const mapCampaignToActivities = (campaign_list) => {
199 - if (cachedUserInfo) { 199 + return (campaign_list || []).map(item => ({
200 - const userInfo = JSON.parse(cachedUserInfo)
201 - if (userInfo.name && userInfo.phone && userInfo.idCard) {
202 - // 通过用户信息获取活动历史信息
203 - const activityRes = await searchOldActivityAPI({
204 - name: userInfo.name,
205 - mobile: userInfo.phone,
206 - idcard: userInfo.idCard
207 - })
208 - if (activityRes.code) {
209 - activityInfo.value = activityRes.data || {}
210 - // 获取历史列表数据
211 - campaign_info.value = activityRes.data?.campaign_info || []
212 - if (campaign_info.value.length) {
213 - activities.value = campaign_info.value.map(item => ({
214 id: item.campaign_id || 0, 200 id: item.campaign_id || 0,
215 stu_uid: item.stu_uid || '', 201 stu_uid: item.stu_uid || '',
216 title: item.campaign_name || '', 202 title: item.campaign_name || '',
217 price: item.fee_stu || 0, 203 price: item.fee_stu || 0,
218 count: item.stu_cnt || 0, 204 count: item.stu_cnt || 0,
219 - date: item.campaign_year|| '', 205 + date: item.campaign_year || '',
220 total: item.fee_stu * item.stu_cnt || 0 206 total: item.fee_stu * item.stu_cnt || 0
221 })) 207 }))
222 - // 遍历日期把列表date字段从小到大排序, 获取到其中日期段 比如 2020-2025 208 +}
223 - const sortedDates = activities.value.map(item => item.date).sort((a, b) => new Date(a) - new Date(b)) 209 +
224 - // if (sortedDates[0] !== sortedDates[sortedDates.length - 1]) { 210 +/**
225 - // recordDate.value = sortedDates[0] + '-' + sortedDates[sortedDates.length - 1] || '' 211 + * 设置记录日期
226 - // } else { 212 + * @param activity_list 活动列表
227 - // recordDate.value = sortedDates[0] || '' 213 + */
228 - // } 214 +const setRecordDateByActivities = (activity_list) => {
229 - // 李琛说就写死到2025年结束 215 + const sorted_dates = (activity_list || []).map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
230 - recordDate.value = sortedDates[0] + '-' + '2025' 216 + recordDate.value = sorted_dates.length ? `${sorted_dates[0]}-2025` : ''
217 +}
218 +
219 +/**
220 + * 处理活动响应
221 + * @param activity_res 活动响应
222 + * @param should_set_activity_info 是否设置活动信息
223 + */
224 +const applyActivityResponse = (activity_res, should_set_activity_info) => {
225 + if (!activity_res || !activity_res.code) return
226 +
227 + if (should_set_activity_info) {
228 + activityInfo.value = activity_res.data || {}
231 } 229 }
230 +
231 + campaign_info.value = activity_res.data?.campaign_info || []
232 + if (!campaign_info.value.length) return
233 +
234 + activities.value = mapCampaignToActivities(campaign_info.value)
235 + setRecordDateByActivities(activities.value)
236 +}
237 +
238 +/**
239 + * 获取缓存用户参数
240 + * @returns 缓存用户参数
241 + */
242 +const getCachedUserParams = () => {
243 + const cached_user_info = localStorage.getItem('cached_user_info')
244 + if (!cached_user_info) return { has_cache: false, params: null }
245 +
246 + try {
247 + const user_info = JSON.parse(cached_user_info)
248 + if (user_info?.name && user_info?.phone && user_info?.idCard) {
249 + return {
250 + has_cache: true,
251 + params: {
252 + name: user_info.name,
253 + mobile: user_info.phone,
254 + idcard: user_info.idCard
232 } 255 }
233 } 256 }
234 - } else {
235 - // 如果是收集完成没有缓存字段的情况, 直接查接口
236 - const activityRes = await searchOldActivityAPI()
237 - if (activityRes.code) {
238 - // 获取历史列表数据
239 - campaign_info.value = activityRes.data?.campaign_info || []
240 - if (campaign_info.value.length) {
241 - activities.value = campaign_info.value.map(item => ({
242 - id: item.campaign_id || 0,
243 - stu_uid: item.stu_uid || '',
244 - title: item.campaign_name || '',
245 - price: item.fee_stu || 0,
246 - count: item.stu_cnt || 0,
247 - date: item.campaign_year || '',
248 - total: item.fee_stu * item.stu_cnt || 0
249 - }));
250 - const sortedDates = activities.value.map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
251 - // if (sortedDates[0] !== sortedDates[sortedDates.length - 1]) {
252 - // recordDate.value = sortedDates[0] + '-' + sortedDates[sortedDates.length - 1] || ''
253 - // } else {
254 - // recordDate.value = sortedDates[0] || ''
255 - // }
256 - // 李琛说就写死到2025年结束
257 - recordDate.value = sortedDates[0] + '-' + '2025'
258 } 257 }
258 + } catch (e) {
259 + return { has_cache: true, params: null }
260 + }
261 +
262 + return { has_cache: true, params: null }
263 +}
264 +
265 +onMounted(async () => {
266 + // 获取补充信息
267 + fetchSupplementInfo()
268 +
269 + // 从缓存获取用户信息
270 + const { has_cache, params } = getCachedUserParams()
271 + if (has_cache) {
272 + if (params) {
273 + const activity_res = await searchOldActivityAPI(params)
274 + applyActivityResponse(activity_res, true)
259 } 275 }
276 + } else {
277 + const activity_res = await searchOldActivityAPI()
278 + applyActivityResponse(activity_res, false)
260 } 279 }
261 280
262 // 获取用户信息 281 // 获取用户信息
......