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 = 'https://wordpad-api-test.efunbox.cn'; //正式环境 // const HOST = 'https://wordpad-api-test.efunbox.cn'; const httpData = { userId: 'test' } //API接口 const interfaces = { //这里边放入请求的接口 //获取全国地址 getOrganizeAll:'/organize/all', //获取学校信息和班级信息(根据type判断) getSchoolOrClass:'/school', //手机号登录注册 phoneRegister: '/tch/user/register', //账号密码登录 pwdPhoneLogin: '/tch/user/login', //根据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': httpData.userId }, 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 = { httpData, 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) } }