http.ts 3.1 KB

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