hookehuyr

fix(checkin): 修复提交任务时接口兼容性问题

当新结构提交失败时,回退到旧结构参数格式重试
处理混合类型附件的错误提示
将editUploadTaskInfoAPI的HTTP方法从GET改为POST
...@@ -130,7 +130,7 @@ export const getUploadTaskInfoAPI = (params) => fn(fetch.get(Api.TASK_UPLOAD_IN ...@@ -130,7 +130,7 @@ export const getUploadTaskInfoAPI = (params) => fn(fetch.get(Api.TASK_UPLOAD_IN
130 * @param gratitude_form_list 感恩表单数据 [{id,name,city,unit,其他信息字段}] 130 * @param gratitude_form_list 感恩表单数据 [{id,name,city,unit,其他信息字段}]
131 * @returns 131 * @returns
132 */ 132 */
133 -export const editUploadTaskInfoAPI = (params) => fn(fetch.get(Api.TASK_UPLOAD_EDIT, params)) 133 +export const editUploadTaskInfoAPI = (params) => fn(fetch.post(Api.TASK_UPLOAD_EDIT, params))
134 134
135 /** 135 /**
136 * @description: 删除打卡动态详情 136 * @description: 删除打卡动态详情
......
1 import { describe, expect, it, vi, beforeEach } from 'vitest' 1 import { describe, expect, it, vi, beforeEach } from 'vitest'
2 2
3 vi.mock('vue-router', () => { 3 vi.mock('vue-router', () => {
4 + const router = {
5 + push: vi.fn(),
6 + back: vi.fn()
7 + }
4 return { 8 return {
5 useRoute: () => ({ 9 useRoute: () => ({
6 query: {} 10 query: {}
7 }), 11 }),
8 - useRouter: () => ({ 12 + useRouter: () => router
9 - push: vi.fn()
10 - })
11 } 13 }
12 }) 14 })
13 15
...@@ -51,10 +53,22 @@ vi.mock('@/contexts/auth', async () => { ...@@ -51,10 +53,22 @@ vi.mock('@/contexts/auth', async () => {
51 53
52 import { useCheckin } from '../useCheckin' 54 import { useCheckin } from '../useCheckin'
53 import { showToast } from 'vant' 55 import { showToast } from 'vant'
56 +import { addUploadTaskAPI } from '@/api/checkin'
54 57
55 describe('useCheckin 上传大小限制', () => { 58 describe('useCheckin 上传大小限制', () => {
56 beforeEach(() => { 59 beforeEach(() => {
57 vi.clearAllMocks() 60 vi.clearAllMocks()
61 + if (!globalThis.sessionStorage) {
62 + let store = {}
63 + globalThis.sessionStorage = {
64 + getItem: (key) => store[key] ?? null,
65 + setItem: (key, value) => { store[key] = String(value) },
66 + removeItem: (key) => { delete store[key] },
67 + clear: () => { store = {} },
68 + }
69 + } else {
70 + sessionStorage.clear()
71 + }
58 }) 72 })
59 73
60 it('setMaxFileSizeMbMap 能更新并保留其他类型默认值', () => { 74 it('setMaxFileSizeMbMap 能更新并保留其他类型默认值', () => {
...@@ -119,3 +133,49 @@ describe('useCheckin 上传大小限制', () => { ...@@ -119,3 +133,49 @@ describe('useCheckin 上传大小限制', () => {
119 }) 133 })
120 }) 134 })
121 135
136 +describe('useCheckin 提交兼容', () => {
137 + beforeEach(() => {
138 + vi.clearAllMocks()
139 + if (globalThis.sessionStorage?.clear) sessionStorage.clear()
140 + })
141 +
142 + it('新结构失败会回退旧结构提交', async () => {
143 + addUploadTaskAPI
144 + .mockResolvedValueOnce({ code: 0, msg: '参数错误' })
145 + .mockResolvedValueOnce({ code: 1, data: { id: 1 } })
146 +
147 + const { activeType, fileList, message, onSubmit } = useCheckin()
148 +
149 + activeType.value = 'audio'
150 + message.value = '随便写点内容'
151 + fileList.value = [
152 + { status: 'done', meta_id: 11, file_type: 'audio' }
153 + ]
154 +
155 + await onSubmit({ subtask_id: 's1' })
156 +
157 + expect(addUploadTaskAPI).toHaveBeenCalledTimes(2)
158 + expect(addUploadTaskAPI.mock.calls[0][0]).toHaveProperty('files')
159 + expect(addUploadTaskAPI.mock.calls[1][0]).toHaveProperty('meta_id')
160 + expect(addUploadTaskAPI.mock.calls[1][0]).not.toHaveProperty('files')
161 + expect(showToast).toHaveBeenCalledWith('提交成功')
162 + })
163 +
164 + it('混合附件且新结构失败时会提示分别提交', async () => {
165 + addUploadTaskAPI.mockResolvedValueOnce({ code: 0, msg: '参数错误' })
166 +
167 + const { activeType, fileList, message, onSubmit } = useCheckin()
168 +
169 + activeType.value = 'image'
170 + message.value = '随便写点内容'
171 + fileList.value = [
172 + { status: 'done', meta_id: 11, file_type: 'image' },
173 + { status: 'done', meta_id: 12, file_type: 'audio' },
174 + ]
175 +
176 + await onSubmit({ subtask_id: 's1' })
177 +
178 + expect(addUploadTaskAPI).toHaveBeenCalledTimes(1)
179 + expect(showToast).toHaveBeenCalledWith('当前接口暂不支持多类型附件,请分别提交')
180 + })
181 +})
......
...@@ -510,7 +510,42 @@ export function useCheckin() { ...@@ -510,7 +510,42 @@ export function useCheckin() {
510 result = await addUploadTaskAPI(submitData) 510 result = await addUploadTaskAPI(submitData)
511 } 511 }
512 512
513 - if (result.code === 1) { 513 + if (result?.code !== 1) {
514 + const types = new Set((files || []).map(f => f.file_type).filter(Boolean))
515 + const hasMixedTypes = types.size > 1
516 +
517 + if (hasMixedTypes) {
518 + showToast('当前接口暂不支持多类型附件,请分别提交')
519 + return
520 + }
521 +
522 + const legacy_type = files?.[0]?.file_type || submitData.file_type || activeType.value
523 + const legacy_meta_id = (files || []).filter(f => f.file_type === legacy_type).map(f => f.meta_id)
524 +
525 + const legacy_payload = {
526 + ...extraData,
527 + note: submitData.note,
528 + file_type: legacy_type,
529 + meta_id: legacy_meta_id,
530 + makeup_time: submitData.makeup_time,
531 + }
532 +
533 + if (route.query.status === 'edit') {
534 + result = await editUploadTaskInfoAPI({
535 + i: route.query.post_id,
536 + subtask_id: legacy_payload.subtask_id || route.query.subtask_id,
537 + note: legacy_payload.note,
538 + meta_id: legacy_payload.meta_id,
539 + file_type: legacy_payload.file_type,
540 + gratitude_form_list: legacy_payload.gratitude_form_list,
541 + gratitude_count: legacy_payload.gratitude_count,
542 + })
543 + } else {
544 + result = await addUploadTaskAPI(legacy_payload)
545 + }
546 + }
547 +
548 + if (result?.code === 1) {
514 showToast('提交成功') 549 showToast('提交成功')
515 550
516 // 设置刷新标记,用于列表页更新数据 551 // 设置刷新标记,用于列表页更新数据
...@@ -524,6 +559,8 @@ export function useCheckin() { ...@@ -524,6 +559,8 @@ export function useCheckin() {
524 } 559 }
525 560
526 router.back() 561 router.back()
562 + } else {
563 + showToast(result?.msg || '提交失败,请重试')
527 } 564 }
528 } catch (error) { 565 } catch (error) {
529 showToast('提交失败,请重试') 566 showToast('提交失败,请重试')
......