微信小程序的授权登录功能

注意:首先微信小程序是不能一进入小程序就让用户授权登录的,一进入小程序就让用户授权登录,是审核不通过的;

图一的这个提示框是我自己写的;要想弹出微信的微信授权提示框,必须用一个按钮来触发,可用授权登录这个按钮来让微信授权这个弹窗显示,具体代码如下:

index.wxml

  <view class="tc" wx:if="{{isShow}}" bindtap="hidden">
    <view class="inner" wx:if="{{isShow}}">
      <view class='header'>
        <image src='../../image/logo.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>

index.wcss

/* 授权提示框css,start */ 
.tc {
  position: fixed;
  top: 0;
  height: 100vh;
  width: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 900;
} 
.inner {
  background: #fff;
  margin: 0rpx 30rpx 0 30rpx;
  padding: 0 30rpx;
  height: 630rpx;
  border-radius: 10rpx;
  position: relative;
  top: 18%;
  z-index: 1000;
} 
.header {
  border-bottom: 1px solid #ccc;
  text-align: center;
  height: 300rpx;
  margin: 0 auto;
  line-height: 450rpx;
} 
.header image {
  width: 200rpx;
  height: 200rpx;
} 
.content {
  margin-left: 50rpx;
  margin-bottom: 70rpx;
} 
.content>view {
  margin-top: 10rpx;
} 
.content text {
  display: block;
  color: #9d9d9d;
  margin-top: 20rpx;
} 
.bottom {
  border-radius: 80rpx;
  font-size: 35rpx;
  padding: 25rpx 10rpx; 
} 
/* 授权提示框css,end */

index.js

  bindGetUserInfo(e) {
    if (e.detail.userInfo) {
//将e.detail.userInfo存入缓存
      wx.setStorage({
        key: "userInfo",
        data: e.detail.userInfo
      })
      this.logIn();
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告' ,
        content:  '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!' ,
        showCancel: false,
        confirmText: '返回授权' ,
        success: function(res) {
          // 用户没有授权成功,不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
 logIn() { 
    wx.login({
      success: res => {
// 请求后台地址,使用 code 换取 openid 和 session_key 等信息 
        call.request_post("/counseling/webUserProfile/loginByWeiXin", {
            code: res.code
          },
          res => {  
// 成功会掉函数
          },
          err => {
// 失败回调函数
            wx.showToast({
              title: err.message
            })
          }
        )
      }
    })
  },

 

  • 7
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
微信小程序授权登录功能的实现需要以下步骤: 1. 在小程序管理后台创建一个应用,并获取到小程序的AppID。 2. 在小程序中使用`wx.login`接口获取临时登录凭证code,并将code发送到开发者服务器。 3. 在开发者服务器中使用`wx.request`接口调用微信登录API,获取到用户的openid和session_key等信息。 4. 将用户的openid和session_key等信息存储到开发者服务器中,用于后续的用户信息查询与验证等操作。 下面是一个简单的微信小程序授权登录的代码示例: ``` // 小程序授权登录 wx.login({ success: function (res) { if (res.code) { // 发起网络请求,获取openid和session_key wx.request({ url: 'https://yourserver.com/api/login', data: { code: res.code }, success: function (res) { // 保存用户信息到本地缓存 wx.setStorageSync('openid', res.data.openid); wx.setStorageSync('session_key', res.data.session_key); } }); } else { console.log('获取用户登录态失败!' + res.errMsg) } } }) ``` 在开发者服务器中,可以使用微信提供的登录API(https://developers.weixin.qq.com/miniprogram/dev/api-backend/auth.code2Session.html)来获取openid和session_key等信息。例如,使用Node.js实现一个简单的登录接口: ``` const request = require('request'); // 微信登录接口 app.get('/api/login', function (req, res) { var code = req.query.code; var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=' + code + '&grant_type=authorization_code'; request(url, function (error, response, body) { if (!error && response.statusCode == 200) { var data = JSON.parse(body); res.send(data); } else { res.send(error); } }); }); ``` 这样,当用户完成微信小程序授权登录后,就可以在开发者服务器中获取到用户的openid和session_key等信息,用于后续的用户信息查询和验证操作。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值