小程序授权登录

$checkAuthSetting 在全局检测授权情况:

已授权 -->  $getUserInfo 获取用户信息 —— 检测 seesion_key 状态(未过期)

                  $getUserInfo 获取用户信息 —— 检测 seesion_key 状态(已过期)—— 调用微信 $wxLogin 登录

未授权 -->  $getCurrentPageUrlWithArgsPromise 获取当前页面带参数的url —— 判断是否白名单(是)

                  $getCurrentPageUrlWithArgsPromise 获取当前页面带参数的url —— 判断是否白名单(否)—— 跳转登录授权界面

// 全局判断 app.js

App({
...
  onShow: function (e) {
    // 调用全局检测授权情况
    this.globalAuthSetting()
  },

  // 全局检测授权情况
  globalAuthSetting () {

    $checkAuthSetting().then(res => {
      if (res.authSetting['scope.userInfo']) {
        // 已经授权操作
        console.log('----APP 全局检测:已授权----')

        // todo 获取用户信息并保存
        $getUserInfo().then(res => {
          let userInfo = res
          this.globalData.userInfo = userInfo
          // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(userInfo)
          }
          return userInfo
        }).then(userInfo => {
          let loginParams = userInfo
          // 检测 session_key状态
          $checkSession().then(sessionRes => {
            console.log('----APP session_key 未过期 ----',  sessionRes)
          }).catch(sessionErr => {
            console.log('----APP session_key 已过期 ----',  sessionErr)
            // 过期重新调用微信wx.login登录
            $wxLogin().then(wxRes => {
              console.log('----APP 重新登录-> wxlogin:----',  wxRes)
              let code = wxRes.code
              wx.setStorageSync("code", code)
              loginParams.code = code
              this.func.$post('login', loginParams, reqRes => {
                let {code, token} = reqRes
                if (code == 0) {
                  wx.setStorageSync("token", token)
                  wx.setStorageSync('nendRefreshChannel', true)
                } else {
                  $toast('网络错误!')
                }
              })
            })
          })
        })

      } else {
        // 未授权跳转授权页面
        console.log('----APP 未授权 ----')
        $getCurrentPageUrlWithArgsPromise().then(res => {
          console.log('----APP 当前打开页面URL:----',  res)
          let currentUrl = $getCurrentPageUrl()
          let isInPageAuthWhite =  checkAuthWhitePage(currentUrl, CONFIG.authWhitePages)
          return isInPageAuthWhite ? isInPageAuthWhite : res
        }).then(res => {
          if (res !== true) {
            console.log('----APP 非白名单页面跳转授权页面 ----')
            let loginCallBackUrl = res
            $routerPage('/pages/login/login', 'redirectTo').then(() => {
              let isHasLoginCallBackUrl = wx.getStorageSync('loginCallBackUrl') || ''
              if (!isHasLoginCallBackUrl) {
                wx.setStorageSync('loginCallBackUrl', loginCallBackUrl)
              }
            })
          } else {
            console.log('----APP 白名单页面不跳转授权页面 ----')
          }
        })
      }
    }).catch(err => {
      console.log('----APP $checkAuthSetting error ----',  err)
      $toast('微信授权接口请求失败!')
    })

  }

...
})

使用小程序 button,open-type 属性值为 getUserInfo,获取用户基本信息; 

<button class="btn submit-btn" open-type="getUserInfo" bindgetuserinfo="getUserInfo" lang="zh_CN">立即授权</button>

 判断用户是否点击允许授权,允许则执行微信登录 $wxLogin

//未授权点击弹出授权
  getUserInfo (e) {
    console.log('点击授权 getUserInfo:', e)
    if (e.detail.errMsg === 'getUserInfo:ok') {
      let userInfo = e.detail
      app.globalData.userInfo = userInfo
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })

      $wxLogin().then(res => {
        let code = res.code
        wx.setStorageSync("code", code)
        return code
      }).then(res => {
        let params = Object.assign(userInfo, { code: res })
        app.func.$post('login', params, reqRes => {
          let {code, token} = reqRes
          if (code === $SUCCESS) {
            wx.setStorageSync("token", token)
            
            // 返回上一级页面
            let loginCallBackUrl = wx.getStorageSync('loginCallBackUrl') || DEFAULT_PATH
            if (loginCallBackUrl) {
              loginCallBackUrl = loginCallBackUrl.startsWith(AUTH_PATH) ? DEFAULT_PATH : loginCallBackUrl
              $routerPage('/' + loginCallBackUrl, isTabBarPage(loginCallBackUrl) ? 'switchTab' : 'redirectTo').then(res => {
                wx.removeStorageSync('loginCallBackUrl')
              })
            }

          } else {
            $toast('网络错误!')
          }
        })
      })
    } else {
      // 取消授权
      $toast('取消授权')
    }
  }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 对于springboot小程序授权登录,我可以为您提供一些指导意见。首先,您可以使用Spring Boot的OAuth2框架来实现小程序授权登录。其次,您还可以通过使用微信的SDK来实现小程序授权登录的功能。 ### 回答2: Springboot小程序授权登录是指在使用Springboot框架开发小程序时,实现用户登录授权的功能。 首先,我们需要在小程序端使用微信提供的API,获取用户的OpenID和SessionKey。通过Springboot的HTTP请求工具,发送请求到微信API接口,获取到用户的OpenID和SessionKey。 然后,我们可以根据获取到的OpenID和SessionKey,在后端的控制器中进行用户登录授权逻辑。可以自定义一个用户实体类存储用户的信息,比如OpenID、SessionKey等。 接下来,我们可以使用Springboot提供的数据库操作工具,比如MyBatis或者Hibernate等,将用户信息存储到数据库中,或者更新用户登录状态。 在前端页面中,我们可以根据用户登录状态,判断用户是否已经登录成功。可以使用Springboot提供的Session管理工具,将用户登录状态存储在Session中,在每次请求时判断用户登录状态,进行相应的处理。 除了基本的授权登录功能,我们还可以结合Spring Security等安全框架,增加用户权限的管理。可以在数据库中存储用户的角色和权限信息,并在后端进行相应的授权校验,保证用户只能访问其具备权限的功能。 总的来说,Springboot小程序授权登录是通过调用微信API获取用户的OpenID和SessionKey,并结合Springboot框架实现用户登录授权功能的一种实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值