• vue3+ts+uniapp(微信小程序)---- 点击按钮保存图片的功能


    vue3+ts+uniapp(微信小程序)---- 点击按钮保存图片的功能

    描述:后台会给一张二维码图片,用户点击保存按钮即可保存图片到相册。
    注意:
    1)图片要是https形式;
    2)要在微信公众平台中更新隐私协议,添加相册写入授权;
    3)要在微信公众平台中开发设置中配置服务器域名中的downloadFile合法域名。

    1. 封装保存图片功能的ts,名称为downloadFile.ts
    /*
     * @Description: 点击按钮保存图片
     */
    
    //引导用户开启权限
    const isAuth = () => {
        uni.showModal({
            content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权。',
            success: (res) => {
                if (res.confirm) {
                    uni.openSetting({
                        success: (result) => {
                            console.log(result.authSetting)
                        }
                    })
                }
            }
        })
    }
    
    //保存网络图片到本地
    const saveNetImageToLocal = (url: string) => {
        uni.downloadFile({
            url: url,
            success: (res) => {
                if (res.statusCode === 200) {
                    uni.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success: () => {
                            setTimeout(() => {
                                uni.$u.toast('保存成功!')
                            }, 1000)
                        },
                        fail: (err) => {
                            console.log(err.errMsg)
                            uni.$u.toast(err.errMsg)
                        },
                        //无论成功失败都走的回调
                        complete: () => {
                            uni.hideLoading()
                        }
                    })
                } else {
                    uni.$u.toast('网络错误!')
                }
            }
        })
    }
    
    /**
     * @description: 点击保存图片
     * @return {*}
     */
    
    export default function downloadFile(url: string) {
        uni.showLoading({
            title: '正在保存图片...'
        })
        //向用户发起授权请求
        uni.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => {
                //授权成功保存图片到系统相册
                uni.hideLoading()
                saveNetImageToLocal(url)
            },
            //授权失败
            fail: () => {
                uni.hideLoading()
                uni.$u.toast('未授权保存图片到相册!')
                //获取用户的当前设置。获取相册权限
                uni.getSetting({
                    success: (res) => {
                        uni.$u.toast(res)
                        //如果没有相册权限
                        if (!res.authSetting['scope.writePhotosAlbum']) {
                            isAuth()
                        }
                    },
                    complete: () => {
                        uni.hideLoading()
                    }
                })
            }
        })
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    1. 在页面中进行调用
    <template>
       <view class="py-[30rpx] px-[30rpx] box-border w-[600rpx]">
        <view class="text-xl font-bold text-center mb-[40rpx]">分享二维码</view>
       	<view class="w-[400rpx] h-[400rpx] m-auto">
             <u-image width="400rpx" height="400rpx" :src="shareCodeImg" closeable></u-image>
        </view>
         <view class="w-[300rpx] m-auto mt-[40rpx]">
             <u-button type="primary" @click="downloadImg(shareCodeImg)">保存图片</u-button>
         </view>
     </view>
    </template>
    <script setup lang="ts">
    import { ref } from 'vue'
    import downloadFile from '@/hooks/downloadFile.ts'
    const shareCodeImg = ref('') // 二维码图片
    /**
     * @description: 点击按钮保存图片
     * @param {*} url:图片路径
     * @return {*}
     */
    const downloadImg = (url) => {
        downloadFile(url)
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    PAT 1061 Dating
    java语言程序设计教程pdf,java面试简历
    C#进程调用FFmpeg操作音视频
    计算Qt中的QAudioOutput缓冲区未播放的音频字节数对应时长
    常见首屏优化
    Canvas 画布
    airlearning-ue4安装的踩坑记录
    原来Linux这么牛:称霸全球超级电脑 500 强!
    【C++修炼之路】1. 初窥门径
    如何解决.NET8 类库Debug时,Debug文件夹中不包含Packages中引入的文件
  • 原文地址:https://blog.csdn.net/heavenz19/article/details/133804676