【使用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>
//样式的话就不给大家啦,如果大家需要的话那就在评论区留言吧,随时恭候
相关推荐
一铭1119916 小时前
微信小程序可视化搭建实战:模板、组件与发布验收
前端·低代码·微信小程序·小程序·页面设计·可视化搭建·发布验收
愚公搬代码19 小时前
【愚公系列】《微信小程序项目实战(AI编程+视频图解)》049-项目实战:使用 AI 编程工具辅助实现图书分类页面
人工智能·微信小程序·ai编程
zhoulcc20 小时前
为什么 uni-app 小程序里并排选项总变竖排?缺了 box-sizing 重置
前端·css·小程序·uni-app·box-sizing
千寻技术帮21 小时前
基于微信小程序的装修管理系统(源码+脚本+文档)50032
数据库·spring boot·微信小程序·毕业设计·装修管理
千寻技术帮21 小时前
医院挂号小程序(源码+脚本+文档)50031
mysql·微信小程序·小程序·毕业设计·医院管理·医院挂号
愚公搬代码1 天前
【愚公系列】《微信小程序项目实战(AI编程+视频图解)》047-设备应用 API
微信小程序·音视频·ai编程
用户112936112192 天前
没写样式为什么还找我要 wxss?uni-app 这个 ENOENT 坑多数人误判了
前端·微信小程序·uni-app
源码手记2 天前
基于微信小程序的流浪动物救助与领养系统-毕设源码
微信小程序·小程序·课程设计
拖孩2 天前
群里有人开始比「次数」之后,我做了个排行榜小程序
前端·后端·微信小程序
长春小霸王2 天前
uniapp仿KFC点餐(套餐)界面
uni-app