axios+vue项目 全局服务统一封装接口拦截

安装插件

1
npm install axios --save

服务封装

项目src/service/index.js文件:

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
50
51
52
53
54
55
import axios from "axios"
import { Message } from "element-ui"
import $store from '@/store'
const httpCodes = require("@/config/http-code.json") // https://blog.csdn.net/qq_42618566/article/details/132541251


// axios.defaults.baseURL = process.env.NODE_ENV === "development" ? "/api" : process.env.VUE_APP_BASE_URL;
axios.defaults.headers = {
'Content-Type': 'application/json; charset=utf-8'
}

// 请求拦截
axios.interceptors.request.use(
(request) => {
// console.log(request)
// 你的逻辑
const token = localStorage.getItem('token');
request.headers['Authorization'] = "Bearer " + token;

return request;
},
(error) => {
return Promise.reject(error);
}
);

// 响应拦截
axios.interceptors.response.use(
(response) => {
// console.log(response)
// 你的逻辑
if (response.data.code === 2001 && location.pathname !== "/logon") {
$store.commit("logout", "1");
}
if (response.data.code !== 0) {
Message.error(response.data.msg);
}

return response.data;
},
(error) => {
// https://www.runoob.com/tags/html-httpmessages.html
// https://www.runoob.com/http/http-status-codes.html
const item = httpCodes.find(item => {
return error.response.status === item.code;
})
Message.error(`${item.code}${item.message}`);
return Promise.reject(error)
}
);




export default axios;

接口封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import http from './index' // 引入刚才封装的服务


// 我的待办
export function myTask (params, config) {
return http.get("/service/demo/myTask.json", params, config)
}

// 任务委托
export function taskEntrust: (params, config) {
return http.post("/service/demo/setTaskAssignee", params, config)
}

// 我的已办
export function myFinished: (params, config) {
return http.get("/service/demo/myFinished.json", params, config)
}

可以使用了


axios+vue项目 全局服务统一封装接口拦截
https://github.com/chergn/chergn.github.io/c18cccd6a9d4/
作者
全易
发布于
2024年3月28日
许可协议