axios.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Button, message, notification, Result } from 'antd';
  2. import axios, { AxiosRequestConfig } from 'axios'
  3. // 标识登录过期的状态
  4. let expired = false;
  5. function Ajax(url?: string, params?: AxiosRequestConfig) {
  6. const ajax = axios.create({
  7. // baseURL: api, // api的base_url process.env.BASE_API,,注意局域网访问时,不能使用localhost
  8. timeout: 60 * 1000 * 5, // 请求超时时间
  9. headers: { ['Authorization']: 'Bearer ' + localStorage.getItem('Admin-Token') }
  10. });
  11. return ajax({
  12. ...params,
  13. url,
  14. method: params?.method ? params?.method : 'GET'
  15. }).then(res => {
  16. let resData = res?.data || { code: 600 }
  17. if (resData.code === 500) {
  18. let msg = localStorage.getItem('ErrMsg')
  19. if (resData.msg === '令牌验证失败') {//
  20. if (window.location.pathname !== '/login') {
  21. if (!msg) {
  22. const key = `open${Date.now()}`;
  23. const btn = (
  24. <Button type="primary" onClick={() => {
  25. localStorage.removeItem('Admin-Token')
  26. sessionStorage.removeItem('userId')
  27. notification.destroy(key)
  28. localStorage.removeItem('ErrMsg')
  29. window.location.href = window.location.origin + '/#/login'
  30. }}>
  31. 重新登录
  32. </Button>
  33. );
  34. const description = (
  35. <Result
  36. status="500"
  37. title='令牌验证失败!'
  38. subTitle="长时间没有操作,令牌失效请重新登录!"
  39. extra={btn}
  40. />
  41. )
  42. localStorage.setItem('ErrMsg', 'true')
  43. notification.open({
  44. message: '',
  45. description,
  46. duration: null,
  47. // getContainer: () => document.getElementById('notificationPop') as any || document.getElementById('root') as any,
  48. key,
  49. style: { zIndex: 999999, right: '50%', top: '50%', transform: 'translate(50%, -50%)', position: 'fixed', transition: 'all 0s' },
  50. onClose: () => {
  51. localStorage.removeItem('Admin-Token')
  52. sessionStorage.removeItem('userId')
  53. localStorage.removeItem('ErrMsg')
  54. window.location.href = window.location.origin + '/#/login'
  55. }
  56. });
  57. }
  58. return { ...resData }
  59. } else {
  60. localStorage.removeItem('Admin-Token')
  61. sessionStorage.removeItem('userId')
  62. }
  63. }
  64. if (!msg) {
  65. localStorage.setItem('ErrMsg', 'true')
  66. notification.error({
  67. message: resData.msg,
  68. onClose: () => {
  69. localStorage.removeItem('ErrMsg')
  70. }
  71. });
  72. }
  73. }
  74. if (resData.code === 310) {//权限错误
  75. localStorage.removeItem('Admin-Token')
  76. sessionStorage.removeItem('userId')
  77. if (expired) return; // 避免多次触发
  78. expired = true;
  79. message.error({
  80. content: resData.msg,
  81. duration: 2,
  82. onClose: () => {
  83. window.location.href = window.location.origin + '/#/login'
  84. }
  85. })
  86. }
  87. return res.data
  88. }).catch(err => {
  89. return Promise.reject(err)
  90. })
  91. }
  92. export default Ajax