request.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { extend } from 'umi-request'
  2. import cleanDeep from 'clean-deep'
  3. import { browser, openLoading, closeLoading } from '@/helpers/utils'
  4. import { setLogout, setLoginError, state } from '@/state'
  5. import { postMessage } from './native-message'
  6. import { showLoadingToast, showToast, closeToast } from 'vant'
  7. export interface SearchInitParams {
  8. rows?: string | number
  9. page?: string | number
  10. }
  11. const request = extend({
  12. // requestType: 'form',
  13. timeout: 20000,
  14. timeoutMessage: '请求超时'
  15. })
  16. // request.use(async (ctx, next) => {
  17. // const { url, options } = ctx.req
  18. // const prefix = options.prefix || '';
  19. // const baseUrl: string = url.replace(prefix, '') || '';
  20. // const linkUrl: string = (ApiRouter as any)[baseUrl];
  21. // if (linkUrl) {
  22. // ctx.req.url = prefix + linkUrl;
  23. // }
  24. // await next();
  25. // })
  26. // 是否是初始化接口
  27. let initRequest = false
  28. let toast: ReturnType<typeof setTimeout>
  29. request.interceptors.request.use(
  30. (url, options: any) => {
  31. // openLoading();
  32. if (!options.hideLoading) {
  33. clearTimeout(toast)
  34. showLoadingToast({
  35. message: '加载中...',
  36. forbidClick: true,
  37. loadingType: 'spinner',
  38. duration: 0
  39. })
  40. }
  41. initRequest = options.initRequest || false
  42. const Authorization = sessionStorage.getItem('Authorization') || ''
  43. const authHeaders: any = {}
  44. if (
  45. Authorization &&
  46. ![
  47. '/api-oauth/userlogin',
  48. // `${state.platformApi}/appLoginUser/getUserInfo`,
  49. '/api-oauth/open/sendSms'
  50. ].includes(url)
  51. ) {
  52. authHeaders.Authorization = Authorization
  53. }
  54. if (state?.user?.data?.schoolInfos) {
  55. const schoolId = (state.user.data.schoolInfos || [])
  56. .map((item) => {
  57. return item.id
  58. })
  59. .join(',')
  60. if (schoolId) {
  61. // console.log('schoolId', schoolId)
  62. // options.data ? (options.data.schoolId = schoolId) : null
  63. // options.params ? (options.params.schoolId = schoolId) : null
  64. authHeaders.schoolId = schoolId
  65. }
  66. }
  67. return {
  68. url,
  69. options: {
  70. ...options,
  71. params: cleanDeep(options.params),
  72. data: cleanDeep(options.data),
  73. headers: {
  74. ...options.headers,
  75. ...authHeaders
  76. }
  77. }
  78. }
  79. },
  80. { global: false }
  81. )
  82. request.interceptors.response.use(
  83. async (res) => {
  84. toast = setTimeout(() => {
  85. closeToast()
  86. }, 100)
  87. if (res.status > 299 || res.status < 200) {
  88. clearTimeout(toast)
  89. const msg = '服务器错误,状态码' + res.status
  90. showToast(msg)
  91. throw new Error(msg)
  92. }
  93. const data = await res.clone().json()
  94. // 999 为特殊code码
  95. if (data.code !== 200 && data.errCode !== 0 && data.code !== 999) {
  96. let msg = data.msg || data.message || '处理失败,请重试'
  97. if (initRequest) {
  98. if (data.code === 403 || data.code === 5000) {
  99. setLogout()
  100. } else {
  101. setLoginError()
  102. }
  103. }
  104. console.log(data.code, '5104')
  105. if (!(data.code === 403 || data.code === 5000)) {
  106. clearTimeout(toast)
  107. showToast(msg)
  108. }
  109. const browserInfo = browser()
  110. if (data.code === 5000) {
  111. msg += '5000'
  112. if (browserInfo.isApp) {
  113. postMessage({
  114. api: 'login'
  115. })
  116. } else {
  117. setLogout()
  118. }
  119. }
  120. throw new Error(msg)
  121. }
  122. return res
  123. },
  124. { global: false }
  125. )
  126. export default request