request.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {
  2. BASEURL
  3. } from './config'
  4. const GET = 'GET';
  5. const POST = 'POST';
  6. const PUT = 'PUT';
  7. const FORM = 'FORM';
  8. const DELETE = 'DELETE';
  9. const request = (method, url, data, header = {
  10. 'content-type': 'application/json'
  11. }) => {
  12. return new Promise(function (resolve, reject) {
  13. wx.showNavigationBarLoading() //在标题栏中显示加载
  14. wx.showLoading({
  15. title: '加载中...',
  16. })
  17. wx.request({
  18. url: BASEURL + url,
  19. method: method,
  20. data: method === POST ? JSON.stringify(data) : data,
  21. header: header,
  22. success(res) {
  23. //请求成功
  24. //判断状态码---errCode状态根据后端定义来判断
  25. wx.hideLoading();
  26. if (res.statusCode == 200) {
  27. resolve(res);
  28. } else {
  29. //其他异常
  30. wx.showToast({
  31. title: '网络异常,请稍后重试!',
  32. icon: 'none'
  33. })
  34. reject('运行时错误,请稍后再试');
  35. }
  36. },
  37. fail(err) {
  38. //请求失败
  39. // wx.showModal({
  40. // showCancel: false,
  41. // confirmColor: '#1d8f59',
  42. // content: '数据加载失败,请检查您的网络,点击确定重新加载数据!',
  43. // success: function (res) {
  44. // if (res.confirm) {
  45. // request(method, url, data);
  46. // }
  47. // }
  48. // });
  49. wx.hideLoading();
  50. reject(err)
  51. },
  52. complete() {
  53. wx.hideNavigationBarLoading(); //完成停止加载
  54. wx.stopPullDownRefresh(); //停止下拉刷新
  55. },
  56. })
  57. })
  58. }
  59. export default request;