vue3+uniapp实现头像上传功能

6 篇文章 0 订阅
<template>
	<view class="userInfo-contanier">
		<view class="userInfo-detail" @click="chooseImage">
			<text>头像</text>
			<view>
				<image :src="userInfo.avatar"></image>
				<text>></text>
			</view>
		</view>
	</view>
</template>
import {
		getCurrentInstance
	} from "vue";
const {
			proxy
		} = getCurrentInstance();
let token = uni.getStorageSync('token');

选择头像

// 选择头像
			function chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
					//获取选择上传图片的路径
						console.log(JSON.stringify(res.tempFilePaths));
						//执行上传图片这个方法
						sendImage(res.tempFilePaths)
					}
				});
			}

上传头像

// 上传头像
			function sendImage(option) {
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: "api",//上传头像的请求地址
					filePath: option[0],//上传图片的路径
					name: 'file',
					formData: {
						token
					},
					success: (res) => {
						console.log(res);
						let update = JSON.parse(res.data);//获取上传成功后的数据(注意转为json格式)
						console.log(update);
						proxy.$http({
							url: 'api',//修改头像的请求地址
							method: 'get',
							data: {
								avatar: update.data.fullurl//上传成功后的图片路径(注意前面要转成json格式,否则会找不到)
							}
						}).then(res => {
							//修改成功后的操作
							uni.hideLoading()
						})

					}
				});
			}

注:proxy.$http({})是自己对请求进行了简易的封装,如果需要,请跳转至链接: uniapp+vue3,对请求的简单封装配置以及使用

完整代码

<template>
	<view class="userInfo-contanier">
		<view class="userInfo-detail" @click="chooseImage">
			<text>头像</text>
			<view>
				<image :src="userInfo.avatar"></image>
				<text>></text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		getCurrentInstance
	} from "vue";
	export default {
		setup() {
			let userInfo = ref(uni.getStorageSync('userInfo'));
			let token = uni.getStorageSync('token');
			const {
				proxy
			} = getCurrentInstance();
			onMounted(() => {
				console.log(userInfo.value)

			});
			// 选择头像
			function chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						sendImage(res.tempFilePaths)
					}
				});
			}
			// 上传头像
			function sendImage(option) {
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: "api",//上传头像的请求地址
					filePath: option[0],
					name: 'file',
					formData: {
						token
					},
					success: (res) => {
						console.log(res);
						let update = JSON.parse(res.data);
						console.log(update);
						proxy.$http({
							url: 'api',//修改头像的请求地址
							method: 'get',
							data: {
								avatar: update.data.fullurl
							}
						}).then(res => {
							uni.showToast({
								title: res.data.msg,
								duration: 2000,
								icon: "none"
							});
							if (res.data.code == 1) {
								uni.setStorageSync("userInfo", res.data.data);
							}
							uni.hideLoading()
						})

					}
				});
			}
			return {
				userInfo,
				chooseImage
			}
		}
	}
</script>

<style lang="scss" scoped>
	.userInfo-detail {
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid #f2f2f2;
		padding: 30rpx;
		color: gray;

		view {
			display: flex;
			align-items: center;

			text {
				margin-left: 20rpx;
			}

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50rpx;
			}
		}
	}
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue3+uni-app实现微信小程序登录流程主要分为以下步骤: 1. 在uni-app项目中安装并引入微信小程序登录插件(如wxlogin),在main.js中初始化并配置插件参数。 2. 创建一个登录页面,该页面包含点击按钮触发微信登录的操作,可以通过uni.login()方法调用微信小程序登录接口获取code。 3. 接收到微信小程序登录接口返回的code后,将code发送给后端服务器,后端服务器将code和小程序的App ID以及App Secret发送给微信服务器进行登录凭证校验,获取到session_key和openid。 4. 服务器根据openid和session_key生成一个自定义的token,返回给前端。 5. 前端将token保存在本地,使用uni.setStorage()方法进行存储,以便后续的登录状态维持和接口请求验证。 6. 在需要登录验证的页面或组件中,通过uni.getStorage()方法获取本地存储的token,并将token添加到请求头中,发送给后端服务器进行接口请求。 7. 后端服务器接收到带有token的请求,对token进行校验和解析,验证token是否有效,从而确保用户的登录状态。 总结:通过以上步骤,实现Vue3+uni-app微信小程序的登录流程。用户通过点击按钮触发微信小程序登录接口,后端服务器校验登录凭证,生成token并返回给前端前端保存token并在请求接口时携带token进行验证,保证了用户的登录状态和接口访问权限的安全性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值