request micro-channel program with a small cookie can request to know the server is requesting the same client. session_id will be unchanged, so good use of server-side session.
Write a utility function, you can use directly into the interface with wx.request
. Automatically sets and updates 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 };
Here is my own package 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, }
use:
var util = the require ( "../../../ utils / util"); // tools / * * * obtained favorites list listing * / getFavoriteShop: function (E) { wx.showToast ({title: ' loading ... ', icon:' loading '}); // display a loading block the let = that the this ; the let URL = + Host "API / Prints / User / favoriteshoplist" ; the let Method = Method; the let Data = { " user_id " : user_id, " Page ": the this .data.page, " size ": the 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: to true }); return to false ; } }). the catch ( function (RES) { wx.hideToast (); // hides loading frame // the console.log (RES); }); },
Turn: https://www.jianshu.com/p/b4740ac347cd