import { host } from '../config.js' const request = options => { return new Promise((resolve, reject) => { const { data, method, url, header } = options if (data && method !== 'get'&&!header) { options.data = JSON.stringify(data) } options.url = `${host}` + url wx.request({ header: header?header:{ 'Content-Type': 'application/json' }, ...options, success: function (res) { // console.log(res); // wx.request成功发出了请求,无论返回什么http状态码,都会走success if (res.statusCode === 200) { resolve(res.data) } else { // reject(res.data) wx.showToast({ title: res.data.error, icon: 'none', duration: 2000 }) } }, fail: function (res) { // 遇到断网,域名解析有问题,或者尤其是我们去调用restful api时,可能会在url格式,参数类型上出些问题,这些情况下才会调用到fail reject(res.data) wx.hideLoading() wx.showToast({ title: '接口调用失败', icon: 'none', duration: 2000 }) } }) }) } export default request