@hzab/data-model 0.0.2 → 0.1.0
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.
- package/package.json +12 -3
- package/src/axios.js +50 -5
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hzab/data-model",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "data model",
|
|
5
5
|
"main": "src",
|
|
6
6
|
"scripts": {
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"@types/react": "^17.0.62",
|
|
26
26
|
"@types/react-dom": "^17.0.20",
|
|
27
27
|
"antd": "^4.14.0",
|
|
28
|
-
"axios": "^1.4.0",
|
|
29
28
|
"eslint": "^8.30.0",
|
|
30
29
|
"less": "^4.1.3",
|
|
31
30
|
"mobx": "^6.7.0",
|
|
@@ -34,7 +33,17 @@
|
|
|
34
33
|
"react-dom": "^17.0.2",
|
|
35
34
|
"react-router-dom": "^6.14.1",
|
|
36
35
|
"typedoc": "^0.24.8",
|
|
37
|
-
"typescript": "^4.9.4"
|
|
36
|
+
"typescript": "^4.9.4",
|
|
37
|
+
"axios": "^1.6.2",
|
|
38
|
+
"js-cookie": "^3.0.5"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"axios": ">=1.6.2",
|
|
42
|
+
"js-cookie": ">=3.0.5"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"axios": "^1.6.2",
|
|
46
|
+
"js-cookie": "^3.0.5"
|
|
38
47
|
},
|
|
39
48
|
"directories": {
|
|
40
49
|
"lib": "lib"
|
package/src/axios.js
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
+
import Cookies from "js-cookie";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
|
|
5
|
+
export const TOKEN = "TOKEN";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 设置 token
|
|
9
|
+
* @param {string} token
|
|
10
|
+
* @param {Object} opt
|
|
11
|
+
*/
|
|
12
|
+
export function setToken(token = "", opt) {
|
|
13
|
+
const { hasCookie } = opt || {};
|
|
14
|
+
const _t = token || "";
|
|
15
|
+
if (hasCookie !== false) {
|
|
16
|
+
const { cookieAttrs } = opt || {};
|
|
17
|
+
const cookieOpt = _.merge(
|
|
18
|
+
{
|
|
19
|
+
expires: 365,
|
|
20
|
+
},
|
|
21
|
+
cookieAttrs,
|
|
22
|
+
);
|
|
23
|
+
Cookies.set(TOKEN, _t, cookieOpt);
|
|
24
|
+
}
|
|
25
|
+
localStorage.setItem(TOKEN, _t);
|
|
26
|
+
setAxAuthorization(_t);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 获取 token
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
export function getToken() {
|
|
34
|
+
let token = Cookies.get(TOKEN) || "";
|
|
35
|
+
if (token) {
|
|
36
|
+
return token;
|
|
37
|
+
}
|
|
38
|
+
token = localStorage.getItem(TOKEN);
|
|
39
|
+
return token;
|
|
40
|
+
}
|
|
2
41
|
|
|
3
42
|
export const temp = { axiosList: [axios] };
|
|
4
43
|
|
|
@@ -10,16 +49,18 @@ export function setAxios(axiosList) {
|
|
|
10
49
|
/**
|
|
11
50
|
* axios 守卫
|
|
12
51
|
* @param {Function} cb
|
|
52
|
+
* @param {Function} errCb
|
|
13
53
|
*/
|
|
14
|
-
export function setAxRequest(cb) {
|
|
54
|
+
export function setAxRequest(cb, errCb) {
|
|
15
55
|
temp.axiosList?.forEach((_ax) => {
|
|
16
|
-
_ax.interceptors.request.use(cb);
|
|
56
|
+
_ax.interceptors.request.use(cb, errCb);
|
|
17
57
|
});
|
|
18
58
|
}
|
|
19
59
|
|
|
20
60
|
/**
|
|
21
61
|
* 请求到结果的拦截处理
|
|
22
62
|
* @param {Function} cb
|
|
63
|
+
* @param {Function} errCb
|
|
23
64
|
*/
|
|
24
65
|
export function setAxResponse(cb, errCb) {
|
|
25
66
|
temp.axiosList?.forEach((_ax) => {
|
|
@@ -65,11 +106,15 @@ export function setAxTimeout(timeout = 5000) {
|
|
|
65
106
|
}
|
|
66
107
|
|
|
67
108
|
/**
|
|
68
|
-
* 设置 token
|
|
109
|
+
* 设置 Authorization token
|
|
69
110
|
* @param {string} token
|
|
70
111
|
*/
|
|
71
|
-
export function
|
|
72
|
-
|
|
112
|
+
export function setAxAuthorization(token) {
|
|
113
|
+
setAxHeaders("Authorization", token);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function setAxBaseUrl(url) {
|
|
117
|
+
setAxDefaults("baseURL", url);
|
|
73
118
|
}
|
|
74
119
|
|
|
75
120
|
export default axios;
|