//项目URL相同部分,减轻代码量,同时方便项目迁移 //这里因为我是本地调试,所以host不规范,实际上应该是你备案的域名信息 var host = 'http://sjhl.brotop.cn'; // var host = 'http://happy.brofirst.cn'; let loginFail = 0; /** * POST请求, * URL:接口 * postData:参数,json类型 * doSuccess:成功的回调函数 * doFail:失败的回调函数 */ function postRequest(url, postData, doSuccess) { wx.showLoading({ title: '加载中', }) wx.request({ url: host + url, header: { 'Authorization': wx.getStorageSync('Authorization') || '' }, data: postData, method: 'POST', success: function (res) { setTimeout(()=>{ wx.hideLoading() },800) if(res.data.msg.indexOf('过期')>-1||res.data.msg.indexOf('请登录')>-1){ loginFail++; if(loginFail<2){ setTimeout(()=>{ wx.redirectTo({ url: '/pages/index/index', }) },1200) } }else{ doSuccess(res.data); } }, fail: function () { console.log('调接口失败') }, }) } //不需要token的post请求 function postRequestTwo(url, postData, doSuccess) { wx.request({ //项目的真正接口,通过字符串拼接方式实现 url: host + url, data: postData, method: 'POST', success: function (res) { doSuccess(res.data); }, fail: function () { console.log('调接口失败') }, }) } // 不需要传参的get请求 function getRequest(url, doSuccess) { wx.request({ url: host + url, method: "Get", header: { 'Authorization': wx.getStorageSync('Authorization') || '' }, success: function (res) { if(res.data.code !=0){ doSuccess(res.data); }else{ wx.showToast({ title: res.data.msg, icon:'none' }) setTimeout(()=>{ wx.redirectTo({ url: '/pages/index/index', }) },1200) } // doSuccess(res.data); }, fail: function () { }, }) } // 不需要传参的delete请求 function deleteRequest(url, doSuccess) { wx.request({ url: host + url, method: "DELETE", header: { 'Authorization': wx.getStorageSync('Authorization') || '' }, success: function (res) { if(res.data.code !=0){ doSuccess(res.data); }else{ wx.showToast({ title: res.data.msg, icon:'none' }) setTimeout(()=>{ wx.redirectTo({ url: '/pages/index/index', }) },1200) } // doSuccess(res.data); }, fail: function () { }, }) } module.exports.postRequest = postRequest; module.exports.postRequestTwo= postRequestTwo; module.exports.getRequest = getRequest; module.exports.deleteRequest=deleteRequest;