uniapp2023年微信小程序头像+昵称分别获取

1、DOM

javascript 复制代码
<view class="m-user">
	<view class="user-info">
		<!--头像 GO-->
		<button class="avatar avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"
			slot="right">
			<image mode="aspectFill" src="默认的空图" v-if="!userinfo.avatar"></image>
			<image mode="aspectFill" :src="userinfo.avatar" v-else></image>
		</button>
		<!--昵称  GO-->
		<view class="nickname">
			<view class="tit" v-if="userinfo.nickname">
				<input  type="nickname" style="font-size:28rpx;flex: 1;" placeholder="请输入昵称"
					v-model="userinfo.nickname" @blur="getNickname" />
				<view class="font-24 mt40">
					{{userinfo.username}}
				</view>
			</view>
			<view class="tit" v-else>
				<text class="auth-login" @click="$zeropo.to(`/pages/users/login`)">授权登录</text>
			</view>
		</view>
	</view>
</view>

3、JS

javascript 复制代码
data() {
	return {
		HTTP_URL: this.$HTTP_URL,//服务器域名,按自己的项目修改
		userinfo: {
			nickname: '',//昵称
			username: '',//微信授权的字符
			avatar: '',//头像
		},
	}
},
/*methods*/
// 获取头像
onChooseAvatar(e) {
	let token = uni.getStorageSync('token')
	uni.showLoading()
	uni.uploadFile({
		url: this.HTTP_URL + '/common/upload',//上传接口,按自己的项目修改
		header: {
			token: token
		},
		filePath: e.detail.avatarUrl,
		name: 'file',
		success: (res) => {
			const r = JSON.parse(res.data);
			this.$api.post({
				url: '/user/profile',//更新用户信息的接口,按自己的项目修改
				data: {
					avatar: this.HTTP_URL + r.data.fileName,
				},
				success: re => {
					setTimeout(function() {
						uni.showToast({
							title: "设置成功"
						})
						uni.hideLoading()
					}, 1000)
					uni.stopPullDownRefresh();
				},
				fail: err => {

				},
			});

		},
		fail: (e) => {
			console.log('e', e)
		}
	});

},
// 获取昵称
getNickname(e) {
	this.nickname = e.detail.value
	this.$api.post({
		url: '/user/profile',更新用户信息的接口,按自己的项目修改
		data: {
			nickname: this.nickname,
		},
		success: re => {
			setTimeout(function() {
				uni.showToast({
					title: "设置成功"
				})
				uni.hideLoading()
			}, 1000)
			uni.stopPullDownRefresh();
			this.getUserinfo();//刷新用户信息,没写这个方法,按自己的项目修改,如果是表单页面则去掉
		},
		fail: err => {
			console.log('e', err)
		},
	});
},

3、CSS

css 复制代码
.m-user {
	height: 150rpx;
	padding: 30rpx 30rpx 0;
	border-bottom: 1rpx solid #f7f7f7;
	.user-info {
		display: flex;
		align-items: center;
		justify-content: space-between;
		.avatar {
			width: 130rpx;
			width: 130rpx;
			border: 0;
			background: none;
			padding: 0;
			margin: 0;
			line-height: 0;

			image {
				width: 130rpx;
				height: 130rpx;
				border-radius: 10rpx;
				overflow: hidden;
			}
		}

		.nickname {
			width: 550rpx;
			height: 130rpx;
			text-align: left;
			float: left;
			.tit {
				font-size: 30rpx;
				width: 100%;
				color: #1d363a;
				float: left;
				.auth-login {
					width: 140rpx;
					height: 50rpx;
					font-size: 26rpx;
					background: #fffae7;
					float: right;
					line-height: 50rpx;
					border-radius: 25rpx;
					text-align: center;
					color: #fecf5d;
				}

			}

		}

	}

}

3、效果图


4、发布申请隐私后,返回结果

发布申请隐私后,返回头像结果!

发布申请隐私后,返回头像结果!

发布申请隐私后,返回头像结果!

相关推荐
说私域8 小时前
公域流量向私域流量转化策略研究——基于开源AI智能客服、AI智能名片与S2B2C商城小程序的融合应用
人工智能·小程序
半生过往8 小时前
微信小程序文件下载与预览功能实现详解
微信小程序·小程序·notepad++·压缩包下载解压
源码_V_saaskw8 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
weixin_lynhgworld11 小时前
淘宝扭蛋机小程序系统开发:重塑电商互动模式
大数据·小程序
996幸存者13 小时前
uni-app区域选择、支持静态、动态数据
微信小程序·uni-app
ᥬ 小月亮14 小时前
Uniapp编写微信小程序,绘制动态圆环进度条
微信小程序·小程序·uni-app
The_era_achievs_hero21 小时前
UniappDay03
vue.js·微信小程序·uni-app
说私域1 天前
技术赋能与营销创新:开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化路径研究
人工智能·小程序·开源
游戏开发爱好者82 天前
没有 Mac,如何上架 iOS App?多项目复用与流程标准化实战分享
android·ios·小程序·https·uni-app·iphone·webview
weixin_lynhgworld2 天前
代驾小程序系统开发:引领出行行业数字化转型
小程序