share.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <button @click="showShareMenu">分享</button>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. imageUrl: 'https://example.com/path/to/your/image.jpg' // 后端提供的图片地址
  9. };
  10. },
  11. methods: {
  12. // 显示分享菜单
  13. showShareMenu() {
  14. uni.showActionSheet({
  15. itemList: ['下载图片'],
  16. success: (res) => {
  17. if (res.tapIndex === 0) {
  18. this.downloadImage(); // 点击“下载图片”
  19. }
  20. }
  21. });
  22. },
  23. // 下载图片
  24. downloadImage() {
  25. // #ifdef H5
  26. this.downloadImageForH5();
  27. // #endif
  28. // #ifdef APP-PLUS
  29. this.downloadImageForApp();
  30. // #endif
  31. },
  32. // H5 环境下载图片
  33. downloadImageForH5() {
  34. // 创建一个隐藏的 <a> 标签
  35. const link = document.createElement('a');
  36. link.href = this.imageUrl;
  37. link.download = 'image.jpg'; // 设置下载文件名
  38. document.body.appendChild(link);
  39. link.click(); // 触发下载
  40. document.body.removeChild(link);
  41. // 提示用户手动保存
  42. uni.showToast({
  43. title: '图片已下载,请手动保存到相册。',
  44. icon: 'none'
  45. });
  46. },
  47. // APP 环境下载图片
  48. downloadImageForApp() {
  49. uni.showLoading({
  50. title: '下载中...',
  51. mask: true
  52. });
  53. // 下载图片
  54. uni.downloadFile({
  55. url: this.imageUrl, // 图片地址
  56. success: (res) => {
  57. if (res.statusCode === 200) {
  58. const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
  59. this.saveImageToAlbum(tempFilePath); // 保存到相册
  60. } else {
  61. uni.showToast({
  62. title: '下载失败',
  63. icon: 'none'
  64. });
  65. }
  66. },
  67. fail: (err) => {
  68. console.error('下载失败:', err);
  69. uni.showToast({
  70. title: '下载失败',
  71. icon: 'none'
  72. });
  73. },
  74. complete: () => {
  75. uni.hideLoading();
  76. }
  77. });
  78. },
  79. // 保存图片到相册(APP 环境)
  80. saveImageToAlbum(tempFilePath) {
  81. // 检查权限(仅 Android 需要)
  82. // #ifdef APP-PLUS
  83. if (uni.getSystemInfoSync().platform === 'android') {
  84. uni.authorize({
  85. scope: 'scope.writePhotosAlbum',
  86. success: () => {
  87. this.saveImage(tempFilePath);
  88. },
  89. fail: () => {
  90. // 无权限,提示用户去设置
  91. uni.showModal({
  92. title: '提示',
  93. content: '需要相册权限才能保存图片,是否去设置?',
  94. success: (res) => {
  95. if (res.confirm) {
  96. uni.openSetting(); // 打开设置页面
  97. }
  98. }
  99. });
  100. }
  101. });
  102. } else {
  103. this.saveImage(tempFilePath);
  104. }
  105. // #endif
  106. },
  107. saveImage(tempFilePath) {
  108. uni.saveImageToPhotosAlbum({
  109. filePath: tempFilePath,
  110. success: () => {
  111. uni.showToast({
  112. title: '保存成功',
  113. icon: 'success'
  114. });
  115. },
  116. fail: (err) => {
  117. console.error('保存失败:', err);
  118. uni.showToast({
  119. title: '保存失败',
  120. icon: 'none'
  121. });
  122. }
  123. });
  124. }
  125. }
  126. };
  127. </script>
  128. <style>
  129. button {
  130. margin-top: 20px;
  131. padding: 10px 20px;
  132. background-color: #007AFF;
  133. color: white;
  134. border-radius: 5px;
  135. }
  136. </style>