http.ts 3.1 KB

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