index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view>
  3. <view>
  4. <image show-menu-by-longpress="true" :src=url ></image>
  5. </view>
  6. <button class="downBtn" @tap="savePoster()">图像下载</button>
  7. <view class="progress-container" v-if="isShowProgress">
  8. <view class="progress-box">
  9. <view class="text">文件下载中,请稍后...</view>
  10. <progress :percent="progress" show-info stroke-width="3" />
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. url: 'http://125.71.216.35:9001/dcm/27/image-00001.png',
  20. //url: 'http://125.71.216.35:9001/dcm/27/222.mp4',
  21. isShowProgress: false,
  22. progress: 0,
  23. }
  24. },
  25. methods: {
  26. savePoster() {
  27. uni.getSetting({ //获取用户的当前设置
  28. success: (res) => {
  29. if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
  30. //this.saveImageToPhotosAlbum();
  31. this.downloadFile()
  32. } else {
  33. uni.authorize({ //如果没有授权,向用户发起请求
  34. scope: 'scope.writePhotosAlbum',
  35. success: () => {
  36. //this.saveImageToPhotosAlbum();
  37. this.downloadFile()
  38. },
  39. fail: () => {
  40. uni.showToast({
  41. title: "请打开保存相册权限,再点击图像下载",
  42. icon: "none",
  43. duration: 3000
  44. });
  45. setTimeout(() => {
  46. uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
  47. success: (res2) => {
  48. // console.log(res2.authSetting)
  49. }
  50. });
  51. }, 3000);
  52. }
  53. })
  54. }
  55. }
  56. })
  57. },
  58. downloadFile() {
  59. const downloadTask = uni.downloadFile({
  60. url: this.url,
  61. success: res => {
  62. if (res.statusCode === 200) {
  63. this.isShowProgress = false;
  64. //console.log('下载成功');
  65. }
  66. let that = this;
  67. //uni.saveFile({
  68. // tempFilePath: res.tempFilePath,
  69. uni.saveImageToPhotosAlbum({
  70. filePath: res.tempFilePath,
  71. success: function(red) {
  72. //console.log(red);
  73. that.isShowProgress = false;
  74. uni.showToast({
  75. title: '图像已存入相册',
  76. //将值设置为 success 或者直接不用写icon这个参数
  77. icon: 'success',
  78. //显示持续时间为 2秒
  79. duration: 3000
  80. })
  81. }
  82. });
  83. }
  84. })
  85. downloadTask.onProgressUpdate((res) => {
  86. if(res.progress > 0) {
  87. this.isShowProgress = true;
  88. }
  89. this.progress = res.progress;
  90. //console.log('下载进度:' + res.progress);
  91. //console.log('已下载长度:' + res.totalBytesWritten);
  92. //console.log('文件总长度:' + res.totalBytesExpectedToWrite);
  93. })
  94. },
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. page{
  100. height: 100%;
  101. background-color: #000;
  102. }
  103. .downBtn{
  104. margin-top: 50%;
  105. width: 30%;
  106. height: 70rpx;
  107. display: flex;
  108. align-items: center;
  109. justify-content: center;
  110. color: #ffffff;
  111. background: #05c86d;
  112. font-size: 32rpx;
  113. border-radius: 50rpx;
  114. }
  115. .progress-container{
  116. position: fixed;
  117. top: 0;
  118. left: 0;
  119. z-index: 99;
  120. background: rgba(0, 0, 0, .2);
  121. width: 750rpx;
  122. height: 100vh;
  123. display: flex;
  124. align-items: center;
  125. justify-content: center;
  126. .progress-box{
  127. background: #FFFFFF;
  128. border-radius: 20rpx;
  129. padding: 30rpx;
  130. .text{
  131. margin-bottom: 20rpx;
  132. }
  133. }
  134. }
  135. </style>