vue 三个环境域名 与 访问url域名不一致 配置,要是一样完全可以 取url地址

 

 

复制代码

 

复制代码

# just a flag
ENV = 'development'

# base api
VUE_APP_BASE_API = 'http://dev.api.watermarker-v1.moviebook.cn'

复制代码
 
复制代码
 

复制代码

复制代码
 

# just a flag
ENV = 'production'
# base api
VUE_APP_BASE_API = 'http://api.watermarker-v1.moviebook.cn'

复制代码
复制代码

复制代码

复制代码
 
 
import axios from 'axios'
// import { Message } from 'element-ui'
// import store from '@/store'
// import { getToken } from '@/utils/auth'
import router from '../router'
console.log('url', process.env.VUE_APP_BASE_API)
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // baseURL: 'http://10.8.1.16:10424',
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 18000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    // console.log(localStorage.getItem('LOCAL-TOKEN'))
    const localtoken = localStorage.getItem('LOCAL-TOKEN')
    if (localtoken) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['AUTHORIZATION'] = 'Token ' + localtoken
    }
    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 (res.status === 401 || res.status === 403) {
      // Message({
      //   message: res.msg,
      //   type: 'error',
      //   duration: 5 * 1000
      // })
      router.replace({
        path: '/login'
      })
      localStorage.removeItem('LOCAL-TOKEN')
      // to re-login
      // MessageBox.confirm('请重新登录', '', {
      //   confirmButtonText: 'Re-Login',
      //   cancelButtonText: 'Cancel',
      //   type: 'warning'
      // }).then(() => {
      //   store.dispatch('user/resetToken').then(() => {
      //     location.reload()
      //   })
      // })
    }
    return res
    // if the custom code is not 20000, it is judged as an error.
    // if (res.status !== 200) {
    //   Message({
    //     message: res.msg || 'Error',
    //     type: 'error',
    //     duration: 5 * 1000
    //   })

    //   // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
    //   if (res.status === 401 || res.status === 403) {
    //     // to re-login
    //     // MessageBox.confirm('请重新登录', 'Confirm logout', {
    //     //   confirmButtonText: 'Re-Login',
    //     //   cancelButtonText: 'Cancel',
    //     //   type: 'warning'
    //     // }).then(() => {
    //     //   store.dispatch('user/resetToken').then(() => {
    //     //     location.reload()
    //     //   })
    //     // })
    //   }
    //   // return Promise.reject(new Error(res.message || 'Error'))
    // } else {
    //   Message({
    //     message: res.msg || 'success',
    //     type: 'success',
    //     duration: 5 * 1000
    //   })
    //   return res
    // }
  },
  error => {
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    // router.replace({
    //   path: '/login'
    // })
    return Promise.reject(error)
  }
)

export default service
 

 

# just a flag
ENV = 'staging'

# base api
VUE_APP_BASE_API = 'http://test.api.watermarker-v1.moviebook.cni'

 

复制代码
复制代码
posted @ 2021-11-01 11:15  小小小小小前端  阅读(706)  评论(0编辑  收藏  举报