pagetype详情页的图片如果本地不存在就自动异步下载到本地
This commit is contained in:
parent
4b18aaf0cd
commit
065e8da04d
@ -416,6 +416,7 @@ import axios from 'axios'
|
||||
import { t } from '../../shared/i18n'
|
||||
import { get_session_api_headers } from '../../shared/api/auth'
|
||||
import { updateRecord, getRecord } from '../../shared/api/common'
|
||||
import { downloadImageToLocal } from '../../shared/api/common'
|
||||
import { usePageTypeSlug } from '../../shared/utils/slug'
|
||||
|
||||
const route = useRoute()
|
||||
@ -860,6 +861,73 @@ async function loadMeta() {
|
||||
}
|
||||
}
|
||||
|
||||
// 提取图片URL的函数
|
||||
function extractImageUrls(data: any): string[] {
|
||||
const urls: string[] = []
|
||||
|
||||
// 从HTML字符串中提取图片URL的正则表达式
|
||||
const imageUrlRegex = /\/files\/[^"'\s>]+\.(jpg|jpeg|png|gif|webp)/gi
|
||||
|
||||
function traverse(obj: any) {
|
||||
if (typeof obj === 'string') {
|
||||
// 检查是否是直接的相对路径图片URL
|
||||
if (obj.startsWith('/files/') && (obj.includes('.jpg') || obj.includes('.jpeg') || obj.includes('.png') || obj.includes('.gif') || obj.includes('.webp'))) {
|
||||
urls.push(obj)
|
||||
}
|
||||
|
||||
// 从HTML字符串中提取图片URL
|
||||
const matches = obj.match(imageUrlRegex)
|
||||
if (matches) {
|
||||
urls.push(...matches)
|
||||
}
|
||||
} else if (Array.isArray(obj)) {
|
||||
obj.forEach(traverse)
|
||||
} else if (obj && typeof obj === 'object') {
|
||||
Object.values(obj).forEach(traverse)
|
||||
}
|
||||
}
|
||||
|
||||
traverse(data)
|
||||
return [...new Set(urls)] // 去重
|
||||
}
|
||||
|
||||
// 图片下载缓存,避免重复检查
|
||||
const imageDownloadCache = new Set<string>()
|
||||
|
||||
// 自动下载图片的函数(优化版)
|
||||
async function autoDownloadImages(imageUrls: string[]) {
|
||||
const downloadPromises = imageUrls.map(async (url) => {
|
||||
const filename = url.split('/').pop() || 'image.jpg'
|
||||
|
||||
// 使用缓存避免重复检查
|
||||
if (imageDownloadCache.has(filename)) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await downloadImageToLocal(url, filename)
|
||||
if (result.success) {
|
||||
imageDownloadCache.add(filename)
|
||||
return true
|
||||
}
|
||||
} catch (error) {
|
||||
// 静默处理下载错误
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
const results = await Promise.all(downloadPromises)
|
||||
const hasNewDownloads = results.some(Boolean)
|
||||
|
||||
if (hasNewDownloads) {
|
||||
// 使用更平滑的更新方式,而不是整页刷新
|
||||
setTimeout(() => {
|
||||
// 重新加载当前页面数据,而不是整页刷新
|
||||
loadDetail()
|
||||
}, 300)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载详情数据
|
||||
async function loadDetail() {
|
||||
if (isNew.value) {
|
||||
@ -872,6 +940,12 @@ async function loadDetail() {
|
||||
const res = await getRecord(entity.value, id.value)
|
||||
if (res.success && res.data) {
|
||||
record.value = res.data
|
||||
|
||||
// 自动下载相对路径的图片
|
||||
const imageUrls = extractImageUrls(record.value)
|
||||
if (imageUrls.length > 0) {
|
||||
await autoDownloadImages(imageUrls)
|
||||
}
|
||||
} else {
|
||||
message.error(res.message || t('Failed to load detail'))
|
||||
record.value = {}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user