hookehuyr

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

将活动数据映射、日期设置和响应处理逻辑提取为独立函数
增加缓存用户参数获取函数,提高代码可读性和复用性
......@@ -190,73 +190,92 @@ const activityInfo = ref({})
// 记录日期
const recordDate = ref('')
/**
* 映射活动数据
* @param campaign_list 活动列表
* @returns 映射后的活动列表
*/
const mapCampaignToActivities = (campaign_list) => {
return (campaign_list || []).map(item => ({
id: item.campaign_id || 0,
stu_uid: item.stu_uid || '',
title: item.campaign_name || '',
price: item.fee_stu || 0,
count: item.stu_cnt || 0,
date: item.campaign_year || '',
total: item.fee_stu * item.stu_cnt || 0
}))
}
/**
* 设置记录日期
* @param activity_list 活动列表
*/
const setRecordDateByActivities = (activity_list) => {
const sorted_dates = (activity_list || []).map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
recordDate.value = sorted_dates.length ? `${sorted_dates[0]}-2025` : ''
}
/**
* 处理活动响应
* @param activity_res 活动响应
* @param should_set_activity_info 是否设置活动信息
*/
const applyActivityResponse = (activity_res, should_set_activity_info) => {
if (!activity_res || !activity_res.code) return
if (should_set_activity_info) {
activityInfo.value = activity_res.data || {}
}
campaign_info.value = activity_res.data?.campaign_info || []
if (!campaign_info.value.length) return
activities.value = mapCampaignToActivities(campaign_info.value)
setRecordDateByActivities(activities.value)
}
/**
* 获取缓存用户参数
* @returns 缓存用户参数
*/
const getCachedUserParams = () => {
const cached_user_info = localStorage.getItem('cached_user_info')
if (!cached_user_info) return { has_cache: false, params: null }
try {
const user_info = JSON.parse(cached_user_info)
if (user_info?.name && user_info?.phone && user_info?.idCard) {
return {
has_cache: true,
params: {
name: user_info.name,
mobile: user_info.phone,
idcard: user_info.idCard
}
}
}
} catch (e) {
return { has_cache: true, params: null }
}
return { has_cache: true, params: null }
}
onMounted(async () => {
// 获取补充信息
fetchSupplementInfo()
// 从缓存获取用户信息
const cachedUserInfo = localStorage.getItem('cached_user_info')
if (cachedUserInfo) {
const userInfo = JSON.parse(cachedUserInfo)
if (userInfo.name && userInfo.phone && userInfo.idCard) {
// 通过用户信息获取活动历史信息
const activityRes = await searchOldActivityAPI({
name: userInfo.name,
mobile: userInfo.phone,
idcard: userInfo.idCard
})
if (activityRes.code) {
activityInfo.value = activityRes.data || {}
// 获取历史列表数据
campaign_info.value = activityRes.data?.campaign_info || []
if (campaign_info.value.length) {
activities.value = campaign_info.value.map(item => ({
id: item.campaign_id || 0,
stu_uid: item.stu_uid || '',
title: item.campaign_name || '',
price: item.fee_stu || 0,
count: item.stu_cnt || 0,
date: item.campaign_year|| '',
total: item.fee_stu * item.stu_cnt || 0
}))
// 遍历日期把列表date字段从小到大排序, 获取到其中日期段 比如 2020-2025
const sortedDates = activities.value.map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
// if (sortedDates[0] !== sortedDates[sortedDates.length - 1]) {
// recordDate.value = sortedDates[0] + '-' + sortedDates[sortedDates.length - 1] || ''
// } else {
// recordDate.value = sortedDates[0] || ''
// }
// 李琛说就写死到2025年结束
recordDate.value = sortedDates[0] + '-' + '2025'
}
}
const { has_cache, params } = getCachedUserParams()
if (has_cache) {
if (params) {
const activity_res = await searchOldActivityAPI(params)
applyActivityResponse(activity_res, true)
}
} else {
// 如果是收集完成没有缓存字段的情况, 直接查接口
const activityRes = await searchOldActivityAPI()
if (activityRes.code) {
// 获取历史列表数据
campaign_info.value = activityRes.data?.campaign_info || []
if (campaign_info.value.length) {
activities.value = campaign_info.value.map(item => ({
id: item.campaign_id || 0,
stu_uid: item.stu_uid || '',
title: item.campaign_name || '',
price: item.fee_stu || 0,
count: item.stu_cnt || 0,
date: item.campaign_year || '',
total: item.fee_stu * item.stu_cnt || 0
}));
const sortedDates = activities.value.map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
// if (sortedDates[0] !== sortedDates[sortedDates.length - 1]) {
// recordDate.value = sortedDates[0] + '-' + sortedDates[sortedDates.length - 1] || ''
// } else {
// recordDate.value = sortedDates[0] || ''
// }
// 李琛说就写死到2025年结束
recordDate.value = sortedDates[0] + '-' + '2025'
}
}
const activity_res = await searchOldActivityAPI()
applyActivityResponse(activity_res, false)
}
// 获取用户信息
......