uniapp アプレットは getUserProfile を使用してログインします (取得されたニックネームは「WeChat ユーザー」に統一されます + アバターは灰色のアバターになります)

1.WeChat認証ログインボタン

<view class="name" v-if="!hasLogin">
							    <u-button @click="getUserProfile()" size="small" class="authorizationBtn" type="primary">
										微信授权登录
								</u-button>
							</view>

2. WeChat認証ログインポップアップウィンドウ

<view>
			<!-- 登录弹框 -->
			<u-popup :show="phoneModalShow" mode="center" border-radius="14" >
				<view class="sq_box">
					<view>授权登录</view>
					<view>授权获取您的手机号</view>
					<view>
						<view class="agreement_warp">
							<view>登录代表您已同意</view>
							<view @click="agreenmentDetailHandle" style="color: #ff431e;">隐私政策</view>
						</view>
						<u-button style="width: 100%;"  type="success"
							@getphonenumber="getphonenumber" open-type="getPhoneNumber">
							微信快捷授权登录
						</u-button>
					</view>
				</view>
			</u-popup>
		</view>

3. データ定義

hasLogin: false,
phoneModalShow: false, // 手机号隐藏层

4. ログイン方法

//获取个人信息
			async getUserProfile() {
				//调用微信获取code
				uni.login({
					provider:'weixin',
					success: (logRes) => {
						console.log(logRes.code)
						this.code = logRes.code
					}
				});
				uni.getUserProfile({
					desc:'WeiXin'
				}).then(res => {
					if (res && res.length > 1) {
						let userMsgHave = JSON.parse(res[1].rawData)
						console.log(JSON.parse(res[1].rawData));
						uni.showLoading({
							title: '登录加载中'
						});
						if(res[1].errMsg == "getUserProfile:ok"){
							console.log("codeLogin", this.code);
							let url = 'wx/code/' + this.code
							this.request(url,"POST",null).then(res =>{
								if(res.data.code == 200){
									this.userRawData = userMsgHave; // 存储用户信息
									this.LoginCodeMsg.openId = res.data.openId
									this.LoginCodeMsg.sessionKey = res.data.sessionKey
									this.phoneModalShow = true;
									uni.hideLoading();
								}else{
									uni.showToast({
										title: '微信登录失败',
										icon: 'none'
									})
									uni.hideLoading();
								}
							})
						}else{
							uni.showToast({
								title:'登录失败请重试',
								icon:'error',
								duration:2000
							})
							uni.hideLoading();
						}
					}
				})
			},
			//获取手机号
			getphonenumber(e){
				let phoneMsg = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					openId: this.LoginCodeMsg.openId,
					sessionKey: this.LoginCodeMsg.sessionKey,
					avatar: this.userRawData.avatarUrl,
					nickName: this.userRawData.nickName,
					sex: this.userRawData.gender
				}
				this.request('wx/login','POST',phoneMsg).then(res =>{
					this.phoneModalShow = false
					if(res.data.code == 200){
						this.isLogin = true
						uni.showToast({
							title:'登录成功',
							icon:'none'
						})
						this.hasLogin = true
						this.userId = res.data.busInfo.userId
						this.userInfo.level = res.data.busInfo.userType
						this.userInfo.nickName = res.data.busInfo.nickName
						this.userInfo.mobiler = res.data.busInfo.phoneNumber
						this.userInfo.avatar = res.data.busInfo.avatar
						console.log(JSON.stringify(this.userInfo))
						console.log(JSON.stringify(res.data.busInfo))
						//缓存登录状态和用户信息
						this.userInfo.token = res.data.token
						this.$store.state.busInfo = res.data.busInfo;
						this.$store.state.hasLogin = this.isLogin;
						this.$store.state.userInfo = this.userInfo;
						this.$store.state.token = res.data.token;
						//将当前代理的个人信息存入缓存中
						uni.setStorageSync('busInfo',res.data.busInfo)
						//token存缓存
						uni.setStorageSync('token',res.data.token)
						setTimeout(function (){
							uni.navigateBack()
						},2000)
						//加载顶部数据
						this.initTopShouYiData()
					}else{
						uni.showToast({
							title:'登录失败',
							icon:'error',
							duration:2000
						})
					}
				})
			},

//退出方法
			logout(){
				uni.showModal({
					title:'提示',
					content:'请确认是否退出',
					success: (res) => {
						if(res.confirm){
							this.$store.state.hasLogin = false;
							this.$store.state.userInfo = null;
							this.$store.state.busUser = null;
							uni.showToast({
								title:'退出登录成功',
								icon:'none'
							})
							uni.redirectTo({
								url:'/pages/index/index'
							})
						}
					}
				})
			}

おすすめ

転載: blog.csdn.net/m0_43584016/article/details/129929339
おすすめ