【使用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>
//样式的话就不给大家啦,如果大家需要的话那就在评论区留言吧,随时恭候
相关推荐
小徐_23334 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮9 小时前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw510 小时前
uni-app中v-if使用”异常”
前端·uni-app
!win !10 小时前
uni-app中v-if使用”异常”
前端·uni-app
Emma歌小白11 小时前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子12 小时前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_9159184113 小时前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张14 小时前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
00后程序员张1 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬1 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app