share.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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="getImage">
  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. import {
  20. jiazhengSharePic
  21. } from "@/api/jiazheng.js"
  22. export default {
  23. data() {
  24. return {
  25. imageUrl: ''
  26. };
  27. },
  28. props: {
  29. id: {
  30. type: Number,
  31. }
  32. },
  33. methods: {
  34. showShareMenu() {
  35. this.$refs.downPopupRef.open('bottom')
  36. },
  37. getImage() {
  38. console.log('id', this.id);
  39. jiazhengSharePic({
  40. id: this.id
  41. }).then(res => {
  42. console.log('res', res);
  43. if (res.code == 0 && res.data) {
  44. this.imageUrl = res.data
  45. this.downloadImage()
  46. } else {
  47. uni.showToast({
  48. title: '获取图片失败',
  49. icon: 'none'
  50. });
  51. return false
  52. }
  53. })
  54. },
  55. downloadImage() {
  56. // #ifdef H5
  57. this.downloadImageForH5();
  58. // #endif
  59. // #ifdef APP-PLUS
  60. this.downloadImageForApp();
  61. // #endif
  62. },
  63. downloadImageForH5() {
  64. const link = document.createElement('a');
  65. link.href = this.imageUrl;
  66. link.download = 'image.jpg';
  67. document.body.appendChild(link);
  68. link.click();
  69. document.body.removeChild(link);
  70. uni.showToast({
  71. title: '图片已下载,请手动保存到相册。',
  72. icon: 'none'
  73. });
  74. },
  75. downloadImageForApp() {
  76. uni.showLoading({
  77. title: '下载中...',
  78. mask: true
  79. });
  80. uni.downloadFile({
  81. url: this.imageUrl,
  82. success: (res) => {
  83. if (res.statusCode === 200) {
  84. const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
  85. this.saveImageToAlbum(tempFilePath); // 保存到相册
  86. } else {
  87. uni.showToast({
  88. title: '下载失败',
  89. icon: 'none'
  90. });
  91. }
  92. },
  93. fail: (err) => {
  94. console.error('下载失败:', err);
  95. uni.showToast({
  96. title: '下载失败',
  97. icon: 'none'
  98. });
  99. },
  100. complete: () => {
  101. uni.hideLoading();
  102. }
  103. });
  104. },
  105. saveImageToAlbum(tempFilePath) {
  106. // #ifdef APP-PLUS
  107. this.saveImage(tempFilePath);
  108. // #endif
  109. },
  110. saveImage(tempFilePath) {
  111. uni.saveImageToPhotosAlbum({
  112. filePath: tempFilePath,
  113. success: () => {
  114. uni.showToast({
  115. title: '保存成功',
  116. icon: 'success'
  117. });
  118. this.$refs.downPopupRef.close()
  119. },
  120. fail: (err) => {
  121. console.error('保存失败:', err);
  122. uni.showToast({
  123. title: '保存失败',
  124. icon: 'none'
  125. });
  126. this.$refs.downPopupRef.close()
  127. }
  128. });
  129. }
  130. }
  131. };
  132. </script>
  133. <style>
  134. button {
  135. margin-top: 20px;
  136. padding: 10px 20px;
  137. background-color: #007AFF;
  138. color: white;
  139. border-radius: 5px;
  140. }
  141. </style>