http.ts 3.2 KB

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