request.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { extend } from 'umi-request'
  2. import cleanDeep from 'clean-deep'
  3. import { browser, openLoading, closeLoading } from '@/helpers/utils'
  4. import { setLogout, setLoginError } from '@/state'
  5. import { postMessage } from './native-message'
  6. import { Toast } 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. Toast.loading({
  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-auth/usernameLogin',
  48. '/api-auth/smsLogin',
  49. '/api-auth/code/sendSms'
  50. ].includes(url)
  51. ) {
  52. authHeaders.Authorization = Authorization
  53. }
  54. return {
  55. url,
  56. options: {
  57. ...options,
  58. params: cleanDeep(options.params),
  59. headers: {
  60. ...options.headers,
  61. ...authHeaders
  62. }
  63. }
  64. }
  65. },
  66. { global: false }
  67. )
  68. request.interceptors.response.use(
  69. async res => {
  70. toast = setTimeout(() => {
  71. Toast.clear()
  72. }, 100)
  73. if (res.status > 299 || res.status < 200) {
  74. clearTimeout(toast)
  75. const msg = '服务器错误,状态码' + res.status
  76. Toast(msg)
  77. throw new Error(msg)
  78. }
  79. const data = await res.clone().json()
  80. const otherCode = [200, 0, 999, 5004]
  81. if (!otherCode.includes(data.code)) {
  82. let msg = data.msg || data.message || '处理失败,请重试'
  83. if (initRequest) {
  84. if (data.code === 403 || data.code === 401) {
  85. setLogout()
  86. } else {
  87. setLoginError()
  88. }
  89. }
  90. if (!(data.code === 403 || data.code === 401)) {
  91. clearTimeout(toast)
  92. Toast(msg)
  93. }
  94. const browserInfo = browser()
  95. if (data.code === 403) {
  96. msg += '403'
  97. if (browserInfo.isApp) {
  98. postMessage({
  99. api: 'login'
  100. })
  101. } else {
  102. setLogout()
  103. }
  104. }
  105. throw new Error(msg)
  106. }
  107. return res
  108. },
  109. { global: false }
  110. )
  111. export default request