栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

uni-app微信小程序登录授权的实现

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uni-app微信小程序登录授权的实现

微信小程序授权是非常简单和常用的功能,但为了方便,还是在此记录一下要点:

首先是需要用到一个授权按钮来触发获取用户信息授权: 关键在于 open-type 为 getUserInfo , 然后有个@getuserinfo的事件,把获取授权接口写到该事件里面去

方法如下:

appLoginWx(){
 // #ifdef MP-WEIXIN
   uni.getProvider({
    service: 'oauth',
    success: function (res) {
     if (~res.provider.indexOf('weixin')) {
uni.login({
  provider: 'weixin',
  success: (res) => {
    _self.authorization = res.code;
    uni.getUserInfo({
      provider: 'weixin',
      success: (info) => {//这里请求接口
 console.log(res);
 console.log(info);
 
      },
      fail: () => {
 uni.showToast({title:"微信登录授权失败",icon:"none"});
      }
    })

  },
  fail: () => {
    uni.showToast({title:"微信登录授权失败",icon:"none"});
  }
})

     }else{
uni.showToast({
  title: '请先安装微信或升级版本',
  icon:"none"
});
     }
    }
   });
   //#endif
      }

在 uni.login 和 uni.getUserInfo 被调用后,你可以获取到以下值用于继续请求后端给你的接口:

常用的值大概有:code 、iv 、encryptedData 和 个人基本信息,这些可以传给后端交换得到openid。

如果需要知道用户当前是否已经授权,则可以使用如下代码:

uniapp的授权文档,可以判断不同的授权类型:https://uniapp.dcloud.io/api/other/authorize?id=authorize

// #ifdef MP-WEIXIN
      uni.getSetting({
success(res) {
 console.log("授权:",res);
 if (!res.authSetting['scope.userInfo']) {
   //这里调用授权
   console.log("当前未授权");
 } else {
   //用户已经授权过了
   console.log("当前已授权");
 }
}
      })
      //#endif

到此这篇关于uni-app微信小程序登录授权的实现的文章就介绍到这了,更多相关uni-app小程序登录授权内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/70216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号