四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.2 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import config from "@/config";
  2. export default function request({
  3. url = '',
  4. method = 'GET',
  5. data = {},
  6. showLoading = false,
  7. header = {},
  8. needToken = false, // 需要token
  9. showToast = true // 默认显示失败的提示
  10. }) {
  11. if (showLoading) uni.showLoading({ title: '加载中' })
  12. if (needToken) {
  13. const token = uni.getStorageSync('token')
  14. if (token) {
  15. header['X-Access-Token'] = token
  16. } else {
  17. if (showLoading) {
  18. uni.hideLoading()
  19. }
  20. uni.showToast({
  21. title: '请先登录',
  22. icon: 'none'
  23. })
  24. uni.reLaunch({ url: '/subPages/login/login' })
  25. return Promise.reject({
  26. code: 401,
  27. message: '请先登录'
  28. })
  29. }
  30. }
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. url: config.baseURL + url,
  34. method,
  35. data,
  36. header: {
  37. 'Content-Type': 'application/x-www-form-urlencoded',
  38. ...header
  39. },
  40. success: (res) => {
  41. console.log(`Success ${method} ${url}`, res);
  42. // 优先处理业务逻辑响应
  43. if (res.statusCode === 200 && res.data) {
  44. // 业务成功
  45. if (res.data.code === 200) {
  46. if (showLoading) {
  47. uni.hideLoading()
  48. }
  49. resolve(res.data)
  50. return
  51. }
  52. // 业务失败但有具体错误信息
  53. const errorMsg = res.data.message || '请求失败'
  54. if (showToast) {
  55. if (showLoading) {
  56. uni.hideLoading()
  57. }
  58. uni.showToast({
  59. title: errorMsg,
  60. icon: 'none',
  61. duration: 3000
  62. })
  63. }
  64. reject({
  65. code: res.data.code,
  66. message: errorMsg,
  67. data: res.data
  68. })
  69. return
  70. }
  71. // 处理HTTP状态码错误(无有效响应体的情况)
  72. const error = {
  73. code: res.statusCode,
  74. message: '网络请求错误'
  75. }
  76. switch (res.statusCode) {
  77. case 401:
  78. uni.reLaunch({ url: '/subPages/login/login' })
  79. error.message = '请先登录'
  80. break;
  81. case 403:
  82. uni.removeStorageSync('token')
  83. uni.reLaunch({ url: '/subPages/login/login' })
  84. error.message = '登录已过期,请重新登录'
  85. break;
  86. case 404:
  87. error.message = '资源不存在'
  88. break;
  89. case 500:
  90. error.message = '服务器错误'
  91. break;
  92. }
  93. if (showToast) {
  94. if (showLoading) {
  95. uni.hideLoading()
  96. }
  97. uni.showToast({
  98. title: error.message,
  99. icon: 'none'
  100. })
  101. }
  102. reject(error)
  103. },
  104. fail: (err) => {
  105. console.log(`Fail ${method} ${url}`, err);
  106. const errorMsg = err.errMsg || '请求失败'
  107. if (showToast) {
  108. if (showLoading) {
  109. uni.hideLoading()
  110. }
  111. uni.showToast({
  112. title: errorMsg,
  113. icon: 'none'
  114. })
  115. }
  116. reject({
  117. code: -1,
  118. message: errorMsg,
  119. data: err
  120. })
  121. },
  122. })
  123. })
  124. }