request.js 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. ajaxTimes++;
  11. uni.showLoading({
  12. title: "加载中",
  13. mask: true,
  14. });
  15. uni.request({
  16. url: url,
  17. data: data,
  18. method: method,
  19. header: header,
  20. success: res => {
  21. if (res.statusCode == 200) {
  22. let data = res.data
  23. if (data.code == 40001) {
  24. uni.clearStorageSync()
  25. uni.navigateTo({
  26. url: '/pages/index/index'
  27. });
  28. return false
  29. }
  30. if (data.code == 0 || data.code == 200) {
  31. resolve(res.data)
  32. } else {
  33. reject(res)
  34. }
  35. } else {
  36. reject(res)
  37. }
  38. },
  39. fail: res => {
  40. reject(res)
  41. },
  42. complete: () => {
  43. ajaxTimes--;
  44. if (ajaxTimes === 0) {
  45. uni.hideLoading();
  46. }
  47. }
  48. })
  49. });
  50. }
  51. module.exports = {
  52. request: request
  53. }