fix(stocking): 修复编辑回填因 attachment 格式导致的车辆明细不渲染

后端 show 返回的 attachment 为 JSON 字符串时,.map 抛 TypeError
中断整个 items 回填。增加 normalizeAttachment 统一兼容
数组/字符串/空值。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
mzl
2026-08-14 17:47:48 +08:00
parent 0dfc9a8bcc
commit 77ac384d29
+15 -1
View File
@@ -974,6 +974,20 @@ export function useStockingForm(isFinal = false, stockingId?: string | number) {
}
// 数据回显方法
// 后端 attachment 可能返回数组或 JSON 字符串(未 decode),统一转数组
const normalizeAttachment = (raw: any): any[] => {
if (Array.isArray(raw)) return raw
if (typeof raw === 'string' && raw.trim()) {
try {
const parsed = JSON.parse(raw)
return Array.isArray(parsed) ? parsed : []
} catch {
return []
}
}
return []
}
const populateFromDetail = (res: any) => {
if (!res) return
isPopulating.value = true
@@ -1021,7 +1035,7 @@ export function useStockingForm(isFinal = false, stockingId?: string | number) {
_echo_vin: Boolean(it.vin),
mileage: it.mileage ?? 0,
model_snapshot: it.model_snapshot || '',
attachment_files: (it.attachment || []).map((f: any) => ({
attachment_files: normalizeAttachment(it.attachment).map((f: any) => ({
name: f.original_name || f.name || '附件',
url: f.url || f.file_url || '',
file_name: f.file_name || f.url || ''