http.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { CustomRequestOptions } from '@/interceptors/request'
  2. import { useUserStore } from '@/store'
  3. import { currRoute, currFullPath } from "@/utils"
  4. export const http = <T>(options: CustomRequestOptions) => {
  5. // 1. 返回 Promise 对象
  6. return new Promise<IResData<T>>((resolve, reject) => {
  7. let temp = {
  8. ...options,
  9. dataType: 'json',
  10. // #ifndef MP-WEIXIN
  11. responseType: 'json',
  12. // #endif
  13. // 响应成功
  14. success(res) {
  15. // console.log("options.url:", options.url);
  16. // console.log("currRoute:", currRoute());
  17. // console.log("currFullPath:", currFullPath());
  18. // 状态码 2xx
  19. if (res.statusCode >= 200 && res.statusCode < 300) {
  20. // 特殊处理(非登录页 && 使用自动登录异常)
  21. if (options.url == "/api/wx/mini/login" && currRoute().path != "/pages/login/index" && res.statusCode == 200 && res.data.body.res == '1') {
  22. uni.setStorageSync("redirectPath", currFullPath())
  23. reject(res)
  24. }
  25. // 提取核心数据 res.data
  26. resolve(res.data as IResData<T>)
  27. } else if (res.statusCode === 401) {
  28. useUserStore().setToken("")
  29. if (options.url == "/api/wx/mini/login") {
  30. // 401错误(登录接口) -> 清理用户信息,跳转到登录页
  31. uni.setStorageSync("redirectPath", currFullPath())
  32. useUserStore().clearUserInfo()
  33. uni.reLaunch({ url: '/pages/login/index?isLogout=1' })
  34. reject(res)
  35. } else {
  36. // 401错误(非登录接口) -> 自动刷新token
  37. useUserStore().wxLogin().then((token) => {
  38. temp.header['X-ACCESS-TOKEN'] = token
  39. uni.request(temp)
  40. }).catch(() => {
  41. reject(res)
  42. })
  43. }
  44. } else {
  45. // 其他错误 -> 根据后端错误信息轻提示
  46. !options.hideErrorToast &&
  47. uni.showToast({
  48. icon: 'none',
  49. duration: 5000,
  50. title: (res.data as IResData<T>).message || '请求错误',
  51. })
  52. reject(res)
  53. }
  54. },
  55. // 响应失败
  56. fail(err) {
  57. if (!options.hideErrorToast) {
  58. uni.showToast({
  59. icon: 'none',
  60. title: '网络错误,换个网络试试',
  61. })
  62. }
  63. reject(err)
  64. },
  65. }
  66. uni.request(temp)
  67. })
  68. }
  69. /**
  70. * GET 请求
  71. * @param url 后台地址
  72. * @param query 请求query参数
  73. * @returns
  74. */
  75. export const httpGet = <T>(url: string, query?: Record<string, any>) => {
  76. return http<T>({
  77. url,
  78. query,
  79. method: 'GET',
  80. })
  81. }
  82. /**
  83. * POST 请求
  84. * @param url 后台地址
  85. * @param data 请求body参数
  86. * @param query 请求query参数,post请求也支持query,很多微信接口都需要
  87. * @returns
  88. */
  89. export const httpPost = <T>(
  90. url: string,
  91. data?: Record<string, any>,
  92. query?: Record<string, any>,
  93. ) => {
  94. return http<T>({
  95. url,
  96. query,
  97. data,
  98. method: 'POST',
  99. })
  100. }
  101. http.get = httpGet
  102. http.post = httpPost