feat: 测试图片分享

This commit is contained in:
2025-11-27 15:55:10 +08:00
parent b6254a231c
commit afc60d3a03
4 changed files with 65 additions and 33 deletions

View File

@@ -8,7 +8,7 @@ import {
setTryOnEffectFavorite,
cancelTryOnEffectFavorite
} from '@/api/workshop'
import { shareImage } from '@/utils/share'
import { shareImageFile, createImageFileFromUrl, shareToWhatsApp } from '@/utils/share'
import { useRouter } from 'vue-router'
const router = useRouter()
@@ -82,25 +82,54 @@ const onLoveItem = (v) => {
})
}
const shareFileCache = new Map<string, File>()
const preloadShareFile = (imageUrl: string) => {
if (shareFileCache.has(imageUrl)) return
createImageFileFromUrl(imageUrl)
.then((file) => {
shareFileCache.set(imageUrl, file)
})
.catch((error) => {
console.error('预加载分享图片失败', error)
})
}
const isShare = ref(false)
const handleOpenShare = () => {
isShare.value = !isShare.value
alert(`现在${isShare.value ? '可以' : '不可以'}分享`)
isShare.value = !isShare.value
if (isShare.value) {
list.forEach((item) => preloadShareFile(item.tryOnUrl))
}
alert(`现在${isShare.value ? '可以' : '不可以'}分享`)
}
const onDownloadItem = (v) => {
// console.log('1111111111111111', v)
const shareTryOnImage = async (imageUrl: string) => {
const cachedFile = shareFileCache.get(imageUrl)
if (!cachedFile) {
preloadShareFile(imageUrl)
alert('图片准备中,请稍后再试')
return
}
try {
await shareImageFile(cachedFile, 'Lane Crawford Creation')
} catch (error) {
console.error('分享图片失败', error)
const text = `Lane Crawford Creation\n\n查看图片: ${imageUrl}`
shareToWhatsApp(text)
}
}
const onDownloadItem = async (v) => {
if (isShare.value) {
shareImage(v.tryOnUrl,'Creation')
} else {
if (v.loading) return
v.loading = true
v.selected = false
DownloadImages([{ url: v.tryOnUrl }], null, null, () => {
v.loading = false
v.downloaded = true
})
await shareTryOnImage(v.tryOnUrl)
return
}
if (v.loading) return
v.loading = true
v.selected = false
DownloadImages([{ url: v.tryOnUrl }], null, null, () => {
v.loading = false
v.downloaded = true
})
}
const onSelectItem = (v) => {
if (selectCount.value >= maxSelectCount && !v.selected) return