hookehuyr

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

将附件类型处理逻辑提取为独立函数 updateAttachmentTypeOptions
调整计数打卡任务回调的执行顺序
添加任务选项找到后的通用回调处理
......@@ -406,24 +406,24 @@ export function useCheckin() {
const option = taskOptions.find(o => o.value === subTaskId.value)
selectedTaskText.value = option ? option.text : ''
// 找到任务选项后的通用回调
if (option && handlers.onTaskFound) {
handlers.onTaskFound(option)
}
// 处理计数打卡的回调
if (route.query.task_type === 'count' && option) {
// 1. 更新动态表单字段
if (handlers.onTaskFound) {
handlers.onTaskFound(option)
}
// 2. 确保目标列表已加载
// 1. 确保目标列表已加载
if (handlers.ensureTargetList) {
await handlers.ensureTargetList(subTaskId.value)
}
// 3. 恢复选中的对象
// 2. 恢复选中的对象
if (gratitudeFormList.value.length > 0 && handlers.setTargets) {
handlers.setTargets(gratitudeFormList.value)
}
// 4. 恢复次数
// 3. 恢复次数
if (gratitudeCount.value && handlers.setCount) {
handlers.setCount(gratitudeCount.value)
}
......
......@@ -72,7 +72,7 @@
</div>
<!-- 类型选项卡 -->
<div class="checkin-tabs">
<div class="checkin-tabs" v-if="selectedTaskValue.length > 0">
<div class="tabs-header">
<div class="tab-title">附件类型</div>
<div class="tabs-nav">
......@@ -288,6 +288,14 @@ const onConfirmTask = async ({ selectedOptions }) => {
// 更新动态表单字段
updateDynamicFormFields(option)
// 更新附件类型选项
if (option.attachment_type) {
updateAttachmentTypeOptions(option.attachment_type)
} else {
// 如果小作业没有配置附件类型,尝试使用大作业的默认配置
updateAttachmentTypeOptions(taskDetail.value.attachment_type)
}
// 如果是计数打卡,根据选中的作业ID查询计数对象
if (taskType.value === 'count') {
await fetchTargetList(selectedTaskValue.value[0])
......@@ -559,46 +567,49 @@ const getTaskDetail = async (month) => {
const { code, data } = await getTaskDetailAPI({ i: route.query.task_id, month })
if (code) {
taskDetail.value = data
}
}
const typeMap = {
'text': '文本',
'image': '图片',
'audio': '音频',
'video': '视频'
}
// 处理编辑模式下的类型合并
if (isEditMode.value && Array.isArray(data.attachment_type)) {
const info = await getUploadTaskInfoAPI({ i: route.query.post_id });
if (info.code) {
data.attachment_type = [...new Set([...data.attachment_type, info.data.file_type])];
}
}
/**
* 更新附件类型选项
* @param {Array|Object} attachmentType - 附件类型数据
*/
const updateAttachmentTypeOptions = (attachmentType) => {
const typeMap = {
'text': '文本',
'image': '图片',
'audio': '音频',
'video': '视频'
}
if (Array.isArray(data.attachment_type)) {
attachmentTypeOptions.value = data.attachment_type.map(key => ({
key,
value: typeMap[key] || key
}))
} else if (data.attachment_type && typeof data.attachment_type === 'object') {
attachmentTypeOptions.value = Object.entries(data.attachment_type).map(([key, value]) => ({
key,
value
}))
}
if (Array.isArray(attachmentType)) {
attachmentTypeOptions.value = attachmentType.map(key => ({
key,
value: typeMap[key] || key
}))
} else if (attachmentType && typeof attachmentType === 'object') {
attachmentTypeOptions.value = Object.entries(attachmentType).map(([key, value]) => ({
key,
value
}))
} else {
attachmentTypeOptions.value = []
}
// 如果没有解析出任何类型,或者列表为空,则使用默认4种类型
if (attachmentTypeOptions.value.length === 0) {
attachmentTypeOptions.value = [
{ key: 'text', value: '文本' },
{ key: 'image', value: '图片' },
{ key: 'audio', value: '音频' },
{ key: 'video', value: '视频' }
]
}
// 如果没有解析出任何类型,或者列表为空,则使用默认4种类型
if (attachmentTypeOptions.value.length === 0) {
attachmentTypeOptions.value = [
{ key: 'text', value: '文本' },
{ key: 'image', value: '图片' },
{ key: 'audio', value: '音频' },
{ key: 'video', value: '视频' }
]
}
// 设置默认选中类型(非计数打卡模式下)
if (taskType.value !== 'count' && attachmentTypeOptions.value.length > 0 && !activeType.value) {
// 设置默认选中类型(非计数打卡模式下)
if (taskType.value !== 'count' && attachmentTypeOptions.value.length > 0) {
// 如果当前选中的类型不在新的选项中,则重置为第一个
if (!activeType.value || !attachmentTypeOptions.value.find(o => o.key === activeType.value)) {
activeType.value = attachmentTypeOptions.value[0].key
}
}
......@@ -902,6 +913,7 @@ onMounted(async () => {
is_makeup: item.is_makeup, // 是否为补录
field_list: item.field_list, // 动态字段列表
person_type: item.person_type, // 打卡对象类型
attachment_type: item.attachment_type, // 附件类型
}))
]
}
......@@ -915,6 +927,12 @@ onMounted(async () => {
personType.value = option.person_type
// 初始化动态表单字段
updateDynamicFormFields(option)
// 更新附件类型选项
if (option.attachment_type) {
updateAttachmentTypeOptions(option.attachment_type)
} else {
updateAttachmentTypeOptions(taskDetail.value.attachment_type)
}
}
// 如果是计数打卡,根据选中的作业ID查询计数对象
......@@ -925,7 +943,15 @@ onMounted(async () => {
// 初始化编辑数据
await initEditData(taskOptions.value, {
onTaskFound: (option) => updateDynamicFormFields(option),
onTaskFound: (option) => {
updateDynamicFormFields(option)
// 更新附件类型选项
if (option.attachment_type) {
updateAttachmentTypeOptions(option.attachment_type)
} else {
updateAttachmentTypeOptions(taskDetail.value.attachment_type)
}
},
ensureTargetList: async (id) => {
if (targetList.value.length === 0) {
await fetchTargetList(id)
......