'use strict' import Vue from 'vue' import axios from 'axios' import qs from 'qs' // post form 表單提交,序列化,否則后臺接收不到數據 let config = { withCredentials: true, // Check cross-site Access-Control baseURL: process.env.VUE_APP_URL } // 請求超時時間 axios.defaults.timeout = 10000; const _axios = axios.create(config) // post 設置請求頭 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; _axios.interceptors.request.use( function (config) { // let token // // if(process.env.NODE_ENV === 'production') { // token = localStorage.getItem("Authorization") // 生產環(huán)境自動調取token // // }else { // // token = 'bearer 01823e87-a666-4ff0-a291-d42bf26285cb' //本地開發(fā)配置token信息 // // } // if (token) { // 判斷是否存在token,如果存在的話,則每個http header都加上token // // 設置請求頭 // config.headers.common['Authorization'] = token // // config.headers.common['Authorization'] = token/ // } let data = qs.parse(config.data) // data參數 const AccessToken = localStorage.getItem('AccessToken') const project_id = localStorage.getItem('project_id') const project_type_id = localStorage.getItem('project_type_id') if (AccessToken) { // let commonData = {} data['AccessToken'] = AccessToken // 每個請求 帶上 project_id project_type_id if (project_type_id) { data["active_project_id"] = project_id } if (project_type_id) { data["active_project_type_id"] = project_type_id } } // 判斷請求的類型 // 如果是post請求就把默認參數拼到data里面 // 如果是get請求就拼到params里面 if (config.method === 'post') { config.data = qs.stringify({ // ...commonData, ...data }) } // } else if(config.method === 'get') { // config.params = { // token: token, // uid: uid, // ...config.params // } // } return config }, error => { // Do something with request error return Promise.reject(error) } ) Plugin.install = function (Vue/* , options */) { Vue.axios = _axios window.axios = _axios Object.defineProperties(Vue.prototype, { axios: { get () { return _axios } }, $axios: { get () { return _axios } } }) } Vue.use(Plugin) export default Plugin
?
本文摘自 :https://www.cnblogs.com/