beer-network 1.1.0-alpha.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/dist/api.d.ts +57 -0
- package/dist/api.js +117 -0
- package/dist/elementUtils.d.ts +12 -0
- package/dist/elementUtils.js +35 -0
- package/dist/session.d.ts +18 -0
- package/dist/session.js +40 -0
- package/package.json +27 -0
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Json 响应通用实体.
|
|
3
|
+
*/
|
|
4
|
+
export type JsonResponse<T> = {
|
|
5
|
+
success: boolean;
|
|
6
|
+
data: T;
|
|
7
|
+
code: string;
|
|
8
|
+
message?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Antd Table 数据类型.
|
|
12
|
+
*/
|
|
13
|
+
export type RequestData<T> = {
|
|
14
|
+
data: T[];
|
|
15
|
+
page: number;
|
|
16
|
+
total: number;
|
|
17
|
+
success: boolean;
|
|
18
|
+
};
|
|
19
|
+
export default class Fetch {
|
|
20
|
+
/**
|
|
21
|
+
* 请求地址.
|
|
22
|
+
*/
|
|
23
|
+
private readonly pathPrefix;
|
|
24
|
+
private constructor();
|
|
25
|
+
/**
|
|
26
|
+
* 发送POST/JSON 请求.
|
|
27
|
+
* @param path 路径.
|
|
28
|
+
* @param params 参数.
|
|
29
|
+
* @param body 内容体.
|
|
30
|
+
*/
|
|
31
|
+
post<T>(path: string, params?: any, body?: any): Promise<JsonResponse<T>>;
|
|
32
|
+
/**
|
|
33
|
+
* 发送Get 请求.
|
|
34
|
+
* @param path 路径.
|
|
35
|
+
* @param params 参数.
|
|
36
|
+
*/
|
|
37
|
+
get<T>(path: string, params?: any): Promise<JsonResponse<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* 缓存.
|
|
40
|
+
* @param key 缓存Key.
|
|
41
|
+
* @param callback 数据回执函数.
|
|
42
|
+
* @param minute 缓存时间 (分钟).
|
|
43
|
+
*/
|
|
44
|
+
cache<T>(key: string, callback: () => Promise<T>, minute: number): Promise<T>;
|
|
45
|
+
/**
|
|
46
|
+
* 默认授权方式.
|
|
47
|
+
*/
|
|
48
|
+
authorization(): {};
|
|
49
|
+
/**
|
|
50
|
+
* 默认响应数据解析方式.
|
|
51
|
+
* @param response 响应数据.
|
|
52
|
+
*/
|
|
53
|
+
responseJson<T>(response: Response): Promise<JsonResponse<T>>;
|
|
54
|
+
pageParams(params: any): any;
|
|
55
|
+
pageTable<T>(response: JsonResponse<any>): Promise<Partial<RequestData<T>>>;
|
|
56
|
+
sleep(value: number): Promise<unknown>;
|
|
57
|
+
}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const session_1 = require("./session");
|
|
4
|
+
class Fetch {
|
|
5
|
+
constructor(pathPrefix) {
|
|
6
|
+
this.pathPrefix = pathPrefix;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 发送POST/JSON 请求.
|
|
10
|
+
* @param path 路径.
|
|
11
|
+
* @param params 参数.
|
|
12
|
+
* @param body 内容体.
|
|
13
|
+
*/
|
|
14
|
+
async post(path, params, body) {
|
|
15
|
+
const paramQuery = new URLSearchParams();
|
|
16
|
+
Object.keys(params || {})
|
|
17
|
+
.forEach(key => {
|
|
18
|
+
paramQuery.append(key, params[key]);
|
|
19
|
+
});
|
|
20
|
+
const response = await fetch(this.pathPrefix + path + (paramQuery.toString() === '' ? '' : '?') + paramQuery.toString(), {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: {
|
|
23
|
+
...this.authorization(),
|
|
24
|
+
'Content-Type': 'application/json'
|
|
25
|
+
},
|
|
26
|
+
body: JSON.stringify(body || {})
|
|
27
|
+
});
|
|
28
|
+
return this.responseJson(response);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 发送Get 请求.
|
|
32
|
+
* @param path 路径.
|
|
33
|
+
* @param params 参数.
|
|
34
|
+
*/
|
|
35
|
+
async get(path, params) {
|
|
36
|
+
const paramQuery = new URLSearchParams();
|
|
37
|
+
Object.keys(params || {})
|
|
38
|
+
.forEach(key => {
|
|
39
|
+
paramQuery.append(key, params[key]);
|
|
40
|
+
});
|
|
41
|
+
const response = await fetch(this.pathPrefix + path + '?' + paramQuery.toString(), {
|
|
42
|
+
method: 'GET',
|
|
43
|
+
headers: {
|
|
44
|
+
...this.authorization()
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return this.responseJson(response);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 缓存.
|
|
51
|
+
* @param key 缓存Key.
|
|
52
|
+
* @param callback 数据回执函数.
|
|
53
|
+
* @param minute 缓存时间 (分钟).
|
|
54
|
+
*/
|
|
55
|
+
async cache(key, callback, minute) {
|
|
56
|
+
const cacheKey = 'CACHE_' + key;
|
|
57
|
+
const cacheValue = localStorage.getItem(cacheKey);
|
|
58
|
+
if (cacheValue !== null) {
|
|
59
|
+
const result = JSON.parse(cacheValue);
|
|
60
|
+
const { expireTime } = result;
|
|
61
|
+
if (expireTime !== undefined && expireTime >= new Date().getTime()) {
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const response = await callback();
|
|
66
|
+
if (response.code === '0') {
|
|
67
|
+
response.expireTime = new Date().getTime() + 60000 * minute;
|
|
68
|
+
localStorage.setItem(cacheKey, JSON.stringify(response));
|
|
69
|
+
}
|
|
70
|
+
return response;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 默认授权方式.
|
|
74
|
+
*/
|
|
75
|
+
authorization() {
|
|
76
|
+
const bearer = session_1.Session.getBearer();
|
|
77
|
+
if (bearer === undefined || bearer === '') {
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
return { Authorization: 'Bearer ' + bearer };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 默认响应数据解析方式.
|
|
84
|
+
* @param response 响应数据.
|
|
85
|
+
*/
|
|
86
|
+
async responseJson(response) {
|
|
87
|
+
const result = await response.json();
|
|
88
|
+
if (result?.code === '401') {
|
|
89
|
+
setTimeout(() => window.location.replace('/auth/login'), 500);
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
pageParams(params) {
|
|
95
|
+
return {
|
|
96
|
+
...params,
|
|
97
|
+
currentPage: params.current,
|
|
98
|
+
current: undefined
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async pageTable(response) {
|
|
102
|
+
return {
|
|
103
|
+
data: response.data.records || response.data,
|
|
104
|
+
page: response.data.currentPage || 1,
|
|
105
|
+
total: response.data.totalSize || response.data.length,
|
|
106
|
+
success: response.success
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async sleep(value) {
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
setTimeout(() => {
|
|
112
|
+
resolve(undefined);
|
|
113
|
+
}, value);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.default = Fetch;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ElementUtils = void 0;
|
|
4
|
+
class ElementUtils {
|
|
5
|
+
/**
|
|
6
|
+
* 选择文件本地文件.
|
|
7
|
+
*/
|
|
8
|
+
static selectFile() {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
const fileInput = document.createElement('input');
|
|
11
|
+
fileInput.type = 'file';
|
|
12
|
+
fileInput.style.display = 'none';
|
|
13
|
+
document.body.appendChild(fileInput);
|
|
14
|
+
fileInput.addEventListener('change', () => {
|
|
15
|
+
resolve(fileInput.files);
|
|
16
|
+
document.body.removeChild(fileInput);
|
|
17
|
+
});
|
|
18
|
+
fileInput.click();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 下载文件.
|
|
23
|
+
* @param url 文件下载地址.
|
|
24
|
+
* @param fileName 文件名称.
|
|
25
|
+
*/
|
|
26
|
+
static download(url, fileName) {
|
|
27
|
+
const link = document.createElement('a');
|
|
28
|
+
link.href = url;
|
|
29
|
+
link.download = fileName;
|
|
30
|
+
document.body.appendChild(link);
|
|
31
|
+
link.click();
|
|
32
|
+
document.body.removeChild(link);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.ElementUtils = ElementUtils;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class Session {
|
|
2
|
+
/**
|
|
3
|
+
* 获取令牌.
|
|
4
|
+
* <li>window.sessionKey 设置读取缓存用户信息的Key, 默认 login_user.</li>
|
|
5
|
+
* <li>window.accessTokenKey 读取用户令牌的字段, 默认 accessToken 或者 token.</li>
|
|
6
|
+
* <li>window.accessToken 设置身份令牌, 直接返回.</li>
|
|
7
|
+
*/
|
|
8
|
+
static getBearer(): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* 检查令牌是否存在.
|
|
11
|
+
*/
|
|
12
|
+
static checkAccessTokenExist(): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 退出登录.
|
|
15
|
+
* @param url 退出之后的地址.
|
|
16
|
+
*/
|
|
17
|
+
static logout(url?: string): void;
|
|
18
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Session = void 0;
|
|
4
|
+
class Session {
|
|
5
|
+
/**
|
|
6
|
+
* 获取令牌.
|
|
7
|
+
* <li>window.sessionKey 设置读取缓存用户信息的Key, 默认 login_user.</li>
|
|
8
|
+
* <li>window.accessTokenKey 读取用户令牌的字段, 默认 accessToken 或者 token.</li>
|
|
9
|
+
* <li>window.accessToken 设置身份令牌, 直接返回.</li>
|
|
10
|
+
*/
|
|
11
|
+
static getBearer() {
|
|
12
|
+
if (window.accessToken != undefined && window.accessToken !== '') {
|
|
13
|
+
return window.accessToken;
|
|
14
|
+
}
|
|
15
|
+
const key = window.sessionKey || 'login_user';
|
|
16
|
+
const loginUser = JSON.parse(localStorage.getItem(key) || '{}');
|
|
17
|
+
if (window.accessTokenKey != undefined && window.accessTokenKey !== '') {
|
|
18
|
+
return loginUser[window.accessTokenKey];
|
|
19
|
+
}
|
|
20
|
+
return loginUser.accessToken || loginUser.token || undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 检查令牌是否存在.
|
|
24
|
+
*/
|
|
25
|
+
static checkAccessTokenExist() {
|
|
26
|
+
const bearer = Session.getBearer();
|
|
27
|
+
return !(bearer === undefined || bearer === '');
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 退出登录.
|
|
31
|
+
* @param url 退出之后的地址.
|
|
32
|
+
*/
|
|
33
|
+
static logout(url) {
|
|
34
|
+
localStorage.clear();
|
|
35
|
+
if (url !== undefined && url !== '') {
|
|
36
|
+
window.location.href = url;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Session = Session;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "beer-network",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "1.1.0-alpha.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"publish": "npm publish"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.0.2",
|
|
16
|
+
"@babel/eslint-parser": "^7.22.15",
|
|
17
|
+
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
|
18
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
19
|
+
"eslint": "^8.45.0",
|
|
20
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
21
|
+
"eslint-config-ali": "^14.0.2",
|
|
22
|
+
"eslint-plugin-import": "^2.28.1",
|
|
23
|
+
"eslint-plugin-react": "^7.33.2",
|
|
24
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
25
|
+
"eslint-plugin-react-refresh": "^0.4.3"
|
|
26
|
+
}
|
|
27
|
+
}
|