小程序初始化加載,根據登錄狀態判斷跳轉首頁還是登錄頁


准備是在小程序初始化進行一下登錄狀態判斷,每次登陸以后會在storage里存一個user,根據這個值判斷,沒登陸過小程序啟動進登錄頁面,登陸過直接跳轉首頁:

App({
    onLaunch: function() {
        //初始化加載,先判斷用戶登錄狀態
        if (wx.getStorageSync('user')) {
            wx.switchTab({
                url: 'pages/home/home'
            })
        } else {
            wx.reLaunch({
                url: 'pages/login/login'
            })
        }
  
    },
    globalData: {
  
    }
})

在aap.js里面初始化的時候進行判斷,真機情況下沒有登錄狀態時,還是會先加載首頁面(一閃而過),之后跳轉到login登錄頁面,登錄成功后,返回首頁。

小程序登錄獲取用戶信息:

login.wxml

<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授權登錄</button>  

<view wx:else>請升級微信版本</view> 
login.js
Page({
  data: {
    //判斷小程序的API,回調,參數,組件等是否在當前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function() {
    // 查看是否授權
    wx.getSetting({
      success: function(res){
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
              //用戶已經授權過
            }
          })
        }
      }
    })
  },
  bindGetUserInfo: function(e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo){
      //用戶按了允許授權按鈕
    } else {
      //用戶按了拒絕按鈕
    }
  }
})

wx.canIUse(string schema)

判斷小程序的API,回調,參數,組件等是否在當前版本可用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM