123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /* eslint-disable no-const-assign */
- import axios from 'axios'
- import router from '../src/router/index'
- import { Message } from 'element-ui'
- // 构建axios实例
- axios.create({
- baseURL: process.env.BASE_API,
- timeout: 10000
- })
- axios.interceptors.request.use(config => {
- const token = sessionStorage.getItem('token')
- if (token) {
- config.headers.Authorization = 'Bearer ' + token
- }
- return config
- },
- err => {
- return Promise.reject(err)
- })
- axios.interceptors.response.use(
- response => {
- if (response.data.errcode !== 0 || response.data.errcode === 403) {
- if (response.data.errmsg.cmd) {
- Message.error(response.data.errmsg.cmd)
- return false
- }
- Message.error(response.data.errmsg)
- return false
- }
- return response
- },
- error => {
- const { status, data } = error.response
- console.log(error.response)
- if (status === 401) {
- Message.error('请重新登录')
- router.push('/login')
- return false
- }
- if (status === 400) {
- Message.error(data.message || data.errmsg)
- return false
- }
- if (status === 500) {
- Message.error('系统错误')
- return false
- }
- return data
- }
- )
- export default axios
|