uniapp获取微信用户信息登录

想要获取用户信息需要先使用wx.login功能

javascript 复制代码
	wx.login({
					success: res => {
						if (res.code) {
							// 获取 code 成功后,通过微信开放接口获取用户 openid
							wx.request({
								url: '后台接口',
								data: {
									code: res.code,
								},
								success: res => {
									console.log(res.data.openid);
									this.openid = res.data.openid
									uni.setStorageSync('openid', res.data.openid)
								}
							});
						}
					},
					fail: err => {
						console.log(err);
					}
				});

然后使用按钮的open-type获取用户的头像,想要获取完整用户信息可以使用getUserInfo

javascript 复制代码
	<view class="">
               // 调用微信开放能力
			<button open-type="chooseAvatar" @chooseavatar="chooseAvatar"
				style="width:190rpx;height: 190rpx;border-radius: 50%;padding: 0;margin-top: 20px;">
				<image :src="image" class="imageAsdf" style="width: 200rpx;height: 200rpx;border-radius: 100%;" />
			</button>
		</view>

获取用户昵称

html 复制代码
<view style="width: 60%;margin: auto;margin-top: 10px;">
			<input id="nickname-input" v-model="nickname" class="authorization white" type="nickname"
				placeholder="请输入用户昵称" style="width: 100%;text-align: center;" @change="change">
		</view>

保存头像和昵称

javascript 复制代码
            change(e) {
				this.nickname = e.detail.value
			},
			chooseAvatar(e) {
				console.log(e);
				this.image = e.detail.avatarUrl
			},

登录判断:

javascript 复制代码
// 登录
			logins() {
				let that = this;
				if (that.nickname == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入用户昵称',
						// duration: 2000
					});
				} else if (that.image == '') {
					uni.showToast({
						icon: 'none',
						title: '请上传用户头像',
						// duration: 2000
					});
				}
				if (that.nickname != '' && that.image != '') {
					uni.request({
						url: '登录接口',
                        //参数
						data: {
							openid: that.openid,
					        value:value
						},
						dataType: 'json',
						method: "POST",
						success(res) {
							console.log(res);
							if (res.data.err == '请求成功') {
								console.log(res.data.userid);
								uni.setStorageSync('userid', res.data.userid);
								
								uni.showToast({
									title: '登录成功',
									duration: 2000
								});
                                // 跳转路径
								let path = uni.getStorageSync('paths');
								setTimeout(() => {
									uni.reLaunch({
										url:`/pages/${path}/${path}`
									})
								}, 2000)
							} else {
								uni.showToast({
									title: '登录失败',
									icon:'none',
									duration: 2000
								});
							}
						}
					})
				}
 
			}
相关推荐
Lao乾妈官方认证唯一女友:D3 小时前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链
ALKAOUA3 小时前
力扣面试150题刷题分享
javascript·笔记
swipe4 小时前
JavaScript 对象与属性描述符:从原理到实战
前端·javascript·面试
&活在当下&4 小时前
Vue3 h函数用法详解
前端·javascript·vue.js
小贵子的博客4 小时前
(vue3错误处理)has naming conflicts with other components, ignored.
前端·javascript·vue.js
西西学代码4 小时前
Flutter---路由与导航
服务器·前端·javascript
跟着珅聪学java6 小时前
electron 安装教程
javascript·arcgis·electron
花哥码天下6 小时前
安装/卸载claude code和codex
开发语言·javascript·ecmascript
跟着珅聪学java6 小时前
Electron 读取 JSON 配置文件教程
前端·javascript·vue.js