123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import axios from 'axios'
- import util from './util'
- import constant from './constant'
- import router from '../../router'
- axios.defaults.withCredentials = true
- // 创建一个 axios 实例
- const service = axios.create({
- baseURL: constant.BASE_URI,
- withCredentials: true,
- timeout: 20000 // 请求超时时间
- })
- // 请求拦截器
- service.interceptors.request.use(
- config => {
- // 在请求发送之前做一些处理
- if (!/^https:\/\/|http:\/\//.test(config.url)) {
- const token = util.getToken()
- console.log(token)
- if (token && token !== 'undefined') {
- // 让每个请求携带token-- ['Authorization']为自定义key 请根据实际情况自行修改
- config.headers.Authorization = 'Bearer' + token
- }
- }
- return config
- },
- error => {
- // 发送失败
- console.log(error)
- Promise.reject(error)
- }
- )
- // 响应拦截器
- service.interceptors.response.use(
- response => {
- const res = response.data
- if (res.statusCode === -401) {
- util.removeToken()
- router.push({
- name: 'mobileLogin'
- })
- }
- return response
- },
- error => {
- return Promise.reject(error)
- }
- )
- export default service
|