@freelog/tools-lib 0.1.161 → 0.1.162

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,141 +1,141 @@
1
- import axios, {AxiosRequestConfig} from 'axios';
2
- // import NProgress from 'nprogress';
3
- // import "nprogress/nprogress.css";
4
- import {completeUrlByDomain} from "./format";
5
- import * as LinkTo from './linkTo';
6
- // import {CommonReturn, RequestParamsType} from "../service-API/tools";
7
- import FServiceAPI from "../service-API";
8
- // import from '../service-API';
9
-
10
- const codeMessage: any = {
11
- 200: '服务器成功返回请求的数据。',
12
- 201: '新建或修改数据成功。',
13
- 202: '一个请求已经进入后台排队(异步任务)。',
14
- 204: '删除数据成功。',
15
- 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
16
- 401: '用户没有权限(令牌、用户名、密码错误)。',
17
- 403: '用户得到授权,但是访问是被禁止的。',
18
- 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
19
- 406: '请求的格式不可得。',
20
- 410: '请求的资源被永久删除,且不会再得到的。',
21
- 422: '当创建一个对象时,发生一个验证错误。',
22
- 500: '服务器发生错误,请检查服务器。',
23
- 502: '网关错误。',
24
- 503: '服务不可用,服务器暂时过载或维护。',
25
- 504: '网关超时。',
26
- };
27
-
28
- // export let apiHost: string = '';
29
- if (window.location.hostname.includes('.com')) {
30
- // apiHost = `${window.location.protocol}//qi.${(window.location.host.match(/(?<=\.).*/) || [''])[0]}`;
31
- // apiHost = window.location.origin.replace('console', 'qi');
32
- axios.defaults.withCredentials = true;
33
- axios.defaults.baseURL = completeUrlByDomain('api');
34
- }
35
-
36
- /**
37
- * 异常处理程序
38
- */
39
- // const errorHandler = (error: { response: Response }): Response => {
40
- // const {response} = error;
41
- // if (response && response.status) {
42
- // const errorText = codeMessage[response.status] || response.statusText;
43
- // const {status, url} = response;
44
- //
45
- // // notification.error({
46
- // // message: `请求错误 ${status}: ${url}`,
47
- // // description: errorText,
48
- // // });
49
- // } else if (!response) {
50
- // // notification.error({
51
- // // description: '您的网络发生异常,无法连接服务器',
52
- // // message: '网络异常',
53
- // // });
54
- // }
55
- // return response;
56
- // };
57
- //
58
- // // Add a request interceptor
59
- axios.interceptors.request.use(
60
- (config) => {
61
- // Do something before request is sent
62
- // NProgress.start();
63
- return config;
64
- },
65
- (error) => {
66
- // Do something with request error
67
- // NProgress.done();
68
- return Promise.reject(error);
69
- }
70
- );
71
-
72
- /**
73
- * 配置request请求时的默认参数
74
- */
75
- axios.interceptors.response.use(
76
- (response) => {
77
- // Do something with response data
78
- // console.log(response, 'response!!!!!!');
79
- // NProgress.done();
80
- if (response.status !== 200) {
81
-
82
- const error = {
83
- description: codeMessage[response.status],
84
- message: response.status,
85
- };
86
-
87
- // notification.error(error);
88
- throw new Error(JSON.stringify(error));
89
- }
90
- const {data, headers} = response;
91
- if (headers['content-disposition']?.includes('attachment;')) {
92
- // downloadFile(response);
93
- return;
94
- }
95
-
96
- // console.log(data, 'data2390jasdflkf');
97
-
98
-
99
- // if ((data.errcode === undefined
100
- // ? (data.errCode !== 0 && data.errCode !== 2)
101
- // : (data.errcode !== 0 && data.errcode !== 2))
102
- // || data.ret !== 0) {
103
- //
104
- // // notification.error({
105
- // // message: data.msg,
106
- // // });
107
- // throw new Error(JSON.stringify(data));
108
- // }
109
- return data;
110
- },
111
- (error) => {
112
- // Do something with response error
113
- // NProgress.done();
114
- return Promise.reject(error);
115
- });
116
-
117
- export default axios;
118
-
119
-
120
- export async function request(config: AxiosRequestConfig, {
121
- // export async function request(config: any, {
122
- noRedirect = false,
123
- noErrorAlert = false,
124
- // }: RequestParamsType = {}): Promise<CommonReturn & { data: any } | void> {
125
- }: any = {}): Promise<any> {
126
- const result: any = await axios.request(config);
127
- // console.log(result, 'response');
128
- // const {data} = response;
129
- // console.log(result, 'result90iowjksdfjlsdkj')
130
- if (result.errCode=== 30 && !noRedirect) {
131
- await FServiceAPI.User.logout();
132
- window.location.replace(`${completeUrlByDomain('user')}${LinkTo.login({goTo: window.location.href})}`);
133
- } else if (result.ret === 4 && result.errCode === 10 && !noRedirect) {
134
- window.location.replace(`${completeUrlByDomain('user')}${LinkTo.userFreeze({goTo: window.location.href})}`);
135
- } else if ((result.errcode !== 0 || result.errCode !== 0) && !noErrorAlert) {
136
- // window.alert(result.msg);
137
- // return;
138
-
139
- }
140
- return result;
141
- }
1
+ import axios, {AxiosRequestConfig} from 'axios';
2
+ // import NProgress from 'nprogress';
3
+ // import "nprogress/nprogress.css";
4
+ import {completeUrlByDomain} from "./format";
5
+ import * as LinkTo from './linkTo';
6
+ // import {CommonReturn, RequestParamsType} from "../service-API/tools";
7
+ import FServiceAPI from "../service-API";
8
+ // import from '../service-API';
9
+
10
+ const codeMessage: any = {
11
+ 200: '服务器成功返回请求的数据。',
12
+ 201: '新建或修改数据成功。',
13
+ 202: '一个请求已经进入后台排队(异步任务)。',
14
+ 204: '删除数据成功。',
15
+ 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
16
+ 401: '用户没有权限(令牌、用户名、密码错误)。',
17
+ 403: '用户得到授权,但是访问是被禁止的。',
18
+ 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
19
+ 406: '请求的格式不可得。',
20
+ 410: '请求的资源被永久删除,且不会再得到的。',
21
+ 422: '当创建一个对象时,发生一个验证错误。',
22
+ 500: '服务器发生错误,请检查服务器。',
23
+ 502: '网关错误。',
24
+ 503: '服务不可用,服务器暂时过载或维护。',
25
+ 504: '网关超时。',
26
+ };
27
+
28
+ // export let apiHost: string = '';
29
+ if (window.location.hostname.includes('.com')) {
30
+ // apiHost = `${window.location.protocol}//qi.${(window.location.host.match(/(?<=\.).*/) || [''])[0]}`;
31
+ // apiHost = window.location.origin.replace('console', 'qi');
32
+ axios.defaults.withCredentials = true;
33
+ axios.defaults.baseURL = completeUrlByDomain('api');
34
+ }
35
+
36
+ /**
37
+ * 异常处理程序
38
+ */
39
+ // const errorHandler = (error: { response: Response }): Response => {
40
+ // const {response} = error;
41
+ // if (response && response.status) {
42
+ // const errorText = codeMessage[response.status] || response.statusText;
43
+ // const {status, url} = response;
44
+ //
45
+ // // notification.error({
46
+ // // message: `请求错误 ${status}: ${url}`,
47
+ // // description: errorText,
48
+ // // });
49
+ // } else if (!response) {
50
+ // // notification.error({
51
+ // // description: '您的网络发生异常,无法连接服务器',
52
+ // // message: '网络异常',
53
+ // // });
54
+ // }
55
+ // return response;
56
+ // };
57
+ //
58
+ // // Add a request interceptor
59
+ axios.interceptors.request.use(
60
+ (config) => {
61
+ // Do something before request is sent
62
+ // NProgress.start();
63
+ return config;
64
+ },
65
+ (error) => {
66
+ // Do something with request error
67
+ // NProgress.done();
68
+ return Promise.reject(error);
69
+ }
70
+ );
71
+
72
+ /**
73
+ * 配置request请求时的默认参数
74
+ */
75
+ axios.interceptors.response.use(
76
+ (response) => {
77
+ // Do something with response data
78
+ // console.log(response, 'response!!!!!!');
79
+ // NProgress.done();
80
+ if (response.status !== 200) {
81
+
82
+ const error = {
83
+ description: codeMessage[response.status],
84
+ message: response.status,
85
+ };
86
+
87
+ // notification.error(error);
88
+ throw new Error(JSON.stringify(error));
89
+ }
90
+ const {data, headers} = response;
91
+ if (headers['content-disposition']?.includes('attachment;')) {
92
+ // downloadFile(response);
93
+ return;
94
+ }
95
+
96
+ // console.log(data, 'data2390jasdflkf');
97
+
98
+
99
+ // if ((data.errcode === undefined
100
+ // ? (data.errCode !== 0 && data.errCode !== 2)
101
+ // : (data.errcode !== 0 && data.errcode !== 2))
102
+ // || data.ret !== 0) {
103
+ //
104
+ // // notification.error({
105
+ // // message: data.msg,
106
+ // // });
107
+ // throw new Error(JSON.stringify(data));
108
+ // }
109
+ return data;
110
+ },
111
+ (error) => {
112
+ // Do something with response error
113
+ // NProgress.done();
114
+ return Promise.reject(error);
115
+ });
116
+
117
+ export default axios;
118
+
119
+
120
+ export async function request(config: AxiosRequestConfig, {
121
+ // export async function request(config: any, {
122
+ noRedirect = false,
123
+ noErrorAlert = false,
124
+ // }: RequestParamsType = {}): Promise<CommonReturn & { data: any } | void> {
125
+ }: any = {}): Promise<any> {
126
+ const result: any = await axios.request(config);
127
+ // console.log(result, 'response');
128
+ // const {data} = response;
129
+ // console.log(result, 'result90iowjksdfjlsdkj')
130
+ if (result.errCode=== 30 && !noRedirect) {
131
+ await FServiceAPI.User.logout();
132
+ window.location.replace(`${completeUrlByDomain('user')}${LinkTo.login({goTo: window.location.href})}`);
133
+ } else if (result.ret === 4 && result.errCode === 10 && !noRedirect) {
134
+ window.location.replace(`${completeUrlByDomain('user')}${LinkTo.userFreeze({goTo: window.location.href})}`);
135
+ } else if ((result.errcode !== 0 || result.errCode !== 0) && !noErrorAlert) {
136
+ // window.alert(result.msg);
137
+ // return;
138
+
139
+ }
140
+ return result;
141
+ }