hookehuyr

refactor(打卡): 重构附件类型处理逻辑并优化任务回调顺序

将附件类型处理逻辑提取为独立函数 updateAttachmentTypeOptions
调整计数打卡任务回调的执行顺序
添加任务选项找到后的通用回调处理
...@@ -406,24 +406,24 @@ export function useCheckin() { ...@@ -406,24 +406,24 @@ export function useCheckin() {
406 const option = taskOptions.find(o => o.value === subTaskId.value) 406 const option = taskOptions.find(o => o.value === subTaskId.value)
407 selectedTaskText.value = option ? option.text : '' 407 selectedTaskText.value = option ? option.text : ''
408 408
409 - // 处理计数打卡的回调 409 + // 找到任务选项后的通用回调
410 - if (route.query.task_type === 'count' && option) { 410 + if (option && handlers.onTaskFound) {
411 - // 1. 更新动态表单字段
412 - if (handlers.onTaskFound) {
413 handlers.onTaskFound(option) 411 handlers.onTaskFound(option)
414 } 412 }
415 413
416 - // 2. 确保目标列表已加载 414 + // 处理计数打卡的回调
415 + if (route.query.task_type === 'count' && option) {
416 + // 1. 确保目标列表已加载
417 if (handlers.ensureTargetList) { 417 if (handlers.ensureTargetList) {
418 await handlers.ensureTargetList(subTaskId.value) 418 await handlers.ensureTargetList(subTaskId.value)
419 } 419 }
420 420
421 - // 3. 恢复选中的对象 421 + // 2. 恢复选中的对象
422 if (gratitudeFormList.value.length > 0 && handlers.setTargets) { 422 if (gratitudeFormList.value.length > 0 && handlers.setTargets) {
423 handlers.setTargets(gratitudeFormList.value) 423 handlers.setTargets(gratitudeFormList.value)
424 } 424 }
425 425
426 - // 4. 恢复次数 426 + // 3. 恢复次数
427 if (gratitudeCount.value && handlers.setCount) { 427 if (gratitudeCount.value && handlers.setCount) {
428 handlers.setCount(gratitudeCount.value) 428 handlers.setCount(gratitudeCount.value)
429 } 429 }
......
...@@ -72,7 +72,7 @@ ...@@ -72,7 +72,7 @@
72 </div> 72 </div>
73 73
74 <!-- 类型选项卡 --> 74 <!-- 类型选项卡 -->
75 - <div class="checkin-tabs"> 75 + <div class="checkin-tabs" v-if="selectedTaskValue.length > 0">
76 <div class="tabs-header"> 76 <div class="tabs-header">
77 <div class="tab-title">附件类型</div> 77 <div class="tab-title">附件类型</div>
78 <div class="tabs-nav"> 78 <div class="tabs-nav">
...@@ -288,6 +288,14 @@ const onConfirmTask = async ({ selectedOptions }) => { ...@@ -288,6 +288,14 @@ const onConfirmTask = async ({ selectedOptions }) => {
288 // 更新动态表单字段 288 // 更新动态表单字段
289 updateDynamicFormFields(option) 289 updateDynamicFormFields(option)
290 290
291 + // 更新附件类型选项
292 + if (option.attachment_type) {
293 + updateAttachmentTypeOptions(option.attachment_type)
294 + } else {
295 + // 如果小作业没有配置附件类型,尝试使用大作业的默认配置
296 + updateAttachmentTypeOptions(taskDetail.value.attachment_type)
297 + }
298 +
291 // 如果是计数打卡,根据选中的作业ID查询计数对象 299 // 如果是计数打卡,根据选中的作业ID查询计数对象
292 if (taskType.value === 'count') { 300 if (taskType.value === 'count') {
293 await fetchTargetList(selectedTaskValue.value[0]) 301 await fetchTargetList(selectedTaskValue.value[0])
...@@ -559,7 +567,14 @@ const getTaskDetail = async (month) => { ...@@ -559,7 +567,14 @@ const getTaskDetail = async (month) => {
559 const { code, data } = await getTaskDetailAPI({ i: route.query.task_id, month }) 567 const { code, data } = await getTaskDetailAPI({ i: route.query.task_id, month })
560 if (code) { 568 if (code) {
561 taskDetail.value = data 569 taskDetail.value = data
570 + }
571 +}
562 572
573 +/**
574 + * 更新附件类型选项
575 + * @param {Array|Object} attachmentType - 附件类型数据
576 + */
577 +const updateAttachmentTypeOptions = (attachmentType) => {
563 const typeMap = { 578 const typeMap = {
564 'text': '文本', 579 'text': '文本',
565 'image': '图片', 580 'image': '图片',
...@@ -567,24 +582,18 @@ const getTaskDetail = async (month) => { ...@@ -567,24 +582,18 @@ const getTaskDetail = async (month) => {
567 'video': '视频' 582 'video': '视频'
568 } 583 }
569 584
570 - // 处理编辑模式下的类型合并 585 + if (Array.isArray(attachmentType)) {
571 - if (isEditMode.value && Array.isArray(data.attachment_type)) { 586 + attachmentTypeOptions.value = attachmentType.map(key => ({
572 - const info = await getUploadTaskInfoAPI({ i: route.query.post_id });
573 - if (info.code) {
574 - data.attachment_type = [...new Set([...data.attachment_type, info.data.file_type])];
575 - }
576 - }
577 -
578 - if (Array.isArray(data.attachment_type)) {
579 - attachmentTypeOptions.value = data.attachment_type.map(key => ({
580 key, 587 key,
581 value: typeMap[key] || key 588 value: typeMap[key] || key
582 })) 589 }))
583 - } else if (data.attachment_type && typeof data.attachment_type === 'object') { 590 + } else if (attachmentType && typeof attachmentType === 'object') {
584 - attachmentTypeOptions.value = Object.entries(data.attachment_type).map(([key, value]) => ({ 591 + attachmentTypeOptions.value = Object.entries(attachmentType).map(([key, value]) => ({
585 key, 592 key,
586 value 593 value
587 })) 594 }))
595 + } else {
596 + attachmentTypeOptions.value = []
588 } 597 }
589 598
590 // 如果没有解析出任何类型,或者列表为空,则使用默认4种类型 599 // 如果没有解析出任何类型,或者列表为空,则使用默认4种类型
...@@ -598,7 +607,9 @@ const getTaskDetail = async (month) => { ...@@ -598,7 +607,9 @@ const getTaskDetail = async (month) => {
598 } 607 }
599 608
600 // 设置默认选中类型(非计数打卡模式下) 609 // 设置默认选中类型(非计数打卡模式下)
601 - if (taskType.value !== 'count' && attachmentTypeOptions.value.length > 0 && !activeType.value) { 610 + if (taskType.value !== 'count' && attachmentTypeOptions.value.length > 0) {
611 + // 如果当前选中的类型不在新的选项中,则重置为第一个
612 + if (!activeType.value || !attachmentTypeOptions.value.find(o => o.key === activeType.value)) {
602 activeType.value = attachmentTypeOptions.value[0].key 613 activeType.value = attachmentTypeOptions.value[0].key
603 } 614 }
604 } 615 }
...@@ -902,6 +913,7 @@ onMounted(async () => { ...@@ -902,6 +913,7 @@ onMounted(async () => {
902 is_makeup: item.is_makeup, // 是否为补录 913 is_makeup: item.is_makeup, // 是否为补录
903 field_list: item.field_list, // 动态字段列表 914 field_list: item.field_list, // 动态字段列表
904 person_type: item.person_type, // 打卡对象类型 915 person_type: item.person_type, // 打卡对象类型
916 + attachment_type: item.attachment_type, // 附件类型
905 })) 917 }))
906 ] 918 ]
907 } 919 }
...@@ -915,6 +927,12 @@ onMounted(async () => { ...@@ -915,6 +927,12 @@ onMounted(async () => {
915 personType.value = option.person_type 927 personType.value = option.person_type
916 // 初始化动态表单字段 928 // 初始化动态表单字段
917 updateDynamicFormFields(option) 929 updateDynamicFormFields(option)
930 + // 更新附件类型选项
931 + if (option.attachment_type) {
932 + updateAttachmentTypeOptions(option.attachment_type)
933 + } else {
934 + updateAttachmentTypeOptions(taskDetail.value.attachment_type)
935 + }
918 } 936 }
919 937
920 // 如果是计数打卡,根据选中的作业ID查询计数对象 938 // 如果是计数打卡,根据选中的作业ID查询计数对象
...@@ -925,7 +943,15 @@ onMounted(async () => { ...@@ -925,7 +943,15 @@ onMounted(async () => {
925 943
926 // 初始化编辑数据 944 // 初始化编辑数据
927 await initEditData(taskOptions.value, { 945 await initEditData(taskOptions.value, {
928 - onTaskFound: (option) => updateDynamicFormFields(option), 946 + onTaskFound: (option) => {
947 + updateDynamicFormFields(option)
948 + // 更新附件类型选项
949 + if (option.attachment_type) {
950 + updateAttachmentTypeOptions(option.attachment_type)
951 + } else {
952 + updateAttachmentTypeOptions(taskDetail.value.attachment_type)
953 + }
954 + },
929 ensureTargetList: async (id) => { 955 ensureTargetList: async (id) => {
930 if (targetList.value.length === 0) { 956 if (targetList.value.length === 0) {
931 await fetchTargetList(id) 957 await fetchTargetList(id)
......