request.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. let requestTimes = 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.Authorization = uni.getStorageSync('token')
  9. }
  10. if (requestTimes === 0) {
  11. uni.showLoading({
  12. title: '加载中',
  13. mask: true,
  14. });
  15. }
  16. requestTimes++;
  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.status == 30001) {
  26. uni.removeStorageSync('token')
  27. if(url.indexOf('teamStatistics')==-1&&url.indexOf('center/clueRait')==-1){
  28. uni.navigateTo({
  29. url: '/pages/login/index'
  30. });
  31. }
  32. return false
  33. }
  34. if (data.status == 10000) {
  35. resolve(res.data)
  36. } else {
  37. reject(res.data)
  38. }
  39. } else {
  40. reject(res.data)
  41. }
  42. },
  43. fail: res => {
  44. reject(res.data)
  45. },
  46. complete: () => {
  47. requestTimes--;
  48. if (requestTimes === 0) {
  49. uni.hideLoading();
  50. }
  51. }
  52. })
  53. });
  54. }
  55. module.exports = {
  56. request: request
  57. }