uniapp——操作成功返回首页

博客主要提及操作成功后返回首页的相关内容,展示了对应的界面,并给出了相关代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、操作成功返回首页

界面如下:
在这里插入图片描述

代码:

<template>
	<view class="view_contain">
		<view class="view_iconText">
			<icon type="success" size="100" color="#39b54a" />
			<text class="text_title">操作成功</text>
			<text class="text_content">恭喜您操作成功!请点击返回首页。</text>
		</view>
		<button class="button_gotohome" @tap="goto_Home">返回首页</button>
		<view class="bottom">
			<text style="color: #007AFF;"> 莉华电子科技公司所有</text>
			<text style="color: #999999;">@ The copyright belongs to lihua</text>
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			goto_Home() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
	}

	.view_contain {
		width: 100%;
		height: 100%;
		margin-top: 50upx;
	}

	.view_iconText {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 30%;
		align-items: center;
		justify-content: center;
	}
	.text_title{
		margin-top: 50upx;
		font-size: 40upx;
		font-weight: 600;
	}
	.button_gotohome{
		width: 50%;
		background: linear-gradient(to right, #74bba7, #28b53d);
		border-radius: 50upx;
		color: #ffffff;
		box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.35);
		margin-top: 100upx;
	}
	.text_content{
		margin-top: 20upx;
		color: #557ead;
		font-size: 25upx;
	}
	.bottom{
		width: 100%;
		display: flex;
		flex-direction: column;
		position: fixed;
		bottom: 20upx;
		align-items: center;
		justify-content: center;
	}
	
</style>

### 如何在 UniApp 中实现将后端返回的图片保存到手机本地 #### 一、设置请求参数与接收数据 当从后端获取图片资源时,需确保网络请求配置正确。例如,在发送 HTTP 请求时应指定 `responseType` 为 `'arraybuffer'`,以便能够接收到二进制格式的数据[^1]。 以下是发起请求的一个简单示例: ```javascript import request from '@/utils/request'; export function fetchImage() { return request({ method: 'get', url: '/api/get-image', // 替换为实际接口地址 responseType: 'arraybuffer' }); } ``` #### 二、转换图片数据至 Base64 编码 从前端角度出发,接收到的二进制数据需要进一步处理成可识别的形式——即 Base64 字符串。这可以通过 JavaScript 的内置工具完成,具体过程如下所示[^2]: ```javascript function arrayBufferToBase64(buffer) { let uint8Array = new Uint8Array(buffer); let charCodes = []; for (let i = 0; i < uint8Array.length; ++i) { charCodes.push(String.fromCharCode(uint8Array[i])); } return btoa(charCodes.join('')); } // 假设 res 是请求响应对象 const base64Data = 'data:image/png;base64,' + arrayBufferToBase64(res.data); ``` #### 三、调用 API 完成图片保存功能 UniApp 提供了一套跨平台的方法用于操作设备存储空间,其中就包含了保存图片的能力。下面展示的是利用这些能力的具体代码片段: ```javascript uni.saveImageToPhotosAlbum({ filePath: base64Data, success: () => { console.log('图片已成功保存'); }, fail: err => { console.error('保存失败:', err); } }); ``` 需要注意的一点是,上述例子中的 `filePath` 应该是指向合法路径或者有效的 Base64 数据流;如果遇到权限问题,则可能还需要额外申请访问相册的相关授权^. #### 四、综合案例分析 假设我们已经完成了以上三个部分的工作,那么最终可以得到这样一个完整的解决方案框架: ```javascript async function saveBackendImageToLocal() { try { const response = await fetchImage(); if (!response || !response.data) throw new Error("未获得有效图像数据"); const imageData = arrayBufferToBase64(response.data); uni.saveImageToPhotosAlbum({ filePath: `data:image/png;base64,${imageData}`, success: () => { uni.showToast({ title: '图片保存成功!', icon: 'success'}); }, fail: error => { uni.showToast({ title: '图片保存失败...', icon: 'none'}); console.error(error); } }); } catch(e){ console.error('发生错误:', e.message); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy313622821

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值