uniapp在微信小程序获取用户信息和openId

uniapp在微信小程序获取用户信息和openId

获取用户信息

使用getUserProfile(),这个方法在每次出发的时候都会调起微信授权的那个弹出层,为什么不用getUserInfo(),微信给程序员们发公告了,不同于gerUserInfo,getUserProfile的使用方法被简化了许多,不需要在按钮属性中添加open-type,更不用规定按钮的方法,只需要:

<button @click="getUserProfile">获取用户信息</button>

就完事了。

这个API需要在微信开发者工具基础调试库版本2.10.4以上或微信版本7.0.9以上才可以使用。

所以使用之前在页面的onLoad()里面判断下:

onLoad() {
	 if(uni.getUserProfile){
	     this.canIUseGetUserProfile = true
	 }
},

在按钮上加上判断就可以了:

<button v-if="canIUseGetUserProfile" class="sys_btn" @click="getUserProfile">授权</button>

使用时的注意事项,这个方法必须由按钮直接调用,不可以在其他函数的回调中使用这个方法,否则就会报如下错误:


> errMsg: "getUserProfile:fail can only be invoked by user TAP gesture."

因此,直接调用就可以了,具体如下:

DOM:

<button v-if="canIUseGetUserProfile" class="sys_btn" @click="getUserProfile">授权</button>

JS:

getUserProfile() {
          //获取用户信息
            uni.getUserProfile({
                desc: '用户信息展示',
                success: (info) => { 
                    this.userInfo = info
                },
                fail: (err) => {
                    uni.showToast({
                        title: "微信登录授权失败",
                        icon: "none"
                    });
                }
            })
},

关于getUserProfile()的参数和出参照文档即可: wx.getUserProfile(Object object)

获取到用户信息之后,存到vuex中供后续使用。

获取openId

在获取openID之前需要先调用login()方法获取一个登陆凭证

uni.login({
     timeout: 10000,
      provider: 'weixin',  //如果是uniapp,在这里需要标明平台的类型,支持的参数请查阅uniapp官网的uni.login()文档
      success: (res) => {
			//登陆成功的回调
	  },
      fail: (err) => {
         	//登陆失败的回调
      }
})

在获取到登陆凭证之后,就可以调用接口获取openId和session_key等参数了。
可以在login()的success中直接使用如下代码:

uni.request({  
      url: 'https://api.weixin.qq.com/sns/jscode2session',  
      method:'GET',  
      data: {  
          appid: 'wx xxxxxxxxxxxx,       //你的小程序的APPID  
          secret: 'xxxxxxxxxxxxx',       //你的小程序的secret,  
          js_code: 'xxxxxxxxxxxxx        //wx.login 登录成功后的code  
      },  
      success: (cts) => {  
          console.log(cts);
      }  
});  

直接使用微信提供的https://api.weixin.qq.com/sns/jscode2session接口,传入的参数重appid和secret需要小程序管理员到微信公众平台获取,具体详细的获取方式不作赘述。

到这里微信授权获取登陆信息的过程就结束了,可以将获取到的信息都存储到uniapp的Storage中做一个数据持久化,这样可以避免页面刷新导致的数据丢失。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp开发微信小程序中,可以通过微信授权的方式获取用户的信息,包括头像、昵称和手机号。具体步骤如下: 1. 使用uni.login()方法获取微信提供的code码。 2. 使用获取到的code码,可以获取用户的唯一标识openid和会话密钥sessionkey,用于解密获取手机的加密信息。 3. 使用微信提供的getphonenumber方法,获取encryptedData和iv两个字段的内容。 4. 使用微信提供的解密方法,可以得到最终的手机号。 5. 要获取用户的头像、昵称等信息,可以直接调用uni.getUserProfile()方法。 通过以上步骤,就可以在uniapp微信小程序获取用户的信息了。引用\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [uniapp小程序中登录,获取用户信息获取手机号逻辑记录](https://blog.csdn.net/u011608672/article/details/124713671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uniapp小程序登录,获取用户信息获取手机号等流程](https://blog.csdn.net/tutodelinglimgho/article/details/131321975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值