uniapp开发微信小程序——微信登录,获取头像和昵称

点击未授权,唤起弹窗

 弹窗中获取头像和昵称
 

<u-popup :show="goLogin" @close="closeLogin" @open="openLogin" mode="bottom" :round="10" :closeable="true">
			<view class="login_box">
				<view class="log_title">获取您的昵称、头像</view>
				<view class="log_tip">获取用户头像、昵称,主要用于向用户提供具有辨识度的用户中心界面</view>
				<button class="ava_box" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar"
					 style="width: 188rpx;height: 188rpx;margin:0;padding: 0;">
					<image class="img_big" :src="avatar?avatar:'../../static/images/mine_avatar.png'" style="width: 188rpx;height: 188rpx;border-radius: 50%;"></image>
					<image class="img_samll" src="../../static/images/mine_camera.png" style="width: 60rpx;height: 60rpx;"></image>
				</button>
				<view class="set_nick">
					<input placeholder="请输入昵称" class="nickname" type="nickname" v-model="nickname"
					 @input="nameInput" @blur="nameInput" />
				</view>
				<view v-if="avatar && nickname" class="act_save" @click="saveUser">保存</view>
				<view v-else class="save">保存</view>
			</view>
		</u-popup>

 更换头像事件
 

onChooseavatar(e) {
				let _this = this;
				let imgUrl = {};
				imgUrl = e.detail;
				uni.showLoading({
					title: '加载中'
				})
				uni.uploadFile({
					url: 'http://xxxx.com/upload',
					filePath: imgUrl.avatarUrl,
					name: 'file',
					formData: {
						file: "file",
					},
					header: {
						"token": getApp().globalData.token || uni.getStorageSync('token'),
					},
					success: uploadFileRes => {
						// 注意:这里返回的uploadFileRes.data 为JSON 需要自己去转换
						let data = JSON.parse(uploadFileRes.data);
						if (data.code == 1) {
							_this.avatar = data.data.fullurl;
							
						}
					},
					fail: (error) => {
						uni.showToast({
							title: error,
							duration: 2000
						});
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			},

更改昵称事件

nameInput(e) {
				this.nickname = e.detail.value
			},

然后点击保存更改信息
 

// 保存信息
			saveUser(){
				let dataInfo = uni.getStorageSync('userInfo');
				let _this = this;
				changeInfo({
					nickname: _this.nickname,
					avatar: _this.avatar,
					gender:_this.gender
				}, 'json').then(res => {
					if (res.code == 1) {
						dataInfo.avatar = _this.avatar
						dataInfo.nickname = _this.nickname
						dataInfo.gender = _this.gender
						uni.setStorageSync('userInfo', dataInfo)
						_this.goLogin = false
						_this.isLogin = true
					}
				})
			}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值