1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import axios from 'axios'
- import store from '@/store'
- import notification from 'ant-design-vue/es/notification'
- import modal from 'ant-design-vue/es/modal'
- import { VueAxios } from './axios'
- const service = axios.create({
- baseURL: process.env.VUE_APP_API_BASE_URL,
- timeout: 60000
- })
- const err = (error) => {
- console.log(error.response.data.message, 'error')
- if (error.response) {
- const status = error.response.status
- if ((status == 503 || status == 500) && window.location.pathname != '/user/login') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: error.response.data.message
- })
- }
- }
-
- if (error.message.indexOf('timeout') >= 0 && window.location.pathname != '/user/login') {
- notification.destroy()
- notification.error({
- message: '提示',
- description: '请求超时'
- })
- }
- return Promise.reject(error)
- }
- service.interceptors.request.use(config => {
- const token = store.getters.token
- config.headers['App-Type'] = 1
- if (token) {
- config.headers['access-token'] = token
- }
- return config
- }, err)
- service.interceptors.response.use((response) => {
- console.log(response, 'response.data.status')
- const et = sessionStorage.getItem('errorTips')
- if (response.data.status == '1001' && !et) {
- sessionStorage.setItem('errorTips', 1)
- modal.destroyAll()
- modal.error({
- title: '提示',
- content: response.data.message,
- onOk () {
- sessionStorage.setItem('errorTips', 0)
- store.dispatch('Logout').then(() => {
- window.location.reload()
- })
- }
- })
- }
- if ((response.data.status == '401' || 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
- }
|