http.js 1008 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const BASE_URL = import.meta.env.VITE_API_BASE_URL || ''
  2. export const http = (options) => {
  3. const {
  4. url,
  5. method = 'GET',
  6. data = {},
  7. header = {},
  8. showLoading = true
  9. } = options
  10. if (showLoading) {
  11. uni.showLoading({
  12. title: '加载中...',
  13. mask: true
  14. })
  15. }
  16. return new Promise((resolve, reject) => {
  17. uni.request({
  18. url: BASE_URL + url,
  19. method,
  20. data,
  21. header: {
  22. 'Content-Type': 'application/json',
  23. ...header
  24. },
  25. success: (res) => {
  26. if (res.statusCode === 200) {
  27. resolve(res.data)
  28. } else {
  29. uni.showToast({
  30. title: '请求失败',
  31. icon: 'none'
  32. })
  33. reject(res)
  34. }
  35. },
  36. fail: (err) => {
  37. uni.showToast({
  38. title: '网络错误',
  39. icon: 'none'
  40. })
  41. reject(err)
  42. },
  43. complete: () => {
  44. if (showLoading) {
  45. uni.hideLoading()
  46. }
  47. }
  48. })
  49. })
  50. }