欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

原生小程序?qū)崿F(xiàn)登錄授權(quán)與封裝統(tǒng)一調(diào)用接口 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!

您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >

原生小程序?qū)崿F(xiàn)登錄授權(quán)與封裝統(tǒng)一調(diào)用接口

發(fā)表時間:2021-1-5

發(fā)布人:葵宇科技

瀏覽次數(shù):67

一。登錄思路

先通過 wx.login 返回 res.code 到后臺接口換取 openId, sessionKey, unionId。然后通過 wx.getUserInfo 獲取用戶信息

如果要獲取用戶敏感信息則要 wx.getUserInfo 返回的數(shù)據(jù)傳到后臺進(jìn)行解析(我這邊是用大佬封裝好的api進(jìn)行解析 )。


二。代碼

1。小程序封裝統(tǒng)一請求接口


function Request(url, param, method, isJson) {
  const resUrl = wx.getStorageSync('url') + url;
  return new Promise((resolve, reject) => {
    wx.request({
      url: resUrl,
      data: param,
      header: {
        'content-type': isJson ? 'application/json' : 'application/x-www-form-urlencoded'
      },
      method: method,
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        // console.log("返回結(jié)果------")
        // console.log(res)
        resolve(res.data)
      },
      fail: function (err) {
        // console.log("請求失?。? + err.errMsg)
        wx.showToast({
          title: '請求失敗',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    })
  }).then((resData) => {
    return resData;
  })
}
module.exports = {
  Request: Request
}
 

2。創(chuàng)建一個api包專門區(qū)別放調(diào)用后臺接口,我這個是api包里的user.js

const requests = require("../utils/request.js")
module.exports = {
  // 登錄
  wxLogin: (data) => {
    return requests.Request("/wx/login/wx-login.json", { jsCode: data }, 'post', true);
  },

  //獲取用戶信息
  getUserInfo: (data) => {
    return requests.Request("/wx/login/get-user-info.json", data, 'post', true);
  },

  //獲取用戶手機(jī)號
  getUserPhone: (data) => {
    return requests.Request("/wx/login/get-user-phone.json", data, 'post', true);
  }
}
 
3.修改下app.js

const userRequest = require("/api/user.js")
App({
  onLaunch: function () {
    // 展示本地存儲能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    wx.setStorageSync('url', "http://localhost:8087");

    this.getUserInfo();
  },
  onShow() { },
  getUserInfo() {
    let that = this;
    // 登錄
    wx.login({
      success: res => {
        // 發(fā)送 res.code 到后臺換取 openId, sessionKey, unionId
        userRequest.wxLogin(res.code).then((res) => {
          if (res.code === "SUCCESS") {
            wx.setStorageSync('sessionKey', res.data.sessionKey);
            that.globalData.userInfo = res.data.wxUser;
            // 由于 getUserInfo 是網(wǎng)絡(luò)請求,可能會在 Page.onLoad 之后才返回
            // 所以此處加入 callback 以防止這種情況
            if (that.userInfoReadyCallback) {
              that.userInfoReadyCallback(res.data.wxUser)
            }
          } else {
            wx.showToast({
              title: '登錄失敗',
              icon: 'none',
              duration: 2000,
              mask: true
            })
          }
        });
      }
    })
  },
  globalData: {
    userInfo: null
  }
})
 
4.別的頁面調(diào)用

onLoad: function () {
    let that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          //用戶授權(quán)了
          that.setData({
            isShowAuth: false
          })
          that.initData();
        } else {
          //用戶還沒授權(quán),彈出授權(quán)窗
          that.setData({
            isShowAuth: true
          })
        }
      }
    })
  }

相關(guān)案例查看更多