request.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 ='Bearer ' + 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. uni.reLaunch({
  28. url: '/pages/login/index'
  29. });
  30. return false
  31. }
  32. if (data.code == 200 || data.status == 200) {
  33. resolve(res.data)
  34. } else {
  35. reject(res.data)
  36. }
  37. } else {
  38. reject(res.data)
  39. }
  40. },
  41. fail: res => {
  42. console.log('11111')
  43. reject(res.data)
  44. },
  45. complete: () => {
  46. requestTimes--;
  47. if (requestTimes === 0) {
  48. uni.hideLoading();
  49. }
  50. }
  51. })
  52. });
  53. }
  54. module.exports = {
  55. request: request
  56. }