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