feat(打卡详情): 完善计数打卡功能的数据初始化和回调处理
添加感恩计数打卡相关数据字段和回调处理逻辑 优化目标选择匹配逻辑,优先使用id匹配 将异步操作改为await方式确保执行顺序
Showing
2 changed files
with
62 additions
and
8 deletions
| ... | @@ -372,11 +372,16 @@ export function useCheckin() { | ... | @@ -372,11 +372,16 @@ export function useCheckin() { |
| 372 | } | 372 | } |
| 373 | } | 373 | } |
| 374 | 374 | ||
| 375 | + // 计数打卡相关数据 | ||
| 376 | + const gratitudeCount = ref(0) | ||
| 377 | + const gratitudeFormList = ref([]) | ||
| 378 | + | ||
| 375 | /** | 379 | /** |
| 376 | * 初始化编辑数据 | 380 | * 初始化编辑数据 |
| 377 | * @param {Array} taskOptions - 任务选项列表 | 381 | * @param {Array} taskOptions - 任务选项列表 |
| 382 | + * @param {Object} handlers - 回调处理函数 | ||
| 378 | */ | 383 | */ |
| 379 | - const initEditData = async (taskOptions = []) => { | 384 | + const initEditData = async (taskOptions = [], handlers = {}) => { |
| 380 | if (route.query.status === 'edit') { | 385 | if (route.query.status === 'edit') { |
| 381 | try { | 386 | try { |
| 382 | const { code, data } = await getUploadTaskInfoAPI({ i: route.query.post_id }) | 387 | const { code, data } = await getUploadTaskInfoAPI({ i: route.query.post_id }) |
| ... | @@ -386,12 +391,43 @@ export function useCheckin() { | ... | @@ -386,12 +391,43 @@ export function useCheckin() { |
| 386 | // 小作业ID | 391 | // 小作业ID |
| 387 | subTaskId.value = data?.subtask_id | 392 | subTaskId.value = data?.subtask_id |
| 388 | 393 | ||
| 394 | + // 恢复计数打卡数据 | ||
| 395 | + if (data?.gratitude_count) { | ||
| 396 | + gratitudeCount.value = data.gratitude_count | ||
| 397 | + } | ||
| 398 | + if (data?.gratitude_form_list && Array.isArray(data.gratitude_form_list)) { | ||
| 399 | + gratitudeFormList.value = data.gratitude_form_list | ||
| 400 | + } | ||
| 401 | + | ||
| 389 | // 更新选中的任务显示 | 402 | // 更新选中的任务显示 |
| 390 | if (subTaskId.value) { | 403 | if (subTaskId.value) { |
| 391 | selectedTaskValue.value = [subTaskId.value] | 404 | selectedTaskValue.value = [subTaskId.value] |
| 392 | if (taskOptions && taskOptions.length > 0) { | 405 | if (taskOptions && taskOptions.length > 0) { |
| 393 | const option = taskOptions.find(o => o.value === subTaskId.value) | 406 | const option = taskOptions.find(o => o.value === subTaskId.value) |
| 394 | selectedTaskText.value = option ? option.text : '' | 407 | selectedTaskText.value = option ? option.text : '' |
| 408 | + | ||
| 409 | + // 处理计数打卡的回调 | ||
| 410 | + if (route.query.task_type === 'count' && option) { | ||
| 411 | + // 1. 更新动态表单字段 | ||
| 412 | + if (handlers.onTaskFound) { | ||
| 413 | + handlers.onTaskFound(option) | ||
| 414 | + } | ||
| 415 | + | ||
| 416 | + // 2. 确保目标列表已加载 | ||
| 417 | + if (handlers.ensureTargetList) { | ||
| 418 | + await handlers.ensureTargetList(subTaskId.value) | ||
| 419 | + } | ||
| 420 | + | ||
| 421 | + // 3. 恢复选中的对象 | ||
| 422 | + if (gratitudeFormList.value.length > 0 && handlers.setTargets) { | ||
| 423 | + handlers.setTargets(gratitudeFormList.value) | ||
| 424 | + } | ||
| 425 | + | ||
| 426 | + // 4. 恢复次数 | ||
| 427 | + if (gratitudeCount.value && handlers.setCount) { | ||
| 428 | + handlers.setCount(gratitudeCount.value) | ||
| 429 | + } | ||
| 430 | + } | ||
| 395 | } | 431 | } |
| 396 | } | 432 | } |
| 397 | 433 | ||
| ... | @@ -442,6 +478,8 @@ export function useCheckin() { | ... | @@ -442,6 +478,8 @@ export function useCheckin() { |
| 442 | isMakeup, | 478 | isMakeup, |
| 443 | maxCount, | 479 | maxCount, |
| 444 | canSubmit, | 480 | canSubmit, |
| 481 | + gratitudeCount, | ||
| 482 | + gratitudeFormList, | ||
| 445 | 483 | ||
| 446 | // 方法 | 484 | // 方法 |
| 447 | beforeRead, | 485 | beforeRead, | ... | ... |
| ... | @@ -208,7 +208,9 @@ const { | ... | @@ -208,7 +208,9 @@ const { |
| 208 | delItem, | 208 | delItem, |
| 209 | onSubmit, | 209 | onSubmit, |
| 210 | switchType, | 210 | switchType, |
| 211 | - initEditData | 211 | + initEditData, |
| 212 | + gratitudeCount, | ||
| 213 | + gratitudeFormList | ||
| 212 | } = useCheckin() | 214 | } = useCheckin() |
| 213 | 215 | ||
| 214 | // 动态字段文字 | 216 | // 动态字段文字 |
| ... | @@ -275,7 +277,7 @@ const updateDynamicFormFields = (option) => { | ... | @@ -275,7 +277,7 @@ const updateDynamicFormFields = (option) => { |
| 275 | } | 277 | } |
| 276 | 278 | ||
| 277 | // 确认作业选择 | 279 | // 确认作业选择 |
| 278 | -const onConfirmTask = ({ selectedOptions }) => { | 280 | +const onConfirmTask = async ({ selectedOptions }) => { |
| 279 | const option = selectedOptions[0] | 281 | const option = selectedOptions[0] |
| 280 | selectedTaskText.value = option.text | 282 | selectedTaskText.value = option.text |
| 281 | selectedTaskValue.value = [option.value] | 283 | selectedTaskValue.value = [option.value] |
| ... | @@ -288,12 +290,12 @@ const onConfirmTask = ({ selectedOptions }) => { | ... | @@ -288,12 +290,12 @@ const onConfirmTask = ({ selectedOptions }) => { |
| 288 | 290 | ||
| 289 | // 如果是计数打卡,根据选中的作业ID查询计数对象 | 291 | // 如果是计数打卡,根据选中的作业ID查询计数对象 |
| 290 | if (taskType.value === 'count') { | 292 | if (taskType.value === 'count') { |
| 291 | - fetchTargetList(selectedTaskValue.value[0]) | 293 | + await fetchTargetList(selectedTaskValue.value[0]) |
| 292 | } | 294 | } |
| 293 | } | 295 | } |
| 294 | 296 | ||
| 295 | // 监听作业选择变化, 当选中的作业ID变化时, 查询对应的计数对象 | 297 | // 监听作业选择变化, 当选中的作业ID变化时, 查询对应的计数对象 |
| 296 | -// watch(selectedTaskValue, (newVal) => { | 298 | +// watch(selectedTaskValue, async (newVal) => { |
| 297 | // if (taskType.value === 'count' && newVal && newVal.length > 0) { | 299 | // if (taskType.value === 'count' && newVal && newVal.length > 0) { |
| 298 | // // fetchTargetList(newVal[0]) | 300 | // // fetchTargetList(newVal[0]) |
| 299 | // console.warn('选中的作业ID:', newVal[0]); | 301 | // console.warn('选中的作业ID:', newVal[0]); |
| ... | @@ -310,7 +312,8 @@ const editingTarget = ref(null) | ... | @@ -310,7 +312,8 @@ const editingTarget = ref(null) |
| 310 | const isConfirmMode = ref(false) // 是否为确认模式(首次点击选中) | 312 | const isConfirmMode = ref(false) // 是否为确认模式(首次点击选中) |
| 311 | 313 | ||
| 312 | const toggleTarget = (item) => { | 314 | const toggleTarget = (item) => { |
| 313 | - const index = selectedTargets.value.findIndex(t => t.name === item.name) | 315 | + // 优先使用id匹配,如果id不存在,则使用name匹配 |
| 316 | + const index = selectedTargets.value.findIndex(t => (item.id ? t.id === item.id : t.name === item.name)) | ||
| 314 | if (index > -1) { | 317 | if (index > -1) { |
| 315 | // 取消选中 | 318 | // 取消选中 |
| 316 | selectedTargets.value.splice(index, 1) | 319 | selectedTargets.value.splice(index, 1) |
| ... | @@ -916,12 +919,25 @@ onMounted(async () => { | ... | @@ -916,12 +919,25 @@ onMounted(async () => { |
| 916 | 919 | ||
| 917 | // 如果是计数打卡,根据选中的作业ID查询计数对象 | 920 | // 如果是计数打卡,根据选中的作业ID查询计数对象 |
| 918 | if (taskType.value === 'count') { | 921 | if (taskType.value === 'count') { |
| 919 | - fetchTargetList(selectedTaskValue.value[0]) | 922 | + await fetchTargetList(selectedTaskValue.value[0]) |
| 920 | } | 923 | } |
| 921 | } | 924 | } |
| 922 | 925 | ||
| 923 | // 初始化编辑数据 | 926 | // 初始化编辑数据 |
| 924 | - await initEditData(taskOptions.value) | 927 | + await initEditData(taskOptions.value, { |
| 928 | + onTaskFound: (option) => updateDynamicFormFields(option), | ||
| 929 | + ensureTargetList: async (id) => { | ||
| 930 | + if (targetList.value.length === 0) { | ||
| 931 | + await fetchTargetList(id) | ||
| 932 | + } | ||
| 933 | + }, | ||
| 934 | + setTargets: (list) => { | ||
| 935 | + selectedTargets.value = list | ||
| 936 | + }, | ||
| 937 | + setCount: (val) => { | ||
| 938 | + countValue.value = val | ||
| 939 | + } | ||
| 940 | + }) | ||
| 925 | 941 | ||
| 926 | console.warn(selectedTaskValue.value); | 942 | console.warn(selectedTaskValue.value); |
| 927 | }) | 943 | }) | ... | ... |
-
Please register or login to post a comment