admin
2024-09-26 dd4b7015b8466e454d6d8ec2352586f255f33412
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import axios from "axios"
import response from "core-js/internals/is-forced";
 
// 创建axios实例,设置配置得默认值
const instance = axios.create({
  baseUrl: 'http://192.168.20.250:8080/bigScreen/',   // 这里写接口的http地址,
  timeout: 20000,  // 设置请求超时的默认值
})
// 设置请求拦截器
instance.interceptors.request.use(
    config => {
      // 判断当前是否有token,有则在请求头上加上token
      if (localStorage.getItem('token')) {
        config.headers.Authorization = localStorage.getItem('token')
      }
      return config
    },
    error => {
      // 请求错误进行拦截并返回错误信息
      console.log(error)
      return Promise.reject(error)
    }
)
// 设置响应拦截
instance.interceptors.response.use(
    () => {
      const res = response.data
      return res
    },
)
// 向外暴露axios实例
export default instance