import { BASEURL } from './config' const GET = 'GET'; const POST = 'POST'; const PUT = 'PUT'; const FORM = 'FORM'; const DELETE = 'DELETE'; const request = (method, url, data, header = { 'content-type': 'application/json' }) => { return new Promise(function (resolve, reject) { wx.showNavigationBarLoading() //在标题栏中显示加载 wx.showLoading({ title: '加载中...', }) wx.request({ url: BASEURL + url, method: method, data: method === POST ? JSON.stringify(data) : data, header: header, success(res) { //请求成功 //判断状态码---errCode状态根据后端定义来判断 wx.hideLoading(); if (res.statusCode == 200) { resolve(res); } else { //其他异常 wx.showToast({ title: '网络异常,请稍后重试!', icon: 'none' }) reject('运行时错误,请稍后再试'); } }, fail(err) { //请求失败 // wx.showModal({ // showCancel: false, // confirmColor: '#1d8f59', // content: '数据加载失败,请检查您的网络,点击确定重新加载数据!', // success: function (res) { // if (res.confirm) { // request(method, url, data); // } // } // }); wx.hideLoading(); reject(err) }, complete() { wx.hideNavigationBarLoading(); //完成停止加载 wx.stopPullDownRefresh(); //停止下拉刷新 }, }) }) } export default request;