123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import axios from 'axios';
- import router from '../src/router/index';
- import { Message } from 'element-ui';
- // 添加请求拦截器
- axios.interceptors.request.use(function (config) {
- const token = sessionStorage.getItem('token');
- if (token) config.headers.authorization = token;
- return config;
- }, function (error) {
- // 对请求错误做些什么
- return Promise.reject(error);
- });
- // 添加响应拦截器
- axios.interceptors.response.use(function (response) {
- if (response.status == 401) {
- // 返回登录处理
- sessionStorage.removeItem('token');
- router.replace('/frame/login');
- }
- if (response.status == 500) {
- // 错误处理
- response.data.data = { errcode: -1001, errmsg: '服务器错误' };
- }
- return response.data;
- }, function (error) {
- Message.error('服务器错误');
- // 对响应错误做点什么
- return Promise.reject(error);
- });
- // 私有方法
- const request = async ({ url, method, params, data, headers }) => {
- return await axios.request({ url, method, params, data, headers });
- };
- // 定义类
- class Point {
- get(url, params, headers) {
- return request({ url, params, headers });
- }
- post(url, data, params, headers) {
- return request({ url, method: 'post', data, params, headers });
- }
- delete(url, params, headers) {
- return request({ url, method: 'delete', params, headers });
- }
- }
- export default new Point();
|