const APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8"; const APPLICATION_JSON_UTF8_FORM = 'application/x-www-form-urlencoded;charset=UTF-8'; const RESPONSE_OK = 20000; const TOKEN_BEOVERDUE = 40001; const TOKEN = wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'); //测试环境 // const HOST = 'http://192.168.1.106:9901'; //正式环境 const HOST = 'http://wordpad-api-test.efunbox.cn'; //API接口 const interfaces = { //这里边放入请求的接口 //根据userId获取用户信息 getUserById: '/tch/user/getUserById' } function fun(url: String, type: any, data: any, otherType: any) { if (!TOKEN) { //这里可以做一个请求的拦截 看有没有携带token console.log("Token:", TOKEN) } let promise = new Promise((resolve, reject) => { wx.request({ url: HOST + url, method: type, data: type == 'GET' ? data : JSON.stringify(data), header: { 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE, 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'), 'userId': TOKEN }, success: function (res: any) { wx.stopPullDownRefresh(); if (res) { resolve(res) } else { wx.showToast({ title: res.data.subMsg ? res.data.subMsg : res.data.msg, icon: 'none', duration: 2000 }) } }, fail: function (res: any) { wx.stopPullDownRefresh(); reject(res) } }) }) return promise } export const httpUtil = { interfaces, wxGet: function (url: String, data: any) { return fun(url, 'GET', data, null) }, wxPost: function (url: String, data: any) { return fun(url, 'POST', data, null) }, wxFormPost: function (url: String, data: any) { return fun(url, 'POST', data, 'formPost') }, wxPut: function (url: String, data: any) { return fun(url, 'PUT', data, null) }, wxDel: function (url: String, data: any) { return fun(url, 'DELETE', data, null) } }