request.js 1.0 KB

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