share.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* uni.showActionSheet({
  37. itemList: ['下载图片'],
  38. success: (res) => {
  39. if (res.tapIndex === 0) {
  40. this.downloadImage();
  41. }
  42. }
  43. }); */
  44. },
  45. getImage() {
  46. console.log('id', this.id);
  47. jiazhengSharePic({
  48. id: this.id
  49. }).then(res => {
  50. console.log('res',res);
  51. if(res.code ==0 &&res.data){
  52. this.imageUrl = res.data
  53. this.downloadImage()
  54. }else{
  55. uni.showToast({
  56. title: '获取图片失败',
  57. icon: 'none'
  58. });
  59. return false
  60. }
  61. })
  62. },
  63. downloadImage() {
  64. // #ifdef H5
  65. this.downloadImageForH5();
  66. // #endif
  67. // #ifdef APP-PLUS
  68. this.downloadImageForApp();
  69. // #endif
  70. },
  71. downloadImageForH5() {
  72. const link = document.createElement('a');
  73. link.href = this.imageUrl;
  74. link.download = 'image.jpg';
  75. document.body.appendChild(link);
  76. link.click();
  77. document.body.removeChild(link);
  78. uni.showToast({
  79. title: '图片已下载,请手动保存到相册。',
  80. icon: 'none'
  81. });
  82. },
  83. downloadImageForApp() {
  84. uni.showLoading({
  85. title: '下载中...',
  86. mask: true
  87. });
  88. uni.downloadFile({
  89. url: this.imageUrl,
  90. success: (res) => {
  91. if (res.statusCode === 200) {
  92. const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
  93. this.saveImageToAlbum(tempFilePath); // 保存到相册
  94. } else {
  95. uni.showToast({
  96. title: '下载失败',
  97. icon: 'none'
  98. });
  99. }
  100. },
  101. fail: (err) => {
  102. console.error('下载失败:', err);
  103. uni.showToast({
  104. title: '下载失败',
  105. icon: 'none'
  106. });
  107. },
  108. complete: () => {
  109. uni.hideLoading();
  110. }
  111. });
  112. },
  113. saveImageToAlbum(tempFilePath) {
  114. // #ifdef APP-PLUS
  115. if (uni.getSystemInfoSync().platform === 'android') {
  116. uni.authorize({
  117. scope: 'scope.writePhotosAlbum',
  118. success: () => {
  119. this.saveImage(tempFilePath);
  120. },
  121. fail: () => {
  122. uni.showModal({
  123. title: '提示',
  124. content: '需要相册权限才能保存图片,是否去设置?',
  125. success: (res) => {
  126. if (res.confirm) {
  127. uni.openSetting(); // 打开设置页面
  128. }
  129. }
  130. });
  131. }
  132. });
  133. } else {
  134. this.saveImage(tempFilePath);
  135. }
  136. // #endif
  137. },
  138. saveImage(tempFilePath) {
  139. uni.saveImageToPhotosAlbum({
  140. filePath: tempFilePath,
  141. success: () => {
  142. uni.showToast({
  143. title: '保存成功',
  144. icon: 'success'
  145. });
  146. },
  147. fail: (err) => {
  148. console.error('保存失败:', err);
  149. uni.showToast({
  150. title: '保存失败',
  151. icon: 'none'
  152. });
  153. }
  154. });
  155. }
  156. }
  157. };
  158. </script>
  159. <style>
  160. button {
  161. margin-top: 20px;
  162. padding: 10px 20px;
  163. background-color: #007AFF;
  164. color: white;
  165. border-radius: 5px;
  166. }
  167. </style>