fix(purchase): 照片删除加确认 + 修复同步照片跨行删除导致图片缺失
问题: 同步照片后多行共享同一物理文件 URL,删除某行照片时 deleteFile 删除了物理文件,其他行引用同一 URL 变图片缺失 修复: - 删除前检查照片被几行引用 - 若被多行引用: 弹窗询问【全部删除】(所有行移除+删文件) 或【仅移出此行】(只从当前行移除引用,不删服务器文件) - 若仅当前行引用: 普通确认后删除 - 普通删除也加确认弹窗
This commit is contained in:
@ -629,14 +629,72 @@ const customUploadRow = async (options: any, row: any) => {
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 行内照片删除
|
||||
// ★ 统计某张照片被多少行引用(用于判断同步后的跨行删除)
|
||||
const countImageRefs = (urlToRemove: string): number => {
|
||||
let count = 0
|
||||
formItems.value.forEach(r => {
|
||||
if ((r.images || []).includes(urlToRemove)) count++
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
// ★ 行内照片删除(带确认 + 跨行引用保护)
|
||||
const handleRemoveRowImage = async (uploadFile: any, row: any) => {
|
||||
const urlToRemove = (row.images || []).find(u => getImageUrl(u) === uploadFile.url) || uploadFile.url
|
||||
row.images = (row.images || []).filter(u => u !== urlToRemove)
|
||||
row.fileList = (row.fileList || []).filter(f => f.url !== uploadFile.url)
|
||||
const filename = urlToRemove.split('/').pop()
|
||||
if (filename && !urlToRemove.startsWith('http')) {
|
||||
await deleteFile(filename).catch(() => {})
|
||||
const isInternal = filename && !urlToRemove.startsWith('http')
|
||||
|
||||
// 检查该照片是否也被其他行引用(同步照片场景)
|
||||
const refCount = countImageRefs(urlToRemove)
|
||||
const sharedByOthers = refCount > 1 // 包含当前行
|
||||
|
||||
if (sharedByOthers) {
|
||||
// ★ 同步照片:询问是否全部删除
|
||||
const otherCount = refCount - 1
|
||||
try {
|
||||
const action = await ElMessageBox.confirm(
|
||||
`此照片当前被 ${refCount} 行使用(含本行)。\n\n` +
|
||||
`• 【全部删除】将移除所有行中的该照片,并删除服务器文件\n` +
|
||||
`• 【仅移出此行】只从当前行移除引用,其他行保留(不删服务器文件)`,
|
||||
'删除照片',
|
||||
{
|
||||
confirmButtonText: '全部删除',
|
||||
cancelButtonText: '仅移出此行',
|
||||
distinguishCancelAndClose: true,
|
||||
type: 'warning'
|
||||
}
|
||||
)
|
||||
// 用户点【全部删除】
|
||||
formItems.value.forEach(r => {
|
||||
r.images = (r.images || []).filter(u => u !== urlToRemove)
|
||||
r.fileList = (r.fileList || []).filter(f => f.url !== uploadFile.url && f.url !== urlToRemove)
|
||||
})
|
||||
if (isInternal) await deleteFile(filename).catch(() => {})
|
||||
ElMessage.success(`已从 ${refCount} 行删除该照片`)
|
||||
} catch (action) {
|
||||
if (action === 'cancel') {
|
||||
// 用户点【仅移出此行】
|
||||
row.images = (row.images || []).filter(u => u !== urlToRemove)
|
||||
row.fileList = (row.fileList || []).filter(f => f.url !== uploadFile.url && f.url !== urlToRemove)
|
||||
ElMessage.success('已从当前行移除该照片,其他行保留')
|
||||
}
|
||||
// close 或直接关闭 → 不操作
|
||||
}
|
||||
} else {
|
||||
// 普通删除:仅当前行引用,确认后删除
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
`确定删除这张照片吗?删除后不可恢复。`,
|
||||
'删除照片',
|
||||
{ confirmButtonText: '删除', cancelButtonText: '取消', type: 'warning' }
|
||||
)
|
||||
row.images = (row.images || []).filter(u => u !== urlToRemove)
|
||||
row.fileList = (row.fileList || []).filter(f => f.url !== uploadFile.url && f.url !== urlToRemove)
|
||||
if (isInternal) await deleteFile(filename).catch(() => {})
|
||||
ElMessage.success('照片已删除')
|
||||
} catch (e) {
|
||||
// 用户取消,不操作
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user