share copy.vue 3.4 KB

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