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 +/**
194 + * 映射活动数据
195 + * @param campaign_list 活动列表
196 + * @returns 映射后的活动列表
197 + */
198 +const mapCampaignToActivities = (campaign_list) => {
199 + return (campaign_list || []).map(item => ({
200 + id: item.campaign_id || 0,
201 + stu_uid: item.stu_uid || '',
202 + title: item.campaign_name || '',
203 + price: item.fee_stu || 0,
204 + count: item.stu_cnt || 0,
205 + date: item.campaign_year || '',
206 + total: item.fee_stu * item.stu_cnt || 0
207 + }))
208 +}
209 +
210 +/**
211 + * 设置记录日期
212 + * @param activity_list 活动列表
213 + */
214 +const setRecordDateByActivities = (activity_list) => {
215 + const sorted_dates = (activity_list || []).map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
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 || {}
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
255 + }
256 + }
257 + }
258 + } catch (e) {
259 + return { has_cache: true, params: null }
260 + }
261 +
262 + return { has_cache: true, params: null }
263 +}
264 +
193 onMounted(async () => { 265 onMounted(async () => {
194 // 获取补充信息 266 // 获取补充信息
195 fetchSupplementInfo() 267 fetchSupplementInfo()
196 268
197 // 从缓存获取用户信息 269 // 从缓存获取用户信息
198 - const cachedUserInfo = localStorage.getItem('cached_user_info') 270 + const { has_cache, params } = getCachedUserParams()
199 - if (cachedUserInfo) { 271 + if (has_cache) {
200 - const userInfo = JSON.parse(cachedUserInfo) 272 + if (params) {
201 - if (userInfo.name && userInfo.phone && userInfo.idCard) { 273 + const activity_res = await searchOldActivityAPI(params)
202 - // 通过用户信息获取活动历史信息 274 + applyActivityResponse(activity_res, true)
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,
215 - stu_uid: item.stu_uid || '',
216 - title: item.campaign_name || '',
217 - price: item.fee_stu || 0,
218 - count: item.stu_cnt || 0,
219 - date: item.campaign_year|| '',
220 - total: item.fee_stu * item.stu_cnt || 0
221 - }))
222 - // 遍历日期把列表date字段从小到大排序, 获取到其中日期段 比如 2020-2025
223 - const sortedDates = activities.value.map(item => item.date).sort((a, b) => new Date(a) - new Date(b))
224 - // if (sortedDates[0] !== sortedDates[sortedDates.length - 1]) {
225 - // recordDate.value = sortedDates[0] + '-' + sortedDates[sortedDates.length - 1] || ''
226 - // } else {
227 - // recordDate.value = sortedDates[0] || ''
228 - // }
229 - // 李琛说就写死到2025年结束
230 - recordDate.value = sortedDates[0] + '-' + '2025'
231 - }
232 - }
233 } 275 }
234 } else { 276 } else {
235 - // 如果是收集完成没有缓存字段的情况, 直接查接口 277 + const activity_res = await searchOldActivityAPI()
236 - const activityRes = await searchOldActivityAPI() 278 + applyActivityResponse(activity_res, false)
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 - }
259 - }
260 } 279 }
261 280
262 // 获取用户信息 281 // 获取用户信息
......