简单封装一下 uni-app 的请求,因为项目中只用 post 请求,所以只封装了 post 和 all 方法。
更新,新增 spread 方法 2019-11-22 10:37:21
global.http = { timer: null, beenLoginPage: false, post: (url, data, loading) => { if (loading) { uni.showLoading({ mask: true }) } return new Promise((resolve, reject) => { uni.request({ url: BaseUrl + url, method: "POST", header: { sessid: getSessid() }, data, success: res => { if (loading) { uni.hideLoading(); } if (res.data.code === 300) { $Toast('登录过期,请重新登录!'); removeItem('sessid'); if (http.beenLoginPage) return; if (http.timer != null) { clearTimeout(http.timer); }; http.timer = setTimeout(function() { http.timer = null; uni.reLaunch({ url: '/pages/login/login' }) http.beenLoginPage = true; setTimeout(() => { http.beenLoginPage = false; }, 600000) }, 1500); return; } if (res.statusCode != 200 || res.data.code != 200) { uni.showToast({ title: res.data.msg || '网络异常', duration: 1500, icon: "none" }) return; } resolve(res); }, fail(err) { if (loading) { uni.hideLoading(); } $Toast('网络异常!'); reject(err); } }) }) }, all: (promise) => { return Promise.all(promise); }, spread: function(callback) { return function wrap(arr) { callback.apply(null, arr); }; } }
2019-11-22 13:09:27
尝试理解 spread 函数
var args = [1, 2, 3]; function spread(cb){ return function wrap(arr){ cb.apply(null,arr); } } (spread((x,y,z)=>{ console.log('x->'+x); console.log('y->'+y); console.log('z->'+z); }))(args);