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. 在页面中进行调用
<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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建一个使用vue3+ts+vite+uniapp微信小程序的步骤如下: 1. 首先安装最新版的Node.js和npm。 2. 安装uni-app-cli脚手架工具,命令如下: ``` npm install -g @vue/cli npm install -g @vue/cli-init npm install -g @dcloudio/uni-cli ``` 3. 创建一个uni-app项目,命令如下: ``` vue create -p dcloudio/uni-preset-vue my-project ``` 4. 进入项目目录,安装依赖包,命令如下: ``` cd my-project npm install ``` 5. 安装并配置微信小程序开发者工具,下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 6. 在微信小程序开发者工具中,选择导入uni-app项目,选择项目目录下的dist/dev/mp-weixin文件夹,导入后即可进行开发和调试。 7. 如果需要使用vue3和typescript,在项目中安装相关依赖包,命令如下: ``` npm install --save-dev vue@next @vue/compiler-sfc typescript ts-loader ``` 8. 在项目根目录下创建vue.config.js文件,配置如下: ``` module.exports = { chainWebpack: config => { config.module .rule('ts') .use('ts-loader') .loader('ts-loader') .tap(options => { options.appendTsSuffixTo = [/\.vue$/] return options }) } } ``` 9. 在src目录下创建shims-vue.d.ts文件,内容如下: ``` declare module "*.vue" { import { ComponentOptions } from "vue"; const component: ComponentOptions; export default component; } ``` 10. 现在你就可以使用vue3和typescript进行开发了。同时,如果需要使用vite进行开发,可以参考uni-app官方文档进行配置:https://uniapp.dcloud.io/collocation/vite 以上就是使用vue3+ts+vite+uniapp搭建微信小程序的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值