123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import Vue from 'vue'
- import axios from 'axios'
- import store from '@/store'
- import notification from 'ant-design-vue/es/notification'
- import { VueAxios } from './axios'
- import { ACCESS_TOKEN } from '@/store/mutation-types'
- console.log(process.env.VUE_APP_PRO_NAME)
- // 创建 axios 实例
- const service = axios.create({
- baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
- timeout: 60000 // 请求超时时间
- })
- const err = (error) => {
- if (error.response) {
- const data = error.response.data
- if ((error.response.status == 403 || error.response.status == 401) && window.location.pathname != '/user/login') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: data.message
- })
- setTimeout(function () {
- store.dispatch('Logout').then(() => {
- window.location.reload()
- })
- }, 2000)
- }
- }
- // 超时无法访问服务
- if (error.message.indexOf('timeout') >= 0 && window.location.pathname != '/user/login') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: error.message
- })
- setTimeout(function () {
- store.dispatch('Logout').then(() => {
- window.location.reload()
- })
- }, 2000)
- }
- return Promise.reject(error)
- }
- // request interceptor
- service.interceptors.request.use(config => {
- const token = Vue.ls.get(ACCESS_TOKEN)
- if (token) {
- // config.headers['Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
- }
- return config
- }, err)
- // response interceptor
- service.interceptors.response.use((response) => {
- console.log(response, 'responseresponseresponse')
- if (response.data.status == '-1') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: response.data.message
- })
- setTimeout(function () {
- store.dispatch('Logout').then(() => {
- window.location.reload()
- })
- }, 2000)
- }
- if ((response.data.status == '900' || response.data.status == '500') && window.location.pathname != '/user/login') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: response.data.message
- })
- }
- return response.data
- }, err)
- const installer = {
- vm: {},
- install (Vue) {
- Vue.use(VueAxios, service)
- }
- }
- export {
- installer as VueAxios,
- service as axios
- }
|