1. 程式人生 > 其它 >微信小程式 登入

微信小程式 登入

app.js

// app.js
const interfaces = require("./utils/urlconfig")
App({
  onLaunch() {
    // 展示本地儲存能力
    const logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);

    this.getUserMsg();
  },
  //獲取登入許可權後  調取伺服器介面 傳遞 code 
  getUserMsg() {
    // 獲取使用者資訊
wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { const that = this; // 已經授權,可以直接呼叫 getUserInfo 獲取頭像暱稱,不會彈框 wx.login({ success: function (res) { var code = res.code; //登入憑證 if (code) {
//2、呼叫獲取使用者資訊介面 wx.getUserInfo({ lang: 'zh_CN', success: function (res) { //3.請求自己的伺服器,解密使用者資訊 獲取unionId等加密資訊 let obj = { encryptedData: res.encryptedData, iv: res.iv, code: code, type:
3 } wx.request({ url: interfaces.decodeUserInfo, //自己的服務介面地址 method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { encryptedData: res.encryptedData, iv: res.iv, code: code, type: 3 }, success: function (data) { //4.解密成功後 獲取自己伺服器返回的結果 console.log(data, '-----------------') if (data.data.status == 1) { var userInfo = data.data.userInfo; // userInfo.isAdmin = false; userInfo.isGetInfo = false; // userInfo.schoolId = null; that.globalData.userInfo = userInfo; that.globalData.session_key = data.data.session_key; that.globalData.openId = data.data.openId; that.globalData.hasUserInfo = true; if (that.userInfoReadyCallback) { that.userInfoReadyCallback(data) } } else { console.log('解密失敗') } }, fail: function () { console.log('系統錯誤') } }) }, fail: function (res) { console.log(res); console.log('獲取使用者資訊失敗' + res); } }) } else { console.log("獲取使用者登入憑證失敗"); } }, fail: function () { } }) } else { console.log("未登入授權"); that.globalData.hasLogin = false; if (that.userInfoReadyCallback) { that.userInfoReadyCallback({ data: { userInfo: null } }) } } } }) }, onShow() { const self = this; wx.getStorage({ key: 'userInfo', success(res) { if (res.data) { self.globalData.userInfo = res.data; } } }) //獲取頂部導航欄膠囊部分高度 let menuButtonObject = wx.getMenuButtonBoundingClientRect(); wx.getSystemInfo({ success: (res) => { // console.log(res.windowHeight*2) let statusBarHeight = res.statusBarHeight; const navTop = menuButtonObject.top; //膠囊按鈕與頂部的距離 const navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //導航高度 let height = res.windowHeight * 2 - navHeight * 2 - 40 - 315 - 40; self.globalData.defineHeader.navHeight = navHeight * 2 self.globalData.defineHeader.navTop = navTop * 2 } }) }, globalData: { userInfo: null, defineHeader: { navHeight: null, navTop: null, } }, })
View Code

第一次獲取使用者許可權 之後才能使用wx.login 返回code 在呼叫伺服器介面 返回openId

  login() {
    wx.getUserProfile({
      desc: '獲取你的暱稱、頭像、地區及性別', // 宣告獲取使用者個人資訊後的用途,後續會展示在彈窗中,請謹慎填寫
      success: (res) => {
       app.getUserMsg()
      },
      fail: function () {
        console.log('獲取使用者資訊失敗')
      }
    })
  },

  onShow: function () {
    let that = this;
    //自定義底部導航欄跳轉方法
    if (typeof that.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }

    // console.log(app.globalData,'-----------')
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      //由於getuserinfo是一個網路請求,可能會在當前page.onload之後才返回
      //為了防止這種情況發生,所以此處加入callback
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.data.userInfo,
          hasUserInfo: true
        });
      }
    }
  },
View Code