http.js
6.9 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import Vue from "vue";
import router from "@/router";
import Router from '@/router/index'
import axios from "axios";
import qs from "qs";
import { Notification, Loading, Message } from "element-ui";
// var instance = axios.create({
// baseURL:'',
// timeout:5000,
// headers:{"Content-Type":"multipart/form-data"}
// });
//
// Vue.prototype.instance=instance;
//样式文件,需单独引入
import "element-ui/lib/theme-chalk/index.css";
Vue.prototype.$http = axios;
Vue.prototype.$notify = Notification;
Vue.prototype.$loading = Loading;
Vue.prototype.$message = Message;
Vue.prototype.$qs = qs;
let LoadingInstance,
token = "";
// 环境设置
// 环境的切换
if (process.env.NODE_ENV == "development") {
axios.defaults.baseURL = "http://cp.zgcareer.com";
} else if (process.env.NODE_ENV == "debug") {
axios.defaults.baseURL = "http://cp.zgcareer.com";
} else if (process.env.NODE_ENV == "production") {
axios.defaults.baseURL = "http://cp.zgcareer.com";
}
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.common['token'] = localStorage.getItem("token");
// 设置请求超时
axios.defaults.timeout = 10000;
// 请求拦截器
axios.interceptors.request.use(
config => {
const token = localStorage.getItem("token");
config.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
if (localStorage.getItem("token")) {
config.params = { 'token': localStorage.getItem("token") }
}
// token && (config.headers.Authorization = token);
return config;
},
error => {
return Promise.reject(error);
}
);
// 响应拦截器
axios.interceptors.response.use(
response => {
LoadingInstance.close();
// 如果返回的状态码为1,说明接口请求成功,可以正常拿到数据
// 否则的话抛出错误
if (response.data.code == 1) {
return Promise.resolve(response);
} else {
console.log(response.data.code)
switch (response.data.code) {
case 0:
Notification.info({
title: "提示",
message: response.data.msg,
duration: 1500
});
break;
case 401: //未登录
console.log(401)
Notification.error({
title: "错误",
message: response.data.msg,
duration: 1500
});
console.log(401, 1)
let test1 = localStorage.getItem("schoolSymbol");
localStorage.clear();
Router.push({
path: "/login/" + test1
});
break;
case 500:
console.log(500, 2)
Notification.error({
title: "错误",
message: response.data.msg,
duration: 1500
});
console.log(500)
let test0 = localStorage.getItem("schoolSymbol");
localStorage.clear();
Router.push({
path: "/login/" + test0
});
break;
default:
Notification.error({
title: "错误",
message: response.data.msg,
duration: 1500
});
let test4 = localStorage.getItem("schoolSymbol");
localStorage.clear();
Router.push({
path: "/login/" + test4
});
}
return Promise.reject(response);
}
},
error => {
LoadingInstance.close();
// console.log(error.response.status)
let test2 = localStorage.getItem("schoolSymbol");
Router.push({
path: "/login/" + test2
});
if (error.response.status) {
Notification.error({
title: "错误",
message: "网络错误",
duration: 1500
});
return Promise.reject(error.response);
}
}
);
/**
* get方法,对应get请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
export function get(url, params, loading) {
//console.log(loading);
// headers? axios.defaults.headers.get['XX-Token'] = headers['XX-Token']:"";
if (
loading == "" ||
loading == undefined ||
typeof loading == undefined ||
loading
) {
LoadingInstance = Loading.service({
//加载loading
fullscreen: true,
lock: true,
// spinner: 'el-icon-loading',
text: "加载中...",
background: "rgba(0, 0, 0, 0)"
});
}
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params
})
.then(res => {
resolve(res.data.data);
})
.catch(err => {
reject(err.data);
});
});
}
/**
* post方法,对应post请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
**/
export function post(url, params, loading) {
if (loading != false) {
LoadingInstance = Loading.service({
fullscreen: true,
lock: true,
text: "加载中...",
background: "rgba(0, 0, 0, 0)"
});
}
return new Promise((resolve, reject) => {
let that = this;
axios
.post(url, qs.stringify(Object.assign({}, params)))
.then(res => {
resolve(res.data.data);
})
.catch(err => {
reject(err.data);
});
});
}
export function uploadFile(Url, data) {
//上传图片的方法
return new Promise((resolve, reject) => {
let instance = axios.create({
// http://cp.zgcareer.com
baseURL: "http://cp.zgcareer.com",
headers: {
"Content-Type": "multipart/form-data"
}
});
instance
.post(Url, data)
.then(res => {
resolve(res.data.data);
})
.catch(error => {
reject(error.data);
});
});
}
export function toast(message, type) {
//上传图片的方法
if (type == "error") {
Notification.error({
title: "错误",
message: message,
duration: 1500
});
} else {
Notification.success({
title: "提示",
message: message,
duration: 1500
});
}
}