iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >uniapp小程序使用getUserProfile登录(获取昵称统一为‘微信用户’+头像为灰色头像)
  • 569
分享到

uniapp小程序使用getUserProfile登录(获取昵称统一为‘微信用户’+头像为灰色头像)

微信小程序 2023-09-16 10:09:53 569人浏览 八月长安
摘要

一、微信授权登录按钮 微信授权登录 二、微信授权登录弹窗 授权登录授权获取您的手机号登录代表您已同意隐私政策微信快捷授权登录 三、数据定义 hasLogin: false,phoneModal

一、微信授权登录按钮

    微信授权登录

二、微信授权登录弹窗

授权登录授权获取您的手机号登录代表您已同意隐私政策微信快捷授权登录

三、数据定义

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

四、登录方法

//获取个人信息async getUserProfile() {//调用微信获取codeuni.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.codethis.request(url,"POST",null).then(res =>{if(res.data.code == 200){this.userRawData = userMsgHave; // 存储用户信息this.LoginCodeMsg.openId = res.data.openIdthis.LoginCodeMsg.sessionKey = res.data.sessionKeythis.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 = falseif(res.data.code == 200){this.isLogin = trueuni.showToast({title:'登录成功',icon:'none'})this.hasLogin = truethis.userId = res.data.busInfo.userIdthis.userInfo.level = res.data.busInfo.userTypethis.userInfo.nickName = res.data.busInfo.nickNamethis.userInfo.mobiler = res.data.busInfo.phoneNumberthis.userInfo.avatar = res.data.busInfo.avatarconsole.log(JSON.stringify(this.userInfo))console.log(JSON.stringify(res.data.busInfo))//缓存登录状态和用户信息this.userInfo.token = res.data.tokenthis.$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'})}}})}

来源地址:https://blog.csdn.net/m0_43584016/article/details/129929339

--结束END--

本文标题: uniapp小程序使用getUserProfile登录(获取昵称统一为‘微信用户’+头像为灰色头像)

本文链接: https://www.lsjlt.com/news/409717.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作