hookehuyr

refactor(plan): 提取提交数据转换逻辑为独立工具函数

将 PlanFormContainer 中内联的金额转换逻辑抽取到
planSubmitTransformers.js,支持函数式和字符串式 transform 策略,
新增 withdrawal_stages 字段的分转元转换。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
......@@ -50,6 +50,10 @@ import SavingsTemplate from './PlanTemplates/SavingsTemplate.vue'
import { PLAN_TEMPLATES } from '@/config/plan-templates'
import { addAPI } from '@/api/plan'
import { useFieldValueTransform } from '@/composables/useFieldValueTransform'
import {
transformSubmitValue,
transformWithdrawalStagesForSubmit
} from '@/utils/planSubmitTransformers'
/**
* 组件属性
......@@ -280,6 +284,7 @@ const submit = async () => {
withdrawal_method: { api_field: 'withdrawal_method' },
annual_withdrawal_amount: { api_field: 'annual_withdrawal_amount', transform: 'fen_to_yuan' },
annual_increase_percentage: { api_field: 'annual_increase_percentage' },
withdrawal_stages: { api_field: 'withdrawal_stages', transform: transformWithdrawalStagesForSubmit },
withdrawal_start_age_specified: { api_field: 'withdrawal_start_age' },
withdrawal_period_specified: { api_field: 'withdrawal_period' },
withdrawal_start_age_fixed: { api_field: 'withdrawal_start_age' },
......@@ -301,10 +306,12 @@ const submit = async () => {
if (mapping) {
const apiField = typeof mapping === 'string' ? mapping : mapping.api_field
let value = formData.value[key]
// 金额字段从分转换为元
if (typeof mapping === 'object' && mapping.transform === 'fen_to_yuan' && value !== null && value !== undefined && value !== '') {
value = toYuan(key, value)
}
value = transformSubmitValue({
fieldKey: key,
value,
mapping,
toYuan
})
requestData[apiField] = value
} else {
requestData[key] = formData.value[key]
......
......@@ -34,6 +34,8 @@
* form_sn: "life-insurance-wiop3e" // 对应下面的配置 key
* }
*/
import { transformWithdrawalStagesForSubmit } from '@/utils/planSubmitTransformers'
// 基础提交字段映射(适用于人寿/重疾等通用表单)
const baseSubmitMapping = {
customer_name: { api_field: 'customer_name' },
......@@ -76,6 +78,7 @@ const savingsSubmitMapping = {
withdrawal_method: { api_field: 'withdrawal_method' },
annual_withdrawal_amount: { api_field: 'annual_withdrawal_amount', transform: 'fen_to_yuan' },
annual_increase_percentage: { api_field: 'annual_increase_percentage' },
withdrawal_stages: { api_field: 'withdrawal_stages', transform: transformWithdrawalStagesForSubmit },
withdrawal_start_age_specified: { api_field: 'withdrawal_start_age' },
withdrawal_period_specified: { api_field: 'withdrawal_period' },
withdrawal_start_age_fixed: { api_field: 'withdrawal_start_age' },
......
import { describe, expect, it, vi } from 'vitest'
import {
transformSubmitValue,
transformWithdrawalStagesForSubmit
} from '../planSubmitTransformers'
describe('transformWithdrawalStagesForSubmit', () => {
it('should convert withdrawal stage amount from fen to yuan', () => {
const result = transformWithdrawalStagesForSubmit([
{
annual_withdrawal_amount: 12300,
withdrawal_start_age: 18,
withdrawal_period: '1年',
annual_increase_percentage: '12'
}
])
expect(result).toEqual([
{
annual_withdrawal_amount: '123.00',
withdrawal_start_age: 18,
withdrawal_period: '1年',
annual_increase_percentage: '12'
}
])
})
it('should keep empty stage amount unchanged', () => {
const result = transformWithdrawalStagesForSubmit([
{
annual_withdrawal_amount: null,
withdrawal_start_age: 18
}
])
expect(result[0].annual_withdrawal_amount).toBeNull()
})
})
describe('transformSubmitValue', () => {
it('should use fen_to_yuan mapping for simple amount fields', () => {
const toYuan = vi.fn().mockReturnValue('123.00')
const result = transformSubmitValue({
fieldKey: 'annual_withdrawal_amount',
value: 12300,
mapping: {
api_field: 'annual_withdrawal_amount',
transform: 'fen_to_yuan'
},
toYuan
})
expect(result).toBe('123.00')
expect(toYuan).toHaveBeenCalledWith('annual_withdrawal_amount', 12300)
})
it('should use custom mapping transform for withdrawal stages', () => {
const result = transformSubmitValue({
fieldKey: 'withdrawal_stages',
value: [
{
annual_withdrawal_amount: 12300,
withdrawal_start_age: 18
}
],
mapping: {
api_field: 'withdrawal_stages',
transform: transformWithdrawalStagesForSubmit
},
toYuan: vi.fn()
})
expect(result).toEqual([
{
annual_withdrawal_amount: '123.00',
withdrawal_start_age: 18
}
])
})
})
import { TRANSFORM_TYPES } from '@/config/plan-fields'
import { fenToYuan } from './planFieldTransformers'
const hasValue = (value) => value !== null && value !== undefined && value !== ''
export function transformWithdrawalStagesForSubmit(value) {
if (!Array.isArray(value)) {
return value
}
return value.map(stage => {
if (!stage || typeof stage !== 'object') {
return stage
}
return {
...stage,
annual_withdrawal_amount: hasValue(stage.annual_withdrawal_amount)
? fenToYuan(stage.annual_withdrawal_amount)
: stage.annual_withdrawal_amount
}
})
}
export function transformSubmitValue({ fieldKey, value, mapping, toYuan }) {
if (!mapping || typeof mapping === 'string') {
return value
}
if (typeof mapping.transform === 'function') {
return mapping.transform(value, { fieldKey, mapping, toYuan })
}
if (mapping.transform === TRANSFORM_TYPES.FEN_TO_YUAN && hasValue(value)) {
return toYuan(fieldKey, value)
}
return value
}