当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现授权登录

微信小程序实现授权登录

2019年07月21日  | 移动技术网IT编程  | 我要评论

前言:由于微信官方修改了 getuserinfo 接口,所以现在无法实现一进入微信小程序就弹出授权窗口,只能通过 button 去触发。官方连接:

1.实现思路

自己写一个微信授权登录页面让用户实现点击的功能,也就是实现了通过 button 组件去触发 getuserinof 接口。在用户进入微信小程序的时候,判断用户是否授权了,如果没有授权的话就显示下面“界面简介”的第一个图,让用户去执行授权的操作。如果已经授权了,则直接跳过这个页面,进入首页。

2.界面简介

3.源码

login.wxml

 <view wx:if="{{caniuse}}">
 <view class='header'>
  <image src='/images/wx_login.png'></image>
 </view>
 
 <view class='content'>
  <view>申请获取以下权限</view>
  <text>获得你的公开信息(昵称,头像等)</text>
 </view>
 
 <button class='bottom' type='primary' open-type="getuserinfo" lang="zh_cn" bindgetuserinfo="bindgetuserinfo">
  授权登录
 </button>
</view>
 
<view wx:else>请升级微信版本</view>

login.wcss

.header {
 margin: 90rpx 0 90rpx 50rpx;
 border-bottom: 1px solid #ccc;
 text-align: center;
 width: 650rpx;
 height: 300rpx;
 line-height: 450rpx;
}
 
.header image {
 width: 200rpx;
 height: 200rpx;
}
 
.content {
 margin-left: 50rpx;
 margin-bottom: 90rpx;
}
 
.content text {
 display: block;
 color: #9d9d9d;
 margin-top: 40rpx;
}
 
.bottom {
 border-radius: 80rpx;
 margin: 70rpx 50rpx;
 font-size: 35rpx;
}

login.json

{
 
"navigationbartitletext": "授权登录"
 
}

login.js

代码的 wx.request 是我项目与后台的一些交互,可直接删除掉。

需要修改的地方:

记得自己补上 wx.switchtab 接口中的 url 属性,这是授权成功后跳转的页面路径,由于我的首页是 tarbar 页面,所以这里用wx.switchtab ,如果不是 tarbar 页面的话,可以用 wx.navigateto 和 wx.redirecto 去跳转

 page({
 data: {
  //判断小程序的api,回调,参数,组件等是否在当前版本可用。
  caniuse: wx.caniuse('button.open-type.getuserinfo')
 },
 onload: function () {
  var that = this;
  // 查看是否授权
  wx.getsetting({
   success: function (res) {
    if (res.authsetting['scope.userinfo']) {
     wx.getuserinfo({
      success: function (res) {
       //从数据库获取用户信息
       that.queryusreinfo();
       //用户已经授权过
       wx.switchtab({
        url: ''
       })
      }
     });
    }
   }
  })
 },
 bindgetuserinfo: function (e) {
  if (e.detail.userinfo) {
   //用户按了允许授权按钮
   var that = this;
   //插入登录的用户的相关信息到数据库
   wx.request({
    url: getapp().globaldata.urlpath + 'hstc_interface/insert_user',
    data: {
     openid: getapp().globaldata.openid,
     nickname: e.detail.userinfo.nickname,
     avatarurl: e.detail.userinfo.avatarurl,
     province:e.detail.userinfo.province,
     city: e.detail.userinfo.city
    },
    header: {
     'content-type': 'application/json'
    },
    success: function (res) {
     //从数据库获取用户信息
     that.queryusreinfo();
     console.log("插入小程序登录用户信息成功!");
    }
   });
   //授权成功后,跳转进入小程序首页
   wx.switchtab({
    url: '' 
   })
  } else {
   //用户按了拒绝按钮
   wx.showmodal({
    title:'警告',
    content:'您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
    showcancel:false,
    confirmtext:'返回授权',
    success:function(res){
     if (res.confirm) {
      console.log('用户点击了“返回授权”')
     } 
    }
   })
  }
 },
 //获取用户信息接口
 queryusreinfo: function () {
  wx.request({
   url: getapp().globaldata.urlpath + 'hstc_interface/querybyopenid',
   data: {
    openid: getapp().globaldata.openid
   },
   header: {
    'content-type': 'application/json'
   },
   success: function (res) {
    console.log(res.data);
    getapp().globaldata.userinfo = res.data;
   }
  });
 },
 
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网