当前位置:   article > 正文

uniapp小程序实现海报下载图片到手机本地_uniapp的下载图片

uniapp的下载图片

 图片效果

  1. <template>
  2. <u-popup :show="show" closeable @close="handleClose" zIndex="999999">
  3. <view class="shareModel">
  4. <view class="title">分享店铺</view>
  5. <!-- 分享模态框 -->
  6. <view class="share-container" @click.stop>
  7. <view class="merchant-info">
  8. <view class="merchantName">{{ storeInfo.merchantName }}</view>
  9. <view class="store-img">
  10. <image class="img" :src="storeInfo.merchantIconStaticPath" mode="aspectFill" />
  11. </view>
  12. <view class="tip">(微信扫描二维码即可进入)</view>
  13. </view>
  14. <view class="user-info">
  15. <view class="header-name">
  16. <u-avatar size="60" :src="$userInfo.portraitFile && $userInfo.portraitFile.staticPath" />
  17. <text>{{ $userInfo.userName }}</text>
  18. </view>
  19. <u--image :showLoading="true" :src="goodsIcon" width="100rpx" height="100rpx" />
  20. </view>
  21. </view>
  22. <view class="share-bot" @click.stop>
  23. <view class="share-btns">
  24. <view class="btns-list" @click="onSaveImg">
  25. <u--image :showLoading="true" :src="copyLink" width="72rpx" height="72rpx" />
  26. <text>复制链接</text>
  27. </view>
  28. <view class="btns-list" @click="onSaveImg">
  29. <u--image :showLoading="true" :src="saveIcon" width="72rpx" height="72rpx" />
  30. <text>保存海报</text>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </u-popup>
  36. </template>
  37. <script>
  38. import {
  39. mapState
  40. } from 'vuex'
  41. export default {
  42. components: {},
  43. mixins: [],
  44. props: {
  45. show: {
  46. type: Boolean,
  47. default: false
  48. },
  49. imgUrl: {
  50. type: String,
  51. default: ''
  52. },
  53. storeInfo: {
  54. type: Object,
  55. default: () => {}
  56. }
  57. },
  58. data() {
  59. return {
  60. saveIcon: require('../../static/download.png'),
  61. copyLink: require('../../static/copyLink.png'),
  62. goodsIcon: require('../../static/goodsIcon.png'),
  63. settingWritePhotosAlbum: false
  64. }
  65. },
  66. watch: {},
  67. computed: {
  68. ...mapState(['$userInfo'])
  69. },
  70. created() {},
  71. methods: {
  72. handleClose() {
  73. this.$emit("update:show", false)
  74. this.$emit('close')
  75. },
  76. saveImageToPhotosAlbumFun() {
  77. const that = this;
  78. uni.downloadFile({
  79. url: that.storeInfo.merchantIconStaticPath, //图片的地址
  80. success: function(res) {
  81. const tempFilePath = res.tempFilePath //通过res中的tempFilePath 得到需要下载的图片地址
  82. uni.saveImageToPhotosAlbum({
  83. filePath: tempFilePath,
  84. success: () => {
  85. uni.hideLoading();
  86. uni.showToast({
  87. title: '保存成功'
  88. });
  89. },
  90. fail: () => {
  91. uni.showToast({
  92. title: '保存失败',
  93. icon: 'error'
  94. });
  95. },
  96. complete: res => {
  97. uni.hideLoading();
  98. }
  99. });
  100. }
  101. })
  102. },
  103. // 保存图片
  104. async onSaveImg() {
  105. const that = this;
  106. // #ifdef MP-WEIXIN
  107. uni.showLoading({
  108. title: '二维码下载中'
  109. });
  110. uni.getSetting({
  111. success: res => {
  112. if (res.authSetting['scope.writePhotosAlbum']) {
  113. that.saveImageToPhotosAlbumFun()
  114. } else {
  115. uni.authorize({
  116. scope: 'scope.writePhotosAlbum',
  117. success: () => {
  118. that.saveImageToPhotosAlbumFun()
  119. },
  120. fail: () => {
  121. uni.showModal({
  122. title: '提示',
  123. content: '请先在设置页面打开“保存相册”使用权限',
  124. confirmText: '去设置',
  125. cancelText: '算了',
  126. success: data => {
  127. if (data.confirm) {
  128. uni.openSetting();
  129. }
  130. uni.hideLoading();
  131. }
  132. });
  133. },
  134. complete: res => {
  135. uni.hideLoading();
  136. }
  137. });
  138. }
  139. }
  140. });
  141. // #endif
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss">
  147. .shareModel {
  148. width: 100%;
  149. background-color: #F5F5F5;
  150. color: #2D2D2D;
  151. .title {
  152. padding: 32rpx;
  153. text-align: center;
  154. font-size: 32rpx;
  155. font-weight: bold;
  156. }
  157. .share-container {
  158. margin: 0 80rpx 24rpx;
  159. padding: 32rpx;
  160. height: 100%;
  161. background: rgba(255, 255, 255, 0.39);
  162. box-shadow: 0 6rpx 20rpx rgba(110, 108, 108, 0.1);
  163. border-radius: 24rpx;
  164. .merchant-info {
  165. display: flex;
  166. flex-direction: column;
  167. justify-content: center;
  168. align-items: center;
  169. .merchantName {
  170. margin-bottom: 48rpx;
  171. font-weight: bold;
  172. font-size: 32rpx;
  173. }
  174. .tip {
  175. padding: 24rpx 0 48rpx;
  176. color: #999999;
  177. font-size: 24rpx;
  178. }
  179. .store-img {
  180. padding: 20rpx;
  181. background-color: #F5F5F5;
  182. .img {
  183. width: 380rpx;
  184. height: 380rpx;
  185. }
  186. }
  187. }
  188. .user-info {
  189. display: flex;
  190. align-items: center;
  191. justify-content: space-between;
  192. .header-name {
  193. display: flex;
  194. align-items: center;
  195. text {
  196. margin-left: 12rpx;
  197. }
  198. }
  199. }
  200. }
  201. .share-bot {
  202. width: 100%;
  203. background: #FFFFFF;
  204. .share-btns {
  205. display: flex;
  206. justify-content: space-around;
  207. align-items: center;
  208. width: 100%;
  209. padding: 30rpx 0;
  210. .btns-list {
  211. display: flex;
  212. flex-direction: column;
  213. justify-content: center;
  214. align-items: center;
  215. text {
  216. color: #2D2D2D;
  217. font-size: 28rpx;
  218. margin-top: 8rpx;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/572257
推荐阅读
相关标签
  

闽ICP备14008679号