http.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { refreshToken } from '../core/libs/requests'
  2. import { CustomRequestOptions } from '../interceptors/request'
  3. import { useUserStore } from '../store'
  4. import dayjs from 'dayjs'
  5. export const http = async <T>(options: CustomRequestOptions) => {
  6. const userStore = useUserStore()
  7. const Authorization = userStore.userInfo?.token
  8. const header: { 'tenant-id': number; Authorization?: string } = {
  9. 'tenant-id': 1,
  10. Authorization,
  11. }
  12. // console.log(dayjs(userStore.userInfo.expiresTime).format('YYYY-MM-DD HH:mm:ss'))
  13. const diff = dayjs(userStore.userInfo.expiresTime).diff(dayjs(), 'minutes')
  14. // console.log(diff)
  15. if (userStore.isLogined && diff < 10 && options.url !== '/app-api/member/auth/refresh-token') {
  16. const { data } = await refreshToken(userStore.userInfo.refreshToken)
  17. userStore.setUserInfo({
  18. ...userStore.userInfo,
  19. token: data.accessToken,
  20. userId: data.userId,
  21. accessToken: data.accessToken,
  22. refreshToken: data.refreshToken,
  23. expiresTime: data.expiresTime,
  24. // openid: data.openid,
  25. })
  26. }
  27. // 1. 返回 Promise 对象
  28. return new Promise<IResData<T>>((resolve, reject) => {
  29. uni.request({
  30. ...options,
  31. header,
  32. dataType: 'json',
  33. // #ifndef MP-WEIXIN
  34. responseType: 'json',
  35. // #endif
  36. // 响应成功
  37. success(res) {
  38. // console.log(res)
  39. // 状态码 2xx,参考 axios 的设计
  40. if (res.statusCode >= 200 && res.statusCode < 300) {
  41. // 2.1 提取核心数据 res.data
  42. if ((res.data as IResData<T>).code === 0) {
  43. resolve(res.data as IResData<T>)
  44. } else if ((res.data as IResData<T>).code === 401) {
  45. userStore.clearUserInfo()
  46. uni.navigateTo({ url: '/pages-sub/login/index' })
  47. reject(res)
  48. } else {
  49. !options.hideErrorToast &&
  50. uni.showToast({
  51. icon: 'none',
  52. title: (res.data as IResData<T>).msg || '请求错误',
  53. })
  54. reject(res.data)
  55. }
  56. } else if (res.statusCode === 401) {
  57. // 401错误 -> 清理用户信息,跳转到登录页
  58. userStore.clearUserInfo()
  59. reject(res)
  60. } else {
  61. // 其他错误 -> 根据后端错误信息轻提示
  62. !options.hideErrorToast &&
  63. uni.showToast({
  64. icon: 'none',
  65. title: (res.data as IResData<T>).msg || '请求错误',
  66. })
  67. reject(res)
  68. }
  69. },
  70. // 响应失败
  71. fail(err) {
  72. uni.showToast({
  73. icon: 'none',
  74. title: '网络错误,换个网络试试',
  75. })
  76. reject(err)
  77. },
  78. })
  79. })
  80. }
  81. /**
  82. * GET 请求
  83. * @param url 后台地址
  84. * @param query 请求query参数
  85. * @returns
  86. */
  87. export const httpGet = <T>(url: string, query?: Record<string, any>) => {
  88. return http<T>({
  89. url,
  90. query,
  91. method: 'GET',
  92. })
  93. }
  94. /**
  95. * POST 请求
  96. * @param url 后台地址
  97. * @param data 请求body参数
  98. * @param query 请求query参数,post请求也支持query,很多微信接口都需要
  99. * @returns
  100. */
  101. export const httpPost = <T>(
  102. url: string,
  103. data?: Record<string, any>,
  104. query?: Record<string, any>,
  105. ) => {
  106. return http<T>({
  107. url,
  108. query,
  109. data,
  110. method: 'POST',
  111. })
  112. }
  113. export const httpPut = <T>(url: string, data?: Record<string, any>) =>
  114. http<T>({
  115. url,
  116. query: {},
  117. data,
  118. method: 'PUT',
  119. })
  120. export const httpDelete = <T>(url: string, query?: Record<string, any>) =>
  121. http<T>({ url, query, method: 'DELETE' })
  122. http.get = httpGet
  123. http.post = httpPost