The WeChat applet in uniapp obtains the nickname of the user's avatar

Announcement on Adjustment of Mini Program User Avatar Nickname Acquisition Rules
It means that after 24:00 on October 25, 2022, wx.getUserProfile user avatars will be uniformly returned to the default gray avatar, and nicknames will be uniformly returned to "WeChat users"

Latest: user nickname, avatar acquisition rules

<template>
	<view class="login">
		<view class="userinfo">
			<view class="avatar">
				<image :src="avatarUrl ? avatarUrl : '../../../static/logo2.png'" mode="" class="img"></image>
				<button class="bg" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">获取头像</button>
				<image src="../../../static/upimg.png" mode="" class="upimg"></image>
			</view>
			<view class="item">
				<view class="label">昵称</view>
				<input type="nickname" class="weui-input" placeholder="请输入昵称" v-model="nickname"/>
			</view>
			<button class="custom-style" @click="submit">提交</button>
		</view>
		
		
	</view>
</template>

<script>
	import {
      
      
		getUserAvatar
	} from '@/config/login.js'
	export default {
      
      
		data() {
      
      
			return {
      
      
				code:'',
				nickname:'',
				avatarUrl:'',
				loading:false,
			}
		},
		onLoad(option) {
      
      
			console.log('option',option);
		},
		methods: {
      
      
			onChooseAvatar(res){
      
      
				// 必须转成base64
				this.avatarUrl = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(res.detail.avatarUrl,'base64')
			},
			submit(){
      
      
				if(!this.avatarUrl){
      
      
					uni.showToast({
      
      
						title: '请选择用户头像',
						icon: 'none'
					});
				}else{
      
      
					getUserAvatar({
      
      
						avatar: this.avatarUrl,
						nickname: this.nickname,
					}).then(res => {
      
      
						this.$message('登录成功')
						setTimeout(()=>{
      
      
							this.JumpPage('/pages/index/index')
						},1500)
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.login{
      
      
		min-height: 100vh;
		background: #FAF9F9;
		padding-top: 100rpx;
		.custom-style{
      
      
			display: flex;
			align-items: center;
			justify-content: center;
			width: 690rpx;
			height: 88rpx;
			background: #F22522;
			border-radius: 44rpx;
			font-size: 28rpx;
			color: #fff;
			margin: 70rpx auto 40rpx;
		}
		.userinfo{
      
      
			.avatar{
      
      
				width: 196rpx;
				height: 196rpx;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				margin: 0 auto 60rpx;
				.img{
      
      
					width: 196rpx;
					height: 196rpx;
					border-radius: 50%;
				}
				.bg{
      
      
					position: absolute;
					left: 0;
					top: 0;
					width: 200rpx;
					height: 200rpx;
					opacity: 0;
				}
				.upimg{
      
      
					width: 60rpx;
					height: 60rpx;
					position: absolute;
					right: 0;
					bottom: 0;
				}
			}
			.item{
      
      
				width: 690rpx;
				height: 88rpx;
				background: #FFFFFF;
				border-radius: 44rpx;
				display: flex;
				align-items: center;
				margin: auto;
				padding: 0 30rpx;
				.label{
      
      
					font-size: 28rpx;
					margin-right: 30rpx;
				}
			}
		}
	}
</style>

onChooseAvatarThe method must be this.avatarUrl = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(res.detail.avatarUrl,'base64')converted to base64 storage.
The avatar obtained by the applet developer tool is http, and the path to the mobile phone is wxfile://tmp_, which imagewill not be displayed in the label. So here it is converted to base64 to store data

Guess you like

Origin blog.csdn.net/qq_40745143/article/details/131686323