小程序登录逻辑(未完待续)

318 阅读1分钟
小程序项目中,我们通常把openid作为唯一标识,即可证明使用者的身份。在操作需要身份的操作时,将身份标识传给后台即可。以下是小程序最佳授权尝试。
  1. 小程序将code传给后台,后台即通过code获取openid判断是否是新用户,是则建立用户;
  2. 操作需要身份标识时,点击直接弹出授权,完善用户头像昵称性别地址等。

通过code获取openid

wx.login({
  success (res) {
    if (res.code) {
      //发起网络请求
      //获取用户信息(未授权或已授权),若授权过则能通过openid获取头像昵称等
      //保存是否是授权用户
    } else {
      console.log('登录失败!' + res.errMsg)
    }
  }
})

判断是否是已授权用户

<view bindtap="goOrder" class="item-order" data-status="2">  <view class="item-num" wx:if="{{myInfo.paid_count>0}}">{{myInfo.paid_count>99?'99+':myInfo.paid_count}}</view>  <text class="iconfont icon-daifahuo1 item-icon"></text>  <view class="item-title font-12 font-sc-re">{{paidCountText}}</view>  <button bindgetuserinfo="userInfoHandler" class="order-authorization" data-button-type="order" data-status="2" lang="zh_CN" openType="getUserInfo" wx:if="{{showAuthorizationButton}}"></button></view>