request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // uni-app请求封装
  2. export default class Request {
  3. static loadingCount = 0;
  4. static excludePaths = []; // 排除的路径列表
  5. static excludePages = []; // 排除的页面列表
  6. // 添加排除路径(支持正则表达式)
  7. static addExcludePath(path) {
  8. this.excludePaths.push(path);
  9. }
  10. // 添加排除页面(页面路径)
  11. static addExcludePage(page) {
  12. this.excludePages.push(page);
  13. }
  14. // 检查当前页面是否在排除列表中
  15. static isCurrentPageExcluded() {
  16. const pages = getCurrentPages();
  17. if (pages.length === 0) return false;
  18. const currentPage = pages[pages.length - 1];
  19. const route = currentPage.route || currentPage.__route__;
  20. return this.excludePages.some(excludePage =>
  21. route.includes(excludePage) || excludePage.includes(route)
  22. );
  23. }
  24. // 检查路径是否在排除列表中
  25. static isPathExcluded(path) {
  26. return this.excludePaths.some(excludePath => {
  27. if (excludePath instanceof RegExp) {
  28. return excludePath.test(path);
  29. }
  30. return path.includes(excludePath) || excludePath.includes(path);
  31. });
  32. }
  33. http(router, data = {}, method, contentType, showLoading = true) {
  34. let that = this;
  35. // let path = 'https://live.test.ylrztop.com/live-api'; // 余红奇
  36. let path = 'http://192.168.10.166:7114'; // 余红奇
  37. // 检查是否应该显示loading
  38. const shouldShowLoading = showLoading &&
  39. !Request.isPathExcluded(router) &&
  40. !Request.isCurrentPageExcluded();
  41. // 只在第一个请求且需要显示loading时显示
  42. if (shouldShowLoading && Request.loadingCount === 0) {
  43. uni.showLoading({
  44. title: '加载中',
  45. mask: true
  46. });
  47. }
  48. if (shouldShowLoading) {
  49. Request.loadingCount++;
  50. }
  51. return new Promise((resolve, reject) => {
  52. let token = uni.getStorageSync('AppToken');
  53. var httpContentType = 'application/x-www-form-urlencoded';
  54. if (contentType != undefined) {
  55. httpContentType = contentType;
  56. }
  57. uni.request({
  58. header: {
  59. 'Content-Type': httpContentType,
  60. 'AppToken': token
  61. },
  62. url: `${path}${router}`,
  63. data: data,
  64. method: method,
  65. success: (res) => {
  66. if (res.code == 401) {
  67. let pages = getCurrentPages();
  68. let url = pages[pages.length - 1];
  69. if (url != undefined && url.route == "/pages/home/index") {
  70. resolve(res.data)
  71. return;
  72. }
  73. uni.reLaunch({
  74. url: '/pages/home/index'
  75. });
  76. return;
  77. }
  78. if (res.token) {
  79. uni.setStorageSync('AppToken', res.token)
  80. }
  81. resolve(res.data)
  82. },
  83. fail: (res) => {
  84. reject(res);
  85. },
  86. complete: (res) => {
  87. // 只有需要显示loading的请求才减少计数器
  88. if (shouldShowLoading) {
  89. Request.loadingCount--;
  90. if (Request.loadingCount <= 0) {
  91. uni.hideLoading();
  92. Request.loadingCount = 0;
  93. }
  94. }
  95. }
  96. })
  97. })
  98. }
  99. }