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

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

描述:后台会给一张二维码图片,用户点击保存按钮即可保存图片到相册。

注意:

1)图片要是https形式;

2)要在微信公众平台中更新隐私协议,添加相册写入授权;

3)要在微信公众平台中开发设置中配置服务器域名中的downloadFile合法域名。

  1. 封装保存图片功能的ts,名称为downloadFile.ts
javascript 复制代码
/*
 * @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. 在页面中进行调用
javascript 复制代码
<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>
相关推荐
秋水丶秋水11 小时前
小程序为什么要安装SSL安全证书
安全·小程序·ssl
^Rocky14 小时前
微信小程序(uniapp)实现腾讯云 IM 消息撤回
微信小程序·uni-app·腾讯云
疯狂的沙粒16 小时前
uniapp开发企业微信小程序时 wx.qy.login 在uniapp中使用的时候,需要导包吗?
前端·javascript·微信小程序·小程序·uni-app
~央千澈~19 小时前
UniApp X:鸿蒙原生开发的机会与DCloud的崛起之路·优雅草卓伊凡
uni-app·uniapp
10990541820 小时前
微信小程序进阶第2篇__事件类型_冒泡_非冒泡
微信小程序
唐人街都是苦瓜脸20 小时前
uni-app 提供的页面跳转方法详细解释及其区别
前端·uni-app
疯狂的沙粒1 天前
uniapp 开发企业微信小程序时,如何在当前页面真正销毁前或者关闭小程序前调用一个api接口
微信小程序·小程序·uni-app
山河故人1631 天前
UniApp微信小程序自定义导航栏实现
微信小程序·uni-app·notepad++
魔术师ID1 天前
微信小程序学习目录
学习·微信小程序·小程序
一蓑烟雨,一任平生1 天前
在h5端实现录音发送功能(兼容内嵌微信小程序) recorder-core
微信小程序·小程序