123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import axios from 'axios'
- import { MessageBox, Message } from 'element-ui'
- import store from '@/store'
- import { getToken } from '@/utils/auth'
- //获取当前url
- var aa = process.env.VUE_APP_BASE_API
- //获取当前url
- if(aa.indexOf('/')==0 && aa.length==1){
- aa= window.location.protocol + "//"+window.location.host+aa
- }
- const service = axios.create({
- baseURL: aa, // url = base url + request url
- withCredentials: true, // send cookies when cross-domain requests
- timeout: 60000 // request timeout
- })
- // request interceptor
- service.interceptors.request.use(
- config => {
- // do something before request is sent
- // config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' //此处是增加的代码,设置请求头的类型
- if (process.env.VUE_APP_BASE_API !== '/dev-api') {
- config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
- config.withCredentials = false
- }
- if (store.getters.token) {
- // let each request carry token
- // ['Authorization'] is a custom headers key
- // please modify it according to the actual situation
- if (process.env.VUE_APP_BASE_API === '/dev-api') {
- config.headers['X-Token'] = getToken()
- } else {
- config.headers['token'] = getToken()
- }
- }
- return config
- },
- error => {
- // do something with request error
- console.log(error) // for debug
- return Promise.reject(error)
- }
- )
- // response interceptor
- service.interceptors.response.use(
- /**
- * If you want to get http information such as headers or status
- * Please return response => response
- */
- /**
- * Determine the request status by custom code
- * Here is just an example
- * You can also judge the status by HTTP Status Code
- */
- response => {
- const res = response.data
- // if the custom code is not 20000, it is judged as an error.
- if (res.code !== 200) {
- Message({
- // message: res.msg + res.code,
- message: '请求超时',
- type: 'error',
- duration: 5 * 1000
- })
- // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
- if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
- // to re-login
- MessageBox.confirm('你已经注销登陆,你可以取消或重新登陆', '确认注销', {
- confirmButtonText: '重新登陆',
- cancelButtonText: '取消',
- type: 'warning'
- }).then(() => {
- store.dispatch('user/resetToken').then(() => {
- location.reload()
- })
- })
- }
- if (res.code === 20002) {
- store.dispatch('user/resetToken').then(() => {
- location.reload()
- })
- }
- if (res.code === undefined) {
- return res
- } else {
- return Promise.reject(new Error(res.message || 'Error'))
- }
- } else {
- return res
- }
- },
- error => {
- console.log('err' + error) // for debug
- Message({
- message: error.message,
- type: 'error',
- duration: 5 * 1000
- })
- return Promise.reject(error)
- }
- )
- export default service
|