四零语境前端代码仓库
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.

126 lines
3.0 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. 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
  26. }
  27. }
  28. return new Promise((resolve, reject) => {
  29. uni.request({
  30. url: config.baseURL + url,
  31. method,
  32. data,
  33. header: {
  34. 'Content-Type': 'application/x-www-form-urlencoded',
  35. ...header
  36. },
  37. success: (res) => {
  38. console.log(`Success ${method} ${url}`, res);
  39. // 优先处理业务逻辑响应
  40. if (res.statusCode === 200 && res.data) {
  41. // 业务成功
  42. if (res.data.code === 200) {
  43. if (showLoading) {
  44. uni.hideLoading()
  45. }
  46. resolve(res.data)
  47. return
  48. }
  49. // 业务失败但有具体错误信息
  50. const errorMsg = res.data.message || '请求失败'
  51. if (showToast) {
  52. if (showLoading) {
  53. uni.hideLoading()
  54. }
  55. uni.showToast({
  56. title: errorMsg,
  57. icon: 'none',
  58. duration: 3000
  59. })
  60. }
  61. reject({
  62. code: res.data.code,
  63. message: errorMsg,
  64. data: res.data
  65. })
  66. return
  67. }
  68. // 处理HTTP状态码错误(无有效响应体的情况)
  69. const error = {
  70. code: res.statusCode,
  71. message: '网络请求错误'
  72. }
  73. switch (res.statusCode) {
  74. case 401:
  75. case 403:
  76. uni.removeStorageSync('token')
  77. uni.reLaunch({ url: '/subPages/login/login' })
  78. error.message = '登录已过期,请重新登录'
  79. break;
  80. case 404:
  81. error.message = '资源不存在'
  82. break;
  83. case 500:
  84. error.message = '服务器错误'
  85. break;
  86. }
  87. if (showToast) {
  88. if (showLoading) {
  89. uni.hideLoading()
  90. }
  91. uni.showToast({
  92. title: error.message,
  93. icon: 'none'
  94. })
  95. }
  96. reject(error)
  97. },
  98. fail: (err) => {
  99. console.log(`Fail ${method} ${url}`, err);
  100. const errorMsg = err.errMsg || '请求失败'
  101. if (showToast) {
  102. if (showLoading) {
  103. uni.hideLoading()
  104. }
  105. uni.showToast({
  106. title: errorMsg,
  107. icon: 'none'
  108. })
  109. }
  110. reject({
  111. code: -1,
  112. message: errorMsg,
  113. data: err
  114. })
  115. },
  116. })
  117. })
  118. }