Merge branch 'main' of ssh://18.167.251.121:10002/aidlab/lanecarford_front
This commit is contained in:
@@ -8,7 +8,6 @@ import {
|
||||
setTryOnEffectFavorite,
|
||||
cancelTryOnEffectFavorite
|
||||
} from '@/api/workshop'
|
||||
import { shareImageFile, createImageFileFromUrl, shareToWhatsApp } from '@/utils/share'
|
||||
|
||||
import { useRouter } from 'vue-router'
|
||||
const router = useRouter()
|
||||
@@ -82,46 +81,36 @@ 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
|
||||
if (isShare.value) {
|
||||
list.forEach((item) => preloadShareFile(item.tryOnUrl))
|
||||
}
|
||||
alert(`现在${isShare.value ? '可以' : '不可以'}分享`)
|
||||
const shareImageToWhatsapp = async (url) => {
|
||||
// 把图片 URL 转为 Blob
|
||||
const blob = await fetch(url).then((res) => res.blob())
|
||||
|
||||
// 创建文件对象
|
||||
const file = new File([blob], 'image.jpg', { type: 'image/jpeg' })
|
||||
|
||||
// 判断浏览器是否支持文件分享
|
||||
if (navigator.canShare && navigator.canShare({ files: [file] })) {
|
||||
await navigator.share({
|
||||
files: [file],
|
||||
title: '分享图片',
|
||||
text: '给你一张图片'
|
||||
})
|
||||
} else {
|
||||
alert('当前浏览器不支持分享图片,请使用安卓 Chrome 浏览器')
|
||||
}
|
||||
}
|
||||
|
||||
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 isShare = ref(false)
|
||||
const handleOpenShare = () => {
|
||||
isShare.value = !isShare.value
|
||||
|
||||
alert(`现在${isShare.value ? '可以' : '不可以'}分享`)
|
||||
}
|
||||
|
||||
const onDownloadItem = async (v) => {
|
||||
if (isShare.value) {
|
||||
await shareTryOnImage(v.tryOnUrl)
|
||||
return
|
||||
await shareImageToWhatsapp(v.tryOnUrl)
|
||||
return
|
||||
}
|
||||
if (v.loading) return
|
||||
v.loading = true
|
||||
|
||||
Reference in New Issue
Block a user