You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB

3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
  1. //vxvdvdsvsdvvds3
  2. import router from "@/router";
  3. import axios from "axios";
  4. // import Promise from 'promise'
  5. import { yztoken } from "@/utlis/istoken";
  6. import { Message, Loading } from "element-ui";
  7. import store from "../store/index";
  8. const sysConfig = getSysConfig()
  9. function getSysConfig(){
  10. console.log('store',store.state.sysConfig)
  11. if(store.state.sysConfig && store.state.sysConfig.apiurl){
  12. window.sessionStorage.setItem('sysConfig', JSON.stringify(store.state.sysConfig))
  13. return store.state.sysConfig
  14. }else{
  15. return JSON.parse(window.sessionStorage.getItem('sysConfig'))
  16. }
  17. }
  18. const instance = axios.create({
  19. baseURL: sysConfig.apiurl,
  20. //timeout: 500000,
  21. withCredentials: true, // 允许跨域
  22. });
  23. let loading = null
  24. let needLoadingRequestCount = 0
  25. function startLoading() {
  26. loading = Loading.service({
  27. lock: true,
  28. text: "正在加载中,请稍后",
  29. spinner: "el-icon-loading",
  30. background: "rgba(0, 0, 0, 0)",
  31. customClass: "boxStyle"
  32. });
  33. }
  34. function endLoading() {
  35. loading.close()
  36. }
  37. export function showFullScreenLoading() {
  38. if (needLoadingRequestCount === 0) {
  39. startLoading()
  40. }
  41. needLoadingRequestCount++
  42. }
  43. export function tryHideFullScreenLoading() {
  44. if (needLoadingRequestCount <= 0) return
  45. needLoadingRequestCount--
  46. if (needLoadingRequestCount === 0) {
  47. endLoading()
  48. }
  49. }
  50. //请求拦截
  51. instance.interceptors.request.use(
  52. async function(config) {
  53. //console.log('old token:',window.sessionStorage.getItem("token"))
  54. //console.log('old expires_in',window.sessionStorage.getItem("expires_in"))
  55. await yztoken();
  56. showFullScreenLoading()
  57. let token = window.sessionStorage.getItem("token");
  58. let tokentype = window.sessionStorage.getItem("tokentype");
  59. config.headers["Access-Control-Allow-Origin"] = "*";
  60. // config.headers.ContentType = 'application/x-www-form-urlencoded'
  61. config.headers.Authorization = `${tokentype} ${token}`;
  62. return config;
  63. },
  64. function(err) {
  65. return Promise.reject(err);
  66. }
  67. );
  68. //响应拦截
  69. instance.interceptors.response.use(
  70. (res) => {
  71. tryHideFullScreenLoading()
  72. // console.log(res);
  73. if (res.data.code < 0) {
  74. Message.error({ showClose: true, message: res.data.message });
  75. }
  76. return res.data;
  77. },
  78. (err) => {
  79. // console.log(err);
  80. // if (response.data.code == -1) {
  81. // router.push({ path: '/login' })
  82. // // this.$router.push({path:'/login'})
  83. // // console.log('状态失效')
  84. // Message.error('状态失效请重新登录')
  85. // console.log('token失效返回登录');
  86. // }
  87. tryHideFullScreenLoading()
  88. Message.error({ showClose: true, message: err.message });
  89. return Promise.reject(err);
  90. }
  91. // function (response) {
  92. // console.log(response,10010)
  93. // const {data} = response
  94. // if (data.status == 401) {
  95. // window.location.href = '#/login'
  96. // this.$message.error('状态失效');
  97. // }
  98. // return data
  99. // },
  100. // function (err) {
  101. // if (err.response.status === 401) {
  102. // window.location.href = '#/login'
  103. // this.$router.push({ path: '/login' })
  104. // this.$message.error('登录失效请重新登录');
  105. // }
  106. // return err
  107. // }
  108. );
  109. export default instance;