request.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. errorHandler: (error: any) => {
  16. Toast(error.message)
  17. }
  18. })
  19. // request.use(async (ctx, next) => {
  20. // const { url, options } = ctx.req
  21. // const prefix = options.prefix || '';
  22. // const baseUrl: string = url.replace(prefix, '') || '';
  23. // const linkUrl: string = (ApiRouter as any)[baseUrl];
  24. // if (linkUrl) {
  25. // ctx.req.url = prefix + linkUrl;
  26. // }
  27. // await next();
  28. // })
  29. // 是否是初始化接口
  30. let initRequest = false
  31. request.interceptors.request.use(
  32. (url, options: any) => {
  33. // openLoading();
  34. Toast.loading({
  35. message: '加载中...',
  36. forbidClick: true,
  37. loadingType: 'spinner',
  38. duration: 0
  39. })
  40. initRequest = options.initRequest || false
  41. const Authorization = sessionStorage.getItem('Authorization') || ''
  42. const authHeaders: any = {}
  43. if (
  44. Authorization &&
  45. ![
  46. '/api-auth/usernameLogin',
  47. '/api-auth/smsLogin',
  48. '/api-auth/code/sendSms'
  49. ].includes(url)
  50. ) {
  51. authHeaders.Authorization = Authorization
  52. }
  53. return {
  54. url,
  55. options: {
  56. ...options,
  57. params: cleanDeep(options.params),
  58. headers: {
  59. ...options.headers,
  60. ...authHeaders
  61. }
  62. }
  63. }
  64. },
  65. { global: false }
  66. )
  67. request.interceptors.response.use(
  68. async res => {
  69. // setTimeout(() => {
  70. // closeLoading();
  71. // }, 100);
  72. Toast.clear()
  73. if (res.status > 299 || res.status < 200) {
  74. const msg = '服务器错误,状态码' + res.status
  75. Toast(msg)
  76. throw new Error(msg)
  77. }
  78. const data = await res.clone().json()
  79. if (data.code !== 200 && data.errCode !== 0) {
  80. const msg = data.msg || '处理失败,请重试'
  81. // if (initRequest) {
  82. if (data.code === 403 || data.code === 401) {
  83. setLogout()
  84. } else {
  85. setLoginError()
  86. }
  87. // }
  88. if (!(data.code === 403 || data.code === 401)) {
  89. Toast(msg)
  90. }
  91. const browserInfo = browser()
  92. if (data.code === 403) {
  93. if (browserInfo.isApp) {
  94. postMessage({
  95. api: 'login'
  96. })
  97. }
  98. }
  99. throw new Error(msg)
  100. }
  101. return res
  102. },
  103. { global: false }
  104. )
  105. export default request