request.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. title: res.data,
  33. icon: 'none'
  34. })
  35. reject('运行时错误,请稍后再试');
  36. }
  37. },
  38. fail(err) {
  39. //请求失败
  40. // wx.showModal({
  41. // showCancel: false,
  42. // confirmColor: '#1d8f59',
  43. // content: '数据加载失败,请检查您的网络,点击确定重新加载数据!',
  44. // success: function (res) {
  45. // if (res.confirm) {
  46. // request(method, url, data);
  47. // }
  48. // }
  49. // });
  50. wx.hideLoading();
  51. reject(err)
  52. },
  53. complete() {
  54. wx.hideNavigationBarLoading(); //完成停止加载
  55. wx.stopPullDownRefresh(); //停止下拉刷新
  56. },
  57. })
  58. })
  59. }
  60. export default request;