修复了课程管理的图片显示问题

This commit is contained in:
wzclm 2025-03-12 14:52:54 +08:00
parent 775f4d1e91
commit a80b379d45

View File

@ -189,6 +189,65 @@ const formRules = {
]
}
// URL
const getFileNameFromUrl = (url) => {
if (!url) return ''
//
const cleanUrl = url.replace(/uploads\/courses\/(images|videos)\/uploads\/courses\/(images|videos)\//, 'uploads/courses/$1/')
// URL
if (cleanUrl.startsWith('http://') || cleanUrl.startsWith('https://')) {
//
const matches = cleanUrl.match(/\/([^/]+)$/)
return matches ? matches[1] : cleanUrl
}
// http
if (cleanUrl.includes('localhost:3000')) {
const matches = cleanUrl.match(/([^/]+)$/)
return matches ? matches[1] : cleanUrl
}
//
if (!cleanUrl.includes('/')) {
return cleanUrl
}
//
return cleanUrl.split('/').pop()
}
// URL
const processImageUrl = (url) => {
if (!url) return ''
// URL
if (url.match(/^http:\/\/localhost:3000\/uploads\/courses\/images\/[^/]+$/)) {
return url
}
//
const fileName = getFileNameFromUrl(url)
// URL
return `http://localhost:3000/uploads/courses/images/${fileName}`
}
// URL
const processVideoUrl = (url) => {
if (!url) return ''
// URL
if (url.match(/^http:\/\/localhost:3000\/uploads\/courses\/videos\/[^/]+$/)) {
return url
}
//
const fileName = getFileNameFromUrl(url)
// URL
return `http://localhost:3000/uploads/courses/videos/${fileName}`
}
//
const handleCoverError = (error) => {
console.error('上传图片错误:', error)
@ -261,22 +320,78 @@ const handleAdd = () => {
const handleEdit = (row) => {
formMode.value = 'edit'
dialogTitle.value = '编辑课程'
// URL
const getFileNameFromUrl = (url) => {
if (!url) return ''
//
const cleanUrl = url.replace(/uploads\/courses\/(images|videos)\/uploads\/courses\/(images|videos)\//, 'uploads/courses/$1/')
// URL
if (cleanUrl.startsWith('http://') || cleanUrl.startsWith('https://')) {
//
const matches = cleanUrl.match(/\/([^/]+)$/)
return matches ? matches[1] : cleanUrl
}
// http
if (cleanUrl.includes('localhost:3000')) {
const matches = cleanUrl.match(/([^/]+)$/)
return matches ? matches[1] : cleanUrl
}
//
if (!cleanUrl.includes('/')) {
return cleanUrl
}
//
return cleanUrl.split('/').pop()
}
// URL
const processImageUrl = (url) => {
if (!url) return ''
// URL
if (url.match(/^http:\/\/localhost:3000\/uploads\/courses\/images\/[^/]+$/)) {
return url
}
//
const fileName = getFileNameFromUrl(url)
// URL
return `http://localhost:3000/uploads/courses/images/${fileName}`
}
// URL
const processVideoUrl = (url) => {
if (!url) return ''
// URL
if (url.match(/^http:\/\/localhost:3000\/uploads\/courses\/videos\/[^/]+$/)) {
return url
}
//
const fileName = getFileNameFromUrl(url)
// URL
return `http://localhost:3000/uploads/courses/videos/${fileName}`
}
//
const fileName = getFileNameFromUrl(row.cover_image)
const videoFileName = getFileNameFromUrl(row.video)
form.value = {
id: row.id,
title: row.title,
category: row.category,
cover_image: row.cover_image,
cover_image_url: row.cover_image ? (
row.cover_image.startsWith('http')
? row.cover_image
: `http://localhost:3000/uploads/courses/images/${row.cover_image}`
) : '',
video: row.video || '', //
video_url: row.video ? (
row.video.startsWith('http')
? row.video
: `http://localhost:3000/uploads/courses/videos/${row.video}`
) : '', // URL
cover_image: fileName, //
cover_image_url: `http://localhost:3000/uploads/courses/images/${fileName}`, // URL
video: videoFileName, //
video_url: `http://localhost:3000/uploads/courses/videos/${videoFileName}`, // URL
video_duration: row.video_duration || 0,
video_size: row.video_size || 0,
description: row.description || '',
@ -285,7 +400,7 @@ const handleEdit = (row) => {
imageInputType: 'upload',
videoInputType: 'upload'
}
console.log('编辑表单数据:', form.value)
dialogVisible.value = true
}
@ -402,10 +517,27 @@ const handleImageTypeChange = () => {
form.value.cover_image = ''
}
//
//
const handleImageError = (e) => {
console.error('图片加载失败:', e)
ElMessage.warning('图片加载失败,但不影响保存')
const img = e.target
if (!img || !form.value.cover_image) return
//
if (!img.dataset.retryCount) {
img.dataset.retryCount = '1'
} else if (Number(img.dataset.retryCount) >= 2) {
//
console.warn('图片加载失败,已达到最大重试次数')
return
}
//
const fileName = getFileNameFromUrl(form.value.cover_image)
// 使URL
img.src = `http://localhost:3000/uploads/courses/images/${fileName}`
//
img.dataset.retryCount = String(Number(img.dataset.retryCount) + 1)
}
//