request.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { getToken, removeToken } from './../utils/auth';
  2. import { extend } from 'umi-request'
  3. import { ElMessage } from 'element-plus'
  4. import cleanDeep from 'clean-deep'
  5. import router from '../router';
  6. import { showLoading, hideLoading } from '../utils/loading'
  7. import runtime, * as RuntimeUtils from '../components/live-broadcast/runtime';
  8. import { state } from '../state';
  9. // import { setLogout } from 'src/actions/users'
  10. // import store from 'src/store'
  11. export interface SearchInitParams {
  12. rows?: string | number;
  13. page?: string | number;
  14. }
  15. export interface InitSearchRespones {
  16. data: {
  17. rows: any[],
  18. [key: string]: any
  19. },
  20. [key: string]: any
  21. }
  22. const request = extend({
  23. requestType: 'form',
  24. timeout: 10000
  25. })
  26. request.interceptors.request.use((url, options: any) => {
  27. let hideLoading = options.hideLoading || false
  28. if(!hideLoading) {
  29. showLoading()
  30. }
  31. const Authorization = getToken()
  32. const tenantId = (localStorage.getItem('tenantId') || '')
  33. const organId = (localStorage.getItem('organId') || '')
  34. const authHeaders: any = {}
  35. if (Authorization && !['/api-auth/usernameLogin', '/api-auth/smsLogin', '/api-auth/code/sendSms'].includes(url)) {
  36. authHeaders.Authorization = Authorization
  37. }
  38. if (tenantId) {
  39. authHeaders.tenantId = tenantId
  40. }
  41. if (organId) {
  42. authHeaders.organId = organId
  43. }
  44. return {
  45. url,
  46. options: {
  47. ...options,
  48. params: cleanDeep(options.params),
  49. headers: {
  50. ...options.headers,
  51. ...authHeaders
  52. }
  53. }
  54. }
  55. })
  56. request.interceptors.response.use(async (res, options) => {
  57. setTimeout(() => {
  58. hideLoading()
  59. }, 200)
  60. let hideMessage = options.hideMessage || false
  61. const url = new URL(res.url)
  62. if (res.status > 299 || res.status < 200) {
  63. const msg = '服务器错误,状态码' + res.status
  64. if (!hideMessage) {
  65. ElMessage.error(msg)
  66. }
  67. throw new Error(msg)
  68. }
  69. const data = await res.clone().json()
  70. if (data.code !== 200 && data.errCode !== 0) {
  71. const msg = data.msg || '处理失败,请重试'
  72. if(data.code === 401 || data.code === 403) {
  73. if(!hideMessage) {
  74. ElMessage.error(`登录过期,请重新登录!`)
  75. }
  76. try {
  77. await RuntimeUtils.leaveIMRoom('IM')
  78. RuntimeUtils.closeDevice('camera')
  79. RuntimeUtils.closeDevice('microphone')
  80. state.user = null
  81. } catch {}
  82. runtime.syncLikeTimer && clearTimeout(runtime.syncLikeTimer)
  83. removeToken()
  84. router.push(`/login`)
  85. }
  86. if(data.code === 404) {
  87. if(!hideMessage) {
  88. ElMessage.error(`请求资源不存在!`)
  89. }
  90. router.push('/404')
  91. }
  92. if (!(data.code === 403 || data.code === 401)) {
  93. if (!hideMessage) {
  94. ElMessage.error(msg)
  95. }
  96. }
  97. throw new Error(msg)
  98. }
  99. return res
  100. })
  101. export default request