微信小程序-授权页获取session3d登录2.0

由于微信小程序不断地更新迭代,本文只要介绍前端如何实现微信登录获取session3d。由于又前一篇文章做精准的讲述,本文就不再一一讲解了。

login.wxml

<image class='logo' src='/images/logo.png'></image>
<button class='save-btn' open-type="getUserInfo" class='getbtn' bindgetuserinfo="getUserInfo"> 
  登录
</button>

login.js

const app = getApp()
const SUCCEED_CODE = 1005;
Page(app.$CREATE_PAGE({
  data: {
    tel: "",
    backUrl: "",
    code:""
  },

  onLoad: function () {
  },
  tel: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  getUserInfo(e) {
    if (this.data.tel.length !== 11) {
      wx.showModal({
        title: '请输入正确的手机号码'
      });
      return;
    }
    let Success = "getUserInfo:ok",
      info,
      that = this;
    if (e.detail.errMsg == Success) { // 授权成功
      info = e.detail;
      // console.log(res)
      info.code = this.data.code;
      info.appId = app.globalData.appId;
      // console.log(info); // 获取session3d的参数
      that.wxlogin(info);
    } else { //授权失败,请重新授权
      wx.showModal({
        title: '授权失败,请重新授权'
      });
    }
  },
  wxlogin: function (info) {
    var that = this;
    app.$GET('/index/login/doWxLogin', {
      "code": info.code,
      "rawData": info.rawData,
      "signature": info.signature,
      'iv': info.iv,
      'encryptedData': info.encryptedData,
      'mobile': this.data.tel
    }).then(res => {
      if (res.code == 1003) {
        app.globalData.token = res.userData.session3rd; //全局缓存
        app.globalData.userInfo = res.userData;
        console.log(app.globalData)
        let backUrl = wx.getStorageSync("backUrl") || '/pages/tabindex/tabindex'; //特殊页面的跳转
        wx.redirectTo({
          url: backUrl,
        })
      } else {
        wx.showModal({
          title: '授权失败,请重新授权'
        });
      }
    })
  },
  onShow: function () {
    let that = this;
    wx.login({ // 获取code
      success(res) {
        that.setData({
          code: res.code // 必须先获取code否则会出现签名已过期,需要第二次点击才能生效
        })
      }
    })
  },
}))

login.wxss

.logo{
  width: 166rpx;
  height: 166rpx;
  margin: 46rpx auto;
  display: block;
  
}
.tel{
  font-size: 24rpx;
  width: 100%;
  border-bottom: 1rpx solid rgb(206, 206 , 206);
  line-height: 100rpx;
  height: 100rpx;
}
.getbtn{
  width: 600rpx;
  height: 88rpx;
  background-color: rgb(68, 153, 255);
  font-size: 33rpx;
  color: #fff;
  margin: 60rpx auto 0;
  border-radius: 10rpx;
  text-align: center;
  line-height: 88rpx;
}
page{
  width: 100%;
  padding: 50rpx;
  box-sizing: border-box;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值