axios.js
import axios from 'axios';
import store from '@/store';
import router from '@/router';
import { Message } from 'view-design';
const { NODE_ENV: env } = process.env;
const axiosInstance = axios.create({
baseURL:'http://********'
});
axiosInstance.interceptors.request.use(
(config) => {
if (store.state.token) {
config.headers['Authorization'] = `wruser ${store.state.token}`
}
return config;
},
(error)=>{
return Promise.reject(error);
}
);
axiosInstance.interceptors.response.use(
(response)=>{
return response.data;
},
(error)=>{
switch(error.request.status){
case 400:
Message.error(error.request.response);
break;
case 401:
if(router.currentRoute.name!=='login'){
router.replace()
}
}
if(error.message==="Network Error"){
Message.error("网络请求错误!");
}
return Promise.resolve({code:"error",message:error.message});
}
);
export default axiosInstance;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
function.js
import axios from "@/api/base/axios";
export const url = {
login: '/api/wrmes/login',
code: '/api/wrmes/get_capta'
};
export const post = () => {
let func = {};
for (let key in url) {
func[key] = (params = {}) => {
return axios.post(
url[key],
params
)
}
}
return func;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24