【使用uniapp完成微信小程序的图片下载到本机】

使用uniapp完成微信小程序的图片下载到本机

话不多说直接上代码吧

使用的vue3的语法糖进行完成的

因为我是请求的后端接口

复制代码
<template>
	<view class="load">
		<view class="selectPart">
			<Select></Select>
		</view>
		<view class="PhotoPart">
			<image :src="image" mode=""></image>
		</view>
		<view class="btnPart">
			<button class="btnOne">
				<image src="../../static/images/mo.png" mode=""></image>
				<text>更像我</text>
			</button>
			<button class="btnTwo" @click="downloadPhoto">下载 ¥4.8</button>
		</view>
	</view>
</template>

<script setup>
	import { ref, watch, computed, onMounted } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import Select from '../components/select.vue'
	import { getCreatePhotoList } from '../../api/index.ts'

	// 图片ID
	const photoID = ref(null)
	
	// 图片
	const image = ref('')
	
	//获取上一个页面路由传递的参数
	onLoad((option) => {
		photoID.value = Number(option.photoID)
		getImageList()
	})

	// 获取生成的照片
	const getImageList = () => {
		const data = {
			id: photoID.value
		}
		getCreatePhotoList(data).then((res) => {
			image.value = res.data.items[0].file_path
		})
	}

	// 下载图片按钮
	const downloadPhoto = () => {
		uni.downloadFile({
			url: image.value,
			success(res) {
				if (res.statusCode === 200) {
					// 下载成功,保存到相册
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath,
						success() {
							uni.showToast({
								title: '保存成功',
								icon: 'success'
							});
						},
						fail() {
							uni.showToast({
								title: '保存失败',
								icon: 'none'
							});
						}
					});
				} else {
					uni.showToast({
						title: '下载失败',
						icon: 'none'
					});
				}
			},
			fail() {
				uni.showToast({
					title: '下载失败',
					icon: 'none'
				});
			}
		});
	};
</script>
//样式的话就不给大家啦,如果大家需要的话那就在评论区留言吧,随时恭候
相关推荐
2501_915918412 小时前
只有 Flutter IPA 文件,通过多工具组合完成有效混淆与保护
android·flutter·ios·小程序·uni-app·iphone·webview
计算机毕设指导67 小时前
基于微信小程序的丽江市旅游分享系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·旅游
2501_915918419 小时前
除了 Perfdog,如何在 Windows 环境中完成 iOS App 的性能测试工作
android·ios·小程序·https·uni-app·iphone·webview
丢,捞仔10 小时前
uni-app上架应用添加权限提示框
前端·javascript·uni-app
程序媛徐师姐10 小时前
Java基于微信小程序的鲜花销售系统,附源码+文档说明
java·微信小程序·鲜花销售小程序·java鲜花销售小程序·鲜花销售微信小程序·java鲜花销售系统小程序·java鲜花销售微信小程序
雪芽蓝域zzs11 小时前
uniapp富文本rich-text
uni-app
難釋懷11 小时前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
Qlittleboy12 小时前
uniAPP报错:v-for 暂不支持循环数据: (env: Windows,mp,1.06.2307260; lib: 3.12.0)
uni-app
曾帅16812 小时前
uniapp安卓启动图
android·opencv·uni-app
m0_7408596212 小时前
解决uniapp折叠面板报错this.collapse.onChange is not a function
uni-app