share.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <button @click="showShareMenu" type="default" class="phone-white-btn bz-tel-btn fx-btn-box">分享</button>
  3. <!-- 答题卡 -->
  4. <uni-popup ref="downPopupRef" background-color="#fff" :animation="false" :is-mask-click="true" :mask-click="false">
  5. <view class="share-haibao-box">
  6. <view class="phone-line-title">分享</view>
  7. <view class="share-list-box">
  8. <view class="share-item-box" @click="downHb">
  9. <view class="share-icon-box">
  10. <icon></icon>
  11. </view>
  12. <text>下载图片</text>
  13. </view>
  14. </view>
  15. </view>
  16. </uni-popup>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. imageUrl: ''
  23. };
  24. },
  25. methods: {
  26. downHb(){
  27. console.log('下载图片');
  28. },
  29. showShareMenu() {
  30. this.$refs.downPopupRef.open('bottom')
  31. /* uni.showActionSheet({
  32. itemList: ['下载图片'],
  33. success: (res) => {
  34. if (res.tapIndex === 0) {
  35. this.downloadImage();
  36. }
  37. }
  38. }); */
  39. },
  40. downloadImage() {
  41. // #ifdef H5
  42. this.downloadImageForH5();
  43. // #endif
  44. // #ifdef APP-PLUS
  45. this.downloadImageForApp();
  46. // #endif
  47. },
  48. downloadImageForH5() {
  49. const link = document.createElement('a');
  50. link.href = this.imageUrl;
  51. link.download = 'image.jpg';
  52. document.body.appendChild(link);
  53. link.click();
  54. document.body.removeChild(link);
  55. uni.showToast({
  56. title: '图片已下载,请手动保存到相册。',
  57. icon: 'none'
  58. });
  59. },
  60. downloadImageForApp() {
  61. uni.showLoading({
  62. title: '下载中...',
  63. mask: true
  64. });
  65. uni.downloadFile({
  66. url: this.imageUrl,
  67. success: (res) => {
  68. if (res.statusCode === 200) {
  69. const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
  70. this.saveImageToAlbum(tempFilePath); // 保存到相册
  71. } else {
  72. uni.showToast({
  73. title: '下载失败',
  74. icon: 'none'
  75. });
  76. }
  77. },
  78. fail: (err) => {
  79. console.error('下载失败:', err);
  80. uni.showToast({
  81. title: '下载失败',
  82. icon: 'none'
  83. });
  84. },
  85. complete: () => {
  86. uni.hideLoading();
  87. }
  88. });
  89. },
  90. saveImageToAlbum(tempFilePath) {
  91. // #ifdef APP-PLUS
  92. if (uni.getSystemInfoSync().platform === 'android') {
  93. uni.authorize({
  94. scope: 'scope.writePhotosAlbum',
  95. success: () => {
  96. this.saveImage(tempFilePath);
  97. },
  98. fail: () => {
  99. uni.showModal({
  100. title: '提示',
  101. content: '需要相册权限才能保存图片,是否去设置?',
  102. success: (res) => {
  103. if (res.confirm) {
  104. uni.openSetting(); // 打开设置页面
  105. }
  106. }
  107. });
  108. }
  109. });
  110. } else {
  111. this.saveImage(tempFilePath);
  112. }
  113. // #endif
  114. },
  115. saveImage(tempFilePath) {
  116. uni.saveImageToPhotosAlbum({
  117. filePath: tempFilePath,
  118. success: () => {
  119. uni.showToast({
  120. title: '保存成功',
  121. icon: 'success'
  122. });
  123. },
  124. fail: (err) => {
  125. console.error('保存失败:', err);
  126. uni.showToast({
  127. title: '保存失败',
  128. icon: 'none'
  129. });
  130. }
  131. });
  132. }
  133. }
  134. };
  135. </script>
  136. <style>
  137. button {
  138. margin-top: 20px;
  139. padding: 10px 20px;
  140. background-color: #007AFF;
  141. color: white;
  142. border-radius: 5px;
  143. }
  144. </style>