微信小程序帶cookie的request請求代碼封裝(小程序使用session)


微信小程序帶cookie的request請求可,以使服務端知道是同一個客戶端請求. session_id會不變,從而很好的使用服務端的session.

寫一個工具函數,直接導入使用即可,接口同 wx.request 。會自動設置和更新 cookie。

const request = function (obj) {
    //設置cookie緩存
    if(obj.fail){
        obj.fail = function(err){
            wx.setStorageSync('cookie', err.header['Set-Cookie']);
            obj.fail(err);
        };
    }
    else{
        obj.fail = function (err) {
            wx.setStorageSync('cookie', err.header['Set-Cookie']);
        };
    }
    if(obj.success){
        obj.success = function (res) {
            wx.setStorageSync('cookie', res.header['Set-Cookie']);
            obj.success(res);
        };
    }
    else{
        obj.success = function (res) {
            wx.setStorageSync('cookie', res.header['Set-Cookie']);
        };
    }

    //設置請求頭
    if(obj.header){
        obj.header = {
            'Cookie': wx.getStorageSync('cookie'),
            "Content-Type": "application/x-www-form-urlencoded",
            ...obj.header
        };
    }
    else{
        obj.header = {
            'Cookie': wx.getStorageSync('cookie'),
            "Content-Type": "application/x-www-form-urlencoded",
        };
    }

    wx.request(obj);
};

module.exports = {
    request: request
};

 

下面是我自己封裝的 util.js :

const Promise = require('./es6-promise.js');

//封裝Request請求方法
function requst(url, method, data = {}) {
  const app = getApp();
  if (app.globalData.token)
  {
    //console.log("token有值:" + app.globalData.token);
    data.token = app.globalData.token;
  }

  wx.showNavigationBarLoading()
  data.method = method
  return new Promise((resove, reject) => {
    wx.request({
      url: url,
      data: data,
      header: {
        'Cookie': wx.getStorageSync('cookie'),
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        wx.hideNavigationBarLoading()
        wx.setStorageSync('cookie', res.header['Set-Cookie']);
        resove(res.data)
      },
      fail: function (msg) {
        console.log('reqest error', msg)
        wx.hideNavigationBarLoading()
        wx.setStorageSync('cookie', res.header['Set-Cookie']);
        reject('fail')
      }
    })
  })
}


module.exports = {
  requst: requst,

}

 

使用:

var util = require("../../../utils/util");//工具類


/**
   * 獲得收藏商家列表
   */
  getFavoriteShop: function (e) {
    wx.showToast({ title: '加載中...', icon: 'loading' });//顯示加載框
    let that = this;
    let url = host + "api/prints/user/favoriteshoplist";
    let method = method;
    let data = {
      "user_id": user_id,
      "page": this.data.page,
      "size": this.data.size,
    }
    let result = util.requst(url, method, data);
    result.then(function (res) {
      wx.hideToast();//隱藏加載框
      console.log(res);
      if (res.code == 0) {
        let shop_list = res.data;
        //數組合並
        that.data.shop_list = that.data.shop_list.concat(shop_list);
        that.data.page++;
        that.setData({ shop_list: that.data.shop_list, page: that.data.page });
      }
      else {
        wx.showToast({ title: res.msg, icon: 'none', duration: 1000, mask: true });
        return false;
      }

    }).catch(function (res) {
      wx.hideToast();//隱藏加載框
      //console.log(res);
    });
  },

 

 

轉: https://www.jianshu.com/p/b4740ac347cd


免責聲明!

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



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