http.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 if ((res.data as IResData<T>).code === 410) {
  49. userStore.clearUserInfo()
  50. uni.navigateTo({ url: '/pages-sub/login/index' })
  51. reject(res)
  52. } else {
  53. !options.hideErrorToast &&
  54. uni.showToast({
  55. icon: 'none',
  56. title: (res.data as IResData<T>).msg || '请求错误',
  57. })
  58. reject(res.data)
  59. }
  60. } else if (res.statusCode === 401) {
  61. // 401错误 -> 清理用户信息,跳转到登录页
  62. userStore.clearUserInfo()
  63. reject(res)
  64. } else {
  65. // 其他错误 -> 根据后端错误信息轻提示
  66. !options.hideErrorToast &&
  67. uni.showToast({
  68. icon: 'none',
  69. title: (res.data as IResData<T>).msg || '请求错误',
  70. })
  71. reject(res)
  72. }
  73. },
  74. // 响应失败
  75. fail(err) {
  76. uni.showToast({
  77. icon: 'none',
  78. title: '网络错误,换个网络试试',
  79. })
  80. reject(err)
  81. },
  82. })
  83. })
  84. }
  85. /**
  86. * GET 请求
  87. * @param url 后台地址
  88. * @param query 请求query参数
  89. * @returns
  90. */
  91. export const httpGet = <T>(url: string, query?: Record<string, any>) => {
  92. return http<T>({
  93. url,
  94. query,
  95. method: 'GET',
  96. })
  97. }
  98. /**
  99. * POST 请求
  100. * @param url 后台地址
  101. * @param data 请求body参数
  102. * @param query 请求query参数,post请求也支持query,很多微信接口都需要
  103. * @returns
  104. */
  105. export const httpPost = <T>(
  106. url: string,
  107. data?: Record<string, any>,
  108. query?: Record<string, any>,
  109. ) => {
  110. return http<T>({
  111. url,
  112. query,
  113. data,
  114. method: 'POST',
  115. })
  116. }
  117. export const httpPut = <T>(url: string, data?: Record<string, any>) =>
  118. http<T>({
  119. url,
  120. query: {},
  121. data,
  122. method: 'PUT',
  123. })
  124. export const httpDelete = <T>(url: string, query?: Record<string, any>) =>
  125. http<T>({ url, query, method: 'DELETE' })
  126. http.get = httpGet
  127. http.post = httpPost