feat(打卡): 优化打卡ID获取逻辑并添加列表刷新功能
添加 get_new_checkin_id 函数用于更可靠地从响应数据中提取打卡ID 新增 refresh_checkin_list 函数作为回退刷新机制 当特定ID刷新失败时自动回退到完整列表刷新
Showing
2 changed files
with
65 additions
and
4 deletions
| ... | @@ -263,6 +263,47 @@ export function useCheckin() { | ... | @@ -263,6 +263,47 @@ export function useCheckin() { |
| 263 | * 提交打卡 | 263 | * 提交打卡 |
| 264 | * @param {Object} extraData - 额外提交数据 | 264 | * @param {Object} extraData - 额外提交数据 |
| 265 | */ | 265 | */ |
| 266 | + const get_new_checkin_id = (data) => { | ||
| 267 | + if (!data) return null | ||
| 268 | + | ||
| 269 | + if (typeof data === 'string' || typeof data === 'number') return data | ||
| 270 | + if (typeof data !== 'object') return null | ||
| 271 | + | ||
| 272 | + const direct_keys = ['id', 'checkin_id', 'post_id', 'i'] | ||
| 273 | + for (const key of direct_keys) { | ||
| 274 | + const value = data?.[key] | ||
| 275 | + if (typeof value === 'string' || typeof value === 'number') return value | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + const visited = new Set() | ||
| 279 | + const deep_find = (obj, max_depth) => { | ||
| 280 | + if (!obj || typeof obj !== 'object') return null | ||
| 281 | + if (visited.has(obj)) return null | ||
| 282 | + visited.add(obj) | ||
| 283 | + | ||
| 284 | + for (const [key, value] of Object.entries(obj)) { | ||
| 285 | + if ((typeof value === 'string' || typeof value === 'number') && /(^|_)(id)$/.test(String(key))) { | ||
| 286 | + return value | ||
| 287 | + } | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + if (max_depth <= 0) return null | ||
| 291 | + for (const value of Object.values(obj)) { | ||
| 292 | + if (value && typeof value === 'object') { | ||
| 293 | + const found = deep_find(value, max_depth - 1) | ||
| 294 | + if (found) return found | ||
| 295 | + } | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + return null | ||
| 299 | + } | ||
| 300 | + | ||
| 301 | + const found_id = deep_find(data, 2) | ||
| 302 | + if (found_id) return found_id | ||
| 303 | + | ||
| 304 | + return null | ||
| 305 | + } | ||
| 306 | + | ||
| 266 | const onSubmit = async (extraData = {}) => { | 307 | const onSubmit = async (extraData = {}) => { |
| 267 | if (uploading.value) return | 308 | if (uploading.value) return |
| 268 | 309 | ||
| ... | @@ -338,9 +379,8 @@ export function useCheckin() { | ... | @@ -338,9 +379,8 @@ export function useCheckin() { |
| 338 | if (refreshType === 'edit') { | 379 | if (refreshType === 'edit') { |
| 339 | sessionStorage.setItem('checkin_refresh_id', route.query.post_id); | 380 | sessionStorage.setItem('checkin_refresh_id', route.query.post_id); |
| 340 | } else if (result.data) { | 381 | } else if (result.data) { |
| 341 | - // 尝试获取新ID | 382 | + const new_id = get_new_checkin_id(result.data) |
| 342 | - const newId = result.data.id || (typeof result.data !== 'object' ? result.data : null); | 383 | + if (new_id) sessionStorage.setItem('checkin_refresh_id', new_id) |
| 343 | - if (newId) sessionStorage.setItem('checkin_refresh_id', newId); | ||
| 344 | } | 384 | } |
| 345 | 385 | ||
| 346 | router.back() | 386 | router.back() | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-05-29 15:34:17 | 2 | * @Date: 2025-05-29 15:34:17 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-12-19 14:55:13 | 4 | + * @LastEditTime: 2026-01-18 21:57:29 |
| 5 | * @FilePath: /mlaj/src/views/checkin/IndexCheckInPage.vue | 5 | * @FilePath: /mlaj/src/views/checkin/IndexCheckInPage.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -743,6 +743,16 @@ onDeactivated(() => { | ... | @@ -743,6 +743,16 @@ onDeactivated(() => { |
| 743 | savedScrollTop.value = window.scrollY | 743 | savedScrollTop.value = window.scrollY |
| 744 | }) | 744 | }) |
| 745 | 745 | ||
| 746 | +const refresh_checkin_list = async () => { | ||
| 747 | + const current_date = route.query.date || dayjs().format('YYYY-MM-DD') | ||
| 748 | + checkinDataList.value = [] | ||
| 749 | + page.value = 0 | ||
| 750 | + finished.value = false | ||
| 751 | + loading.value = true | ||
| 752 | + await getTaskDetail(dayjs(current_date).format('YYYY-MM')) | ||
| 753 | + await onLoad(current_date) | ||
| 754 | +} | ||
| 755 | + | ||
| 746 | onActivated(async () => { | 756 | onActivated(async () => { |
| 747 | // 检查任务ID是否变化 | 757 | // 检查任务ID是否变化 |
| 748 | // 注意:route.query.id 可能是数字或字符串,统一转为字符串比较 | 758 | // 注意:route.query.id 可能是数字或字符串,统一转为字符串比较 |
| ... | @@ -774,6 +784,7 @@ onActivated(async () => { | ... | @@ -774,6 +784,7 @@ onActivated(async () => { |
| 774 | sessionStorage.removeItem('checkin_refresh_flag') | 784 | sessionStorage.removeItem('checkin_refresh_flag') |
| 775 | sessionStorage.removeItem('checkin_refresh_id') | 785 | sessionStorage.removeItem('checkin_refresh_id') |
| 776 | 786 | ||
| 787 | + let has_refreshed = false | ||
| 777 | if (refreshId) { | 788 | if (refreshId) { |
| 778 | try { | 789 | try { |
| 779 | // 获取最新的打卡信息 | 790 | // 获取最新的打卡信息 |
| ... | @@ -792,6 +803,7 @@ onActivated(async () => { | ... | @@ -792,6 +803,7 @@ onActivated(async () => { |
| 792 | if (index > -1) { | 803 | if (index > -1) { |
| 793 | checkinDataList.value.splice(index, 1, formattedItem) | 804 | checkinDataList.value.splice(index, 1, formattedItem) |
| 794 | } | 805 | } |
| 806 | + has_refreshed = true | ||
| 795 | } else if (refreshType === 'add') { | 807 | } else if (refreshType === 'add') { |
| 796 | // 新增模式:添加到列表顶部,且去重 | 808 | // 新增模式:添加到列表顶部,且去重 |
| 797 | const exists = checkinDataList.value.some(item => item.id == formattedItem.id) | 809 | const exists = checkinDataList.value.some(item => item.id == formattedItem.id) |
| ... | @@ -801,6 +813,7 @@ onActivated(async () => { | ... | @@ -801,6 +813,7 @@ onActivated(async () => { |
| 801 | // 更新统计数据 | 813 | // 更新统计数据 |
| 802 | const current_date = route.query.date || dayjs().format('YYYY-MM-DD'); | 814 | const current_date = route.query.date || dayjs().format('YYYY-MM-DD'); |
| 803 | getTaskDetail(dayjs(current_date).format('YYYY-MM')); | 815 | getTaskDetail(dayjs(current_date).format('YYYY-MM')); |
| 816 | + has_refreshed = true | ||
| 804 | } | 817 | } |
| 805 | } | 818 | } |
| 806 | } | 819 | } |
| ... | @@ -808,6 +821,14 @@ onActivated(async () => { | ... | @@ -808,6 +821,14 @@ onActivated(async () => { |
| 808 | console.error('刷新打卡数据失败:', error) | 821 | console.error('刷新打卡数据失败:', error) |
| 809 | } | 822 | } |
| 810 | } | 823 | } |
| 824 | + | ||
| 825 | + if (!has_refreshed) { | ||
| 826 | + try { | ||
| 827 | + await refresh_checkin_list() | ||
| 828 | + } catch (error) { | ||
| 829 | + console.error('回退刷新打卡列表失败:', error) | ||
| 830 | + } | ||
| 831 | + } | ||
| 811 | } | 832 | } |
| 812 | }) | 833 | }) |
| 813 | </script> | 834 | </script> | ... | ... |
-
Please register or login to post a comment