// 本地环境 //const $perfix = 'http://localhost:8111'; // 测试环境 const $perfix = 'http://114.135.61.188:58082'; // 演示环境 // const $perfix = 'https://water.zoniot.com'; // 正式环境 //const $perfix = 'https://water.zoniot.com'; const toLogin = (status = 0) => { if (status !== -406) return; wx.removeStorageSync('token'); getApp().token = ''; }; const httpFun = (url, data, type, isForm) => { let token = getApp().token || wx.getStorageSync('token'); return new Promise(resolve => { const filterUrl = [ '/user/openidLogin', ]; if (token && filterUrl.indexOf(url) == -1) { token = 'Bearer ' + token; } else { token = null; } wx.request({ url: `${$perfix}/api${url}`, data: data, method: type, header: { [token && "Authorization"]: token, [isForm && "Content-Type"]: "application/x-www-form-urlencoded" }, complete(res) { if (res.statusCode === 200) { toLogin(res.data.status); resolve(res.data); } else if (res.statusCode >= 500 && res.statusCode < 520) { wx.hideLoading(); wx.showModal({ title: '提示', content: '网络或服务异常,点击“确定”返回当前页', showCancel: false, success(res) { getApp().globalData = {}; getApp().getUserInfo(getApp().token); } }) resolve(res); return; } else { // wx.showToast({ // title: res.statusCode + '', // icon: 'none' // }) resolve(res); } } }) }) } module.exports = { domain: $perfix, postForm(url, data) { return httpFun(url, data, 'post', true); }, post(url, data) { return httpFun(url, data, 'post', false); }, deleteForm(url, data) { return httpFun(url, data, 'delete', true); }, get(url, data) { return httpFun(url, data, 'get', false); } }