tianheng-ui 0.1.82 → 0.1.84

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,87 +1,87 @@
1
- import axios from "axios";
2
- import Log from "./Log";
3
- import { Notification } from "element-ui";
4
-
5
- export const init = baseConfig => {
6
- // 创建axios实例
7
- const Axios = axios.create({
8
- baseURL: baseConfig.baseUrl,
9
- timeout: 60000 // 请求超时时间
10
- // withCredentials: true, //允许携带cookie
11
- });
12
-
13
- // 添加请求拦截器
14
- Axios.interceptors.request.use(
15
- config => {
16
- const info = sessionStorage.getItem("_TH_OauthConfig");
17
- if (info) {
18
- const oauthInfo = JSON.parse(info)
19
- config.baseURL = oauthInfo.baseUrl
20
- config.headers["Authorization"] = oauthInfo.token;
21
- }
22
- if (baseConfig.headers) {
23
- config.headers = Object.assign(config.headers, baseConfig.headers);
24
- }
25
- return config;
26
- },
27
- error => {
28
- Promise.reject(error);
29
- }
30
- );
31
-
32
- // 添加响应拦截器
33
- Axios.interceptors.response.use(
34
- response => {
35
- const code = response.status;
36
- if (code < 200 || code > 300) {
37
- Notification.error({
38
- title: response.message
39
- });
40
- return Promise.reject("error");
41
- }
42
-
43
- const dataCode = response.data.code;
44
- if (dataCode && dataCode !== 200) {
45
- Notification.error({
46
- title: response.data.message
47
- });
48
- return Promise.reject("error");
49
- }
50
-
51
- if (baseConfig.debug) {
52
- Log.prettyPrimary("Request Url:", response.request.responseURL);
53
- Log.prettySuccess("Request Res:", response);
54
- }
55
-
56
- return response.data;
57
- },
58
- error => {
59
- let code = 0;
60
- try {
61
- code = error.response.data.status;
62
- } catch (e) {
63
- if (error.toString().indexOf("Error: timeout") !== -1) {
64
- Notification.error({
65
- title: "网络请求超时",
66
- duration: 5000
67
- });
68
- return Promise.reject(error);
69
- }
70
- }
71
- if (code) {
72
- const errorMsg = error.response.data.message;
73
- Notification.error({
74
- title: errorMsg || "未知错误",
75
- duration: 5000
76
- });
77
- } else {
78
- Notification.error({
79
- title: "接口请求失败",
80
- duration: 5000
81
- });
82
- }
83
- return Promise.reject(error);
84
- }
85
- );
86
- return Axios;
87
- };
1
+ import axios from "axios";
2
+ import Log from "./Log";
3
+ import { Notification } from "element-ui";
4
+
5
+ export const init = baseConfig => {
6
+ // 创建axios实例
7
+ const Axios = axios.create({
8
+ baseURL: baseConfig.baseUrl,
9
+ timeout: 60000 // 请求超时时间
10
+ // withCredentials: true, //允许携带cookie
11
+ });
12
+
13
+ // 添加请求拦截器
14
+ Axios.interceptors.request.use(
15
+ config => {
16
+ const info = sessionStorage.getItem("_TH_OauthConfig");
17
+ if (info) {
18
+ const oauthInfo = JSON.parse(info)
19
+ config.baseURL = oauthInfo.baseUrl
20
+ config.headers["Authorization"] = oauthInfo.token;
21
+ }
22
+ if (baseConfig.headers) {
23
+ config.headers = Object.assign(config.headers, baseConfig.headers);
24
+ }
25
+ return config;
26
+ },
27
+ error => {
28
+ Promise.reject(error);
29
+ }
30
+ );
31
+
32
+ // 添加响应拦截器
33
+ Axios.interceptors.response.use(
34
+ response => {
35
+ const code = response.status;
36
+ if (code < 200 || code > 300) {
37
+ Notification.error({
38
+ title: response.message
39
+ });
40
+ return Promise.reject("error");
41
+ }
42
+
43
+ const dataCode = response.data.code;
44
+ if (dataCode && dataCode !== 200) {
45
+ Notification.error({
46
+ title: response.data.message
47
+ });
48
+ return Promise.reject("error");
49
+ }
50
+
51
+ if (baseConfig.debug) {
52
+ Log.prettyPrimary("Request Url:", response.request.responseURL);
53
+ Log.prettySuccess("Request Res:", response);
54
+ }
55
+
56
+ return response.data;
57
+ },
58
+ error => {
59
+ let code = 0;
60
+ try {
61
+ code = error.response.data.status;
62
+ } catch (e) {
63
+ if (error.toString().indexOf("Error: timeout") !== -1) {
64
+ Notification.error({
65
+ title: "网络请求超时",
66
+ duration: 5000
67
+ });
68
+ return Promise.reject(error);
69
+ }
70
+ }
71
+ if (code) {
72
+ const errorMsg = error.response.data.message;
73
+ Notification.error({
74
+ title: errorMsg || "未知错误",
75
+ duration: 5000
76
+ });
77
+ } else {
78
+ Notification.error({
79
+ title: "接口请求失败",
80
+ duration: 5000
81
+ });
82
+ }
83
+ return Promise.reject(error);
84
+ }
85
+ );
86
+ return Axios;
87
+ };