In the small program development will inevitably use the interface request, the interface request package is delicate and easy to use is an excellent small program developers indispensable quality.
const app = getApp(); let { baseUrl } = app.globalData; const loginUrl = '/pages/loginIteration/loginMethod'; // login address // log const log = wx.getrealtimelogManager? wx.getRealtimeLogManager() : null const err_log = () => { if (! log) return log.error.apply(log, Arguments)} const getCurrentPath = () => {const history = getCurrentPages(); const currentPage = history[history.length - 1]; return currentPage.route; Const startLoading = () =>{wx.showloading ({title: 'loading... ', icon: 'none', mask: true }) } const endLoading = () =>{ wx.hideLoading(); } needLoadingRequestCount = 0; const showFullScreenLoading = () => { if (needLoadingRequestCount === 0) { startLoading(); } needLoadingRequestCount++; }; const tryHideFullScreenLoading = () =>{ needLoadingRequestCount--; if (needLoadingRequestCount <= 1) { endLoading(); return; }; if (needLoadingRequestCount === 0) { endLoading(); }}; const request = (url, method, data, header) => { return new Promise((resolve, reject) => { wx.request({ url: baseUrl + url, header: header, data: data, method: method, success: (res) => { resolve(res); }, fail: (res) => { reject(res); }}); })} / / get/post requests const fun = async (url, method, params, header, the -quiet = false,) = > {params = params | | {}; header = header || {}; Const accountInfo = wx.getStoragesync ("accountInfo"); // Add a header parameter based on back-end requirements const accountInfo = wx.getStoragesync ("accountInfo"); header.token = accountInfo.token || ''; header.userId = accountInfo.userId || ''; if(! quiet) { wx.showNavigationBarLoading(); showFullScreenLoading(); } try { const { data: resData } = await request(url, method, params, header); if (! quiet) { tryHideFullScreenLoading(); wx.hideNavigationBarLoading(); } const { code, errCode, msg, data, returnCode } = resData; if (code === '1' || returnCode === 200) { return Promise.resolve(resData); } else if (code === '1110001') {wx.showtoast ({title: 'you have been forced offline ', icon: "none"}); wx.redirectTo({ url: loginUrl }); Wx.removestoragesync ('accountInfo'); // Delete all user information when forced offline. return Promise.reject(resData); } else { wx.showToast({ title: msg, icon: "none", }); return Promise.reject(resData); } } catch (err) { if(! quiet) { tryHideFullScreenLoading(); wx.hideNavigationBarLoading(); Wx.showtoast ({title: 'server exception, please try again later ', icon: "None ",}); } return Promise.reject(err); }} const uploadFile = (url, filePath, name = 'file') => {let header = {}; wx.showNavigationBarLoading(); showFullScreenLoading(); let promise = new Promise( (resolve, reject) => { wx.uploadFile({ url: baseUrl + url, filePath: filePath, name: name, header: header, success: (res) => { resolve(res.data); tryHideFullScreenLoading(); }, fail: () = > {reject ({error: 'network error code: 0}). tryHideFullScreenLoading(); }, complete: () => { wx.hideNavigationBarLoading(); wx.hideLoading(); }}); }); return promise; } module.exports = { "get": (url, data, header) => { return fun(url, "GET", data, header); }, "post": (url, data) => { return fun(url, "POST", data, { 'Content-Type': 'application/x-www-form-urlencoded' }); }, "postQuiet": (url, data) => { return fun(url, "POST", data, { 'Content-Type': 'application/x-www-form-urlencoded' }, true); }, 'postJSON': (url, data) => { return fun(url, 'POST', data, { 'content-type': 'application/json'}) }, 'postJSONQuiet': (url, data) => { return fun(url, 'POST', data, { 'content-type': 'application/json'}, true) }, upload: (url, filePath, name ) => { return uploadFile(url, filePath, name); }};Copy the code