|
@@ -1,28 +1,29 @@
|
|
|
<template>
|
|
|
<button @click="showShareMenu">分享</button>
|
|
|
+
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
- imageUrl: 'https://example.com/path/to/your/image.jpg' // 后端提供的图片地址
|
|
|
+ imageUrl: ''
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- // 显示分享菜单
|
|
|
+
|
|
|
showShareMenu() {
|
|
|
uni.showActionSheet({
|
|
|
itemList: ['下载图片'],
|
|
|
success: (res) => {
|
|
|
if (res.tapIndex === 0) {
|
|
|
- this.downloadImage(); // 点击“下载图片”
|
|
|
+ this.downloadImage();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- // 下载图片
|
|
|
+
|
|
|
downloadImage() {
|
|
|
// #ifdef H5
|
|
|
this.downloadImageForH5();
|
|
@@ -33,33 +34,29 @@ export default {
|
|
|
// #endif
|
|
|
},
|
|
|
|
|
|
- // H5 环境下载图片
|
|
|
+
|
|
|
downloadImageForH5() {
|
|
|
- // 创建一个隐藏的 <a> 标签
|
|
|
+
|
|
|
const link = document.createElement('a');
|
|
|
link.href = this.imageUrl;
|
|
|
- link.download = 'image.jpg'; // 设置下载文件名
|
|
|
+ link.download = 'image.jpg';
|
|
|
document.body.appendChild(link);
|
|
|
- link.click(); // 触发下载
|
|
|
+ link.click();
|
|
|
document.body.removeChild(link);
|
|
|
-
|
|
|
- // 提示用户手动保存
|
|
|
uni.showToast({
|
|
|
title: '图片已下载,请手动保存到相册。',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- // APP 环境下载图片
|
|
|
downloadImageForApp() {
|
|
|
uni.showLoading({
|
|
|
title: '下载中...',
|
|
|
mask: true
|
|
|
});
|
|
|
|
|
|
- // 下载图片
|
|
|
uni.downloadFile({
|
|
|
- url: this.imageUrl, // 图片地址
|
|
|
+ url: this.imageUrl,
|
|
|
success: (res) => {
|
|
|
if (res.statusCode === 200) {
|
|
|
const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
|
|
@@ -84,9 +81,8 @@ export default {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- // 保存图片到相册(APP 环境)
|
|
|
+
|
|
|
saveImageToAlbum(tempFilePath) {
|
|
|
- // 检查权限(仅 Android 需要)
|
|
|
// #ifdef APP-PLUS
|
|
|
if (uni.getSystemInfoSync().platform === 'android') {
|
|
|
uni.authorize({
|
|
@@ -95,7 +91,6 @@ export default {
|
|
|
this.saveImage(tempFilePath);
|
|
|
},
|
|
|
fail: () => {
|
|
|
- // 无权限,提示用户去设置
|
|
|
uni.showModal({
|
|
|
title: '提示',
|
|
|
content: '需要相册权限才能保存图片,是否去设置?',
|