request.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. let ajaxTimes = 0;
  2. const request = (url, data, method = 'GET') => {
  3. return new Promise((resolve, reject) => {
  4. let header = {
  5. 'Content-Type': 'application/json',
  6. }
  7. if (uni.getStorageSync('token')) {
  8. header.token = 'Bearer ' + uni.getStorageSync('token')
  9. }
  10. if (url.indexOf('app/product/user/auctionProductDetail/biddingHall/') == -1) {
  11. ajaxTimes++;
  12. uni.showLoading({
  13. title: "加载中",
  14. mask: true,
  15. });
  16. }
  17. uni.request({
  18. url: url,
  19. data: data,
  20. method: method,
  21. header: header,
  22. success: res => {
  23. if (res.statusCode == 200) {
  24. let data = res.data
  25. if (data.code == 40001 || data.code == 40002 || data.code == 40003 || data.code == 40004 || data.code ==
  26. 40005) {
  27. uni.clearStorageSync()
  28. uni.navigateTo({
  29. url: '/pages/index/login'
  30. });
  31. return false
  32. }
  33. if (data.code == 0 || data.code == 200) {
  34. resolve(res.data)
  35. } else {
  36. reject(res)
  37. }
  38. } else {
  39. reject(res)
  40. }
  41. },
  42. fail: res => {
  43. reject(res)
  44. },
  45. complete: () => {
  46. if (url.indexOf('app/product/user/auctionProductDetail/biddingHall/') == -1) {
  47. ajaxTimes--;
  48. }
  49. if (ajaxTimes === 0) {
  50. uni.hideLoading();
  51. }
  52. }
  53. })
  54. });
  55. }
  56. module.exports = {
  57. request: request
  58. }