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>
相关推荐
艾小逗39 分钟前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
程序员阿龙40 分钟前
【2025】儿童疫苗接种预约小程序(源码+文档+解答)
小程序·毕业设计·小程序开发·预约小程序·疫苗管理小程序·出勤数据分析·智能考勤
818源码资源站1 小时前
表情包创作、取图小程序端(带流量主)
小程序
2401_8459375310 小时前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
程序员入门进阶12 小时前
基于微信小程序的科创微应用平台设计与实现+ssm(lw+演示+源码+运行)
微信小程序·小程序
鸭子嘎鹅子呱15 小时前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社20 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
DreamByte21 小时前
Python Tkinter小程序
开发语言·python·小程序
说私域1 天前
开源 AI 智能名片小程序:开启内容营销新境界
人工智能·小程序
汇匠源1 天前
零工市场小程序:保障灵活就业
java·小程序·零工市场