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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
| import axios from "axios";
| import config from "@/config/config";
| import { getToken, removeAll } from "@/utils/auth";
|
| import { Notification, MessageBox } from "element-ui";
| import qs from "qs";
|
| // 创建 axios 实例
| const request = axios.create({
| // API 请求的默认前缀
| baseURL: config.BASE_API_URL,
|
| // 请求超时时间
| timeout: 20000,
| });
|
| let isRefreshing = false
| /**
| * 异常拦截处理器
| *
| * @param {*} error
| */
| const errorHandler = (error) => {
| console.log(error);
| // 判断是否是响应错误信息
| if (error.response) {
| if (error.response.status == 401) {
| removeAll();
| location.reload();
| } else if (error.response.status == 403) {
| if(!isRefreshing){
|
| /**
| * 403提示将重新从商家移动端进入当前页面
| */
| MessageBox("当前登录已失效,请从关闭重新进入。", "提示", {
| confirmButtonText: "确定",
| cancelButtonText: "取消",
| closeOnPressEscape: false,
| closeOnClickModal: false,
| type: "warning",
| })
| .then(() => {
| isRefreshing = true
| window.close();
| Notification({
| title:"登录失效提示",
| message: "请手动关闭当前页面",
| type:"error",
| position: "top-right",
| });
|
| })
| .catch(() => {
| isRefreshing = true
| Notification({
| title:"登录失效提示",
| message: "请手动关闭当前页面",
| type:"error",
| position: "top-right",
| });
| });
| isRefreshing = false
| }
| } else if(error.response.status == 400){
| Notification({
| message: error.response.data.message,
| position: "top-right",
| type:"error",
| });
| }
| }
|
| return Promise.reject(error);
| };
|
| // 请求拦截器
| request.interceptors.request.use((config) => {
| const token = getToken();
|
| if (token) {
| config.headers["accessToken"] = `${token}`;
| return config;
| }
| }, errorHandler);
|
| // 响应拦截器
| request.interceptors.response.use((response) => {
| return response.data;
| }, errorHandler);
|
| /**
| * GET 请求
| *
| * @param {String} url
| * @param {Object} data
| * @param {Object} options
| * @returns {Promise<any>}
| */
| export const get = (url, data = {}, options = {}) => {
| return request({
| url,
| params: data,
| method: "get",
| ...options,
| });
| };
|
| /**
| * POST 请求
| *
| * @param {String} url
| * @param {Object} data
| * @param {Object} options
| * @returns {Promise<any>}
| */
| export const post = (url, data = {}, options = {}) => {
| return request({
| url,
| method: "post",
| data: qs.stringify(data),
| ...options,
| headers: { "Content-Type": "application/x-www-form-urlencoded" },
| });
| };
|
| /**
| * del 请求
| *
| * @param {String} url
| * @param {Object} data
| * @param {Object} options
| * @returns {Promise<any>}
| */
| export const del = (url, data = {}, options = {}) => {
| return request({
| url,
| method: "delete",
| data: data,
| ...options,
| });
| };
|
| /**
| * 上传文件 POST 请求
| *
| * @param {String} url
| * @param {Object} data
| * @param {Object} options
| * @returns {Promise<any>}
| */
| export const upload = (url, data = {}, options = {}) => {
| return request({
| url: config.BASE_COMMON + url,
| method: "post",
| data: data,
| ...options,
| });
| };
|
|