http.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { CustomRequestOptions } from '@/http/types'
  2. export function http<T>(options: CustomRequestOptions) {
  3. // 1. 返回 Promise 对象
  4. return new Promise<IResData<T>>((resolve, reject) => {
  5. uni.request({
  6. ...options,
  7. dataType: 'json',
  8. // #ifndef MP-WEIXIN
  9. responseType: 'json',
  10. // #endif
  11. // 响应成功
  12. success(res) {
  13. // 状态码 2xx,参考 axios 的设计
  14. if (res.statusCode >= 200 && res.statusCode < 300) {
  15. // 2.1 提取核心数据 res.data
  16. resolve(res.data as IResData<T>)
  17. }
  18. else if (res.statusCode === 401) {
  19. // 401错误 -> 清理用户信息,跳转到登录页
  20. // userStore.clearUserInfo()
  21. // uni.navigateTo({ url: '/pages/login/login' })
  22. reject(res)
  23. }
  24. else {
  25. // 其他错误 -> 根据后端错误信息轻提示
  26. !options.hideErrorToast
  27. && uni.showToast({
  28. icon: 'none',
  29. title: (res.data as IResData<T>).msg || '请求错误',
  30. })
  31. reject(res)
  32. }
  33. },
  34. // 响应失败
  35. fail(err) {
  36. uni.showToast({
  37. icon: 'none',
  38. title: '网络错误,换个网络试试',
  39. })
  40. reject(err)
  41. },
  42. })
  43. })
  44. }
  45. /**
  46. * GET 请求
  47. * @param url 后台地址
  48. * @param query 请求query参数
  49. * @param header 请求头,默认为json格式
  50. * @returns
  51. */
  52. export function httpGet<T>(url: string, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
  53. return http<T>({
  54. url,
  55. query,
  56. method: 'GET',
  57. header,
  58. ...options,
  59. })
  60. }
  61. /**
  62. * POST 请求
  63. * @param url 后台地址
  64. * @param data 请求body参数
  65. * @param query 请求query参数,post请求也支持query,很多微信接口都需要
  66. * @param header 请求头,默认为json格式
  67. * @returns
  68. */
  69. export function httpPost<T>(url: string, data?: Record<string, any>, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
  70. return http<T>({
  71. url,
  72. query,
  73. data,
  74. method: 'POST',
  75. header,
  76. ...options,
  77. })
  78. }
  79. /**
  80. * PUT 请求
  81. */
  82. export function httpPut<T>(url: string, data?: Record<string, any>, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
  83. return http<T>({
  84. url,
  85. data,
  86. query,
  87. method: 'PUT',
  88. header,
  89. ...options,
  90. })
  91. }
  92. /**
  93. * DELETE 请求(无请求体,仅 query)
  94. */
  95. export function httpDelete<T>(url: string, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
  96. return http<T>({
  97. url,
  98. query,
  99. method: 'DELETE',
  100. header,
  101. ...options,
  102. })
  103. }
  104. http.get = httpGet
  105. http.post = httpPost
  106. http.put = httpPut
  107. http.delete = httpDelete
  108. // 支持与 alovaJS 类似的API调用
  109. http.Get = httpGet
  110. http.Post = httpPost
  111. http.Put = httpPut
  112. http.Delete = httpDelete