簡單封裝一下 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);