pagetype详情页的图片如果本地不存在就自动异步下载到本地

This commit is contained in:
jingrow 2025-10-11 04:53:23 +08:00
parent 4b18aaf0cd
commit 065e8da04d

View File

@ -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[] = []
// HTMLURL
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)
}
// HTMLURL
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 = {}