@jiangood/open-admin 3.0.14 → 3.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 +15 -2
- package/src/framework/biz/OrgTree.tsx +10 -6
- package/src/framework/biz/PermActions.tsx +5 -3
- package/src/framework/biz/RoleTree.tsx +10 -6
- package/src/framework/biz/index.tsx +1 -0
- package/src/framework/components/ContextMenu/index.tsx +17 -1
- package/src/framework/components/DownloadModal/index.tsx +40 -33
- package/src/framework/components/FormModal/index.tsx +10 -8
- package/src/framework/components/NamedIcon/index.tsx +2 -2
- package/src/framework/components/OrgSwitcher/index.tsx +6 -7
- package/src/framework/components/PageLoading/index.tsx +3 -3
- package/src/framework/components/ProTable/index.tsx +28 -25
- package/src/framework/components/ValueType/index.tsx +1 -1
- package/src/framework/config/themeConfig.ts +5 -5
- package/src/framework/fields/BaseRemoteSelect/index.tsx +23 -21
- package/src/framework/fields/FieldBoolean/index.tsx +6 -6
- package/src/framework/fields/FieldDate/index.tsx +10 -10
- package/src/framework/fields/FieldDateRange/index.tsx +20 -20
- package/src/framework/fields/FieldDictSelect/index.tsx +2 -2
- package/src/framework/fields/FieldEditor/index.tsx +1 -1
- package/src/framework/fields/FieldNumberRange/index.tsx +16 -17
- package/src/framework/fields/FieldPercent/index.tsx +2 -2
- package/src/framework/fields/FieldRemoteSelect/index.tsx +4 -4
- package/src/framework/fields/FieldRemoteTree/index.tsx +1 -1
- package/src/framework/fields/FieldRemoteTreeCascader/index.tsx +1 -1
- package/src/framework/fields/FieldRemoteTreeSelect/index.tsx +1 -1
- package/src/framework/fields/FieldSysOrgTree/index.tsx +1 -1
- package/src/framework/fields/FieldSysOrgTreeSelect/index.tsx +1 -1
- package/src/framework/fields/FieldTable/index.tsx +15 -13
- package/src/framework/fields/FieldTableSelect/index.tsx +5 -5
- package/src/framework/fields/FieldUploadFile/index.tsx +3 -3
- package/src/framework/fields/FieldUploadImage/index.tsx +31 -29
- package/src/framework/fields/types.ts +0 -2
- package/src/framework/router/PageFrame.tsx +1 -1
- package/src/framework/router/matcher.ts +2 -2
- package/src/framework/utils/ArrayUtils.ts +2 -2
- package/src/framework/utils/DateUtils.ts +1 -1
- package/src/framework/utils/{system/DictUtils.ts → DictUtils.ts} +1 -1
- package/src/framework/utils/EventBus.ts +7 -7
- package/src/framework/utils/HttpClient.ts +283 -0
- package/src/framework/utils/{system/PageUtils.ts → PageUtils.ts} +4 -4
- package/src/framework/utils/{system/PermUtils.ts → PermUtils.ts} +3 -3
- package/src/framework/utils/StringUtils.ts +10 -8
- package/src/framework/utils/TreeUtils.ts +3 -3
- package/src/framework/utils/UrlUtils.ts +3 -2
- package/src/framework/utils/index.ts +4 -1
- package/src/framework/views/ViewApproveStatus/index.tsx +1 -2
- package/src/framework/views/ViewBoolean/index.tsx +4 -1
- package/src/framework/views/ViewFile/index.tsx +4 -4
- package/src/framework/views/ViewImage/index.tsx +2 -2
- package/src/framework/views/ViewPassword/index.tsx +3 -2
- package/src/framework/views/ViewText/index.tsx +12 -3
- package/src/framework/views/types.ts +2 -4
- package/src/layouts/admin/AdminTabs.jsx +9 -0
- package/src/layouts/admin/AdminTabs.less +2 -2
- package/src/layouts/admin/HeaderRight.jsx +8 -7
- package/src/layouts/admin/TabLayout.jsx +2 -2
- package/src/layouts/admin/index.jsx +15 -11
- package/src/layouts/admin/index.less +16 -1
- package/src/layouts/index.tsx +24 -18
- package/src/pages/404.jsx +1 -1
- package/src/pages/article/$code.jsx +4 -4
- package/src/pages/index.jsx +1 -1
- package/src/pages/public/login.jsx +34 -27
- package/src/pages/public/test.jsx +1 -1
- package/src/pages/standalone/forceUpdatePwd.jsx +9 -7
- package/src/pages/system/article/index.jsx +9 -8
- package/src/pages/system/dict/index.jsx +19 -16
- package/src/pages/system/file/index.jsx +9 -9
- package/src/pages/system/job/index.jsx +17 -17
- package/src/pages/system/log/index.jsx +5 -7
- package/src/pages/system/org/index.jsx +31 -18
- package/src/pages/system/role/index.jsx +13 -12
- package/src/pages/system/role/rolePerm.jsx +25 -21
- package/src/pages/system/user/index.jsx +35 -33
- package/src/pages/system/user/userPerm.jsx +12 -12
- package/src/pages/userCenter/index.jsx +11 -9
- package/src/pages/userCenter/permView.jsx +7 -7
- package/src/framework/utils/system/HttpUtils.ts +0 -97
- package/src/framework/utils/system/index.ts +0 -4
|
@@ -2,14 +2,14 @@ import React from "react";
|
|
|
2
2
|
import {Button, Select} from "antd";
|
|
3
3
|
import type {TableColumnsType} from "antd";
|
|
4
4
|
import {ProTable} from "../../components";
|
|
5
|
-
import {
|
|
5
|
+
import {HttpClient} from "../../utils";
|
|
6
6
|
import type {FieldProps} from '../types';
|
|
7
7
|
|
|
8
8
|
export interface FieldTableSelectProps extends FieldProps<string> {
|
|
9
9
|
/** 数据加载地址 */
|
|
10
10
|
url: string;
|
|
11
11
|
/** 表格列配置(操作列由组件自动追加) */
|
|
12
|
-
columns: TableColumnsType<Record<string,
|
|
12
|
+
columns: TableColumnsType<Record<string, unknown>>;
|
|
13
13
|
/** 占位文本,默认 请搜索选择 */
|
|
14
14
|
placeholder?: string;
|
|
15
15
|
}
|
|
@@ -26,7 +26,7 @@ interface FieldTableSelectState {
|
|
|
26
26
|
*/
|
|
27
27
|
export class FieldTableSelect extends React.Component<FieldTableSelectProps, FieldTableSelectState> {
|
|
28
28
|
|
|
29
|
-
static defaultProps = {
|
|
29
|
+
static readonly defaultProps = {
|
|
30
30
|
placeholder: '请搜索选择',
|
|
31
31
|
};
|
|
32
32
|
|
|
@@ -62,13 +62,13 @@ export class FieldTableSelect extends React.Component<FieldTableSelectProps, Fie
|
|
|
62
62
|
label: record.name,
|
|
63
63
|
open: false
|
|
64
64
|
});
|
|
65
|
-
this.props.onChange
|
|
65
|
+
this.props.onChange?.(record.id);
|
|
66
66
|
}}>选择</Button>;
|
|
67
67
|
}
|
|
68
68
|
}]}
|
|
69
69
|
request={(params) => {
|
|
70
70
|
params.selected = this.props.value;
|
|
71
|
-
return
|
|
71
|
+
return HttpClient.get(this.props.url, params);
|
|
72
72
|
}}>
|
|
73
73
|
</ProTable>;
|
|
74
74
|
};
|
|
@@ -88,7 +88,7 @@ export class FieldUploadFile extends React.Component<FieldUploadFileProps, Field
|
|
|
88
88
|
if (f.response) { // 新上传的
|
|
89
89
|
const ajaxResult = f.response;
|
|
90
90
|
if (ajaxResult.success) {
|
|
91
|
-
const {objectName
|
|
91
|
+
const {objectName} = ajaxResult.data;
|
|
92
92
|
f.objectName = objectName;
|
|
93
93
|
objectNames.push(objectName);
|
|
94
94
|
} else {
|
|
@@ -102,9 +102,9 @@ export class FieldUploadFile extends React.Component<FieldUploadFileProps, Field
|
|
|
102
102
|
return objectNames;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
handleChange = ({fileList,
|
|
105
|
+
handleChange = ({fileList, file}: UploadChangeParam<SysUploadFile>) => {
|
|
106
106
|
const rs = file.response;
|
|
107
|
-
if (rs
|
|
107
|
+
if (rs?.success === false) {
|
|
108
108
|
this.setState({errorTitle: '上传失败', errorContent: rs.message});
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
@@ -4,9 +4,7 @@ import {DeleteOutlined, EyeOutlined, PlusOutlined} from "@ant-design/icons";
|
|
|
4
4
|
import Compressor from "compressorjs";
|
|
5
5
|
import Cropper from "cropperjs";
|
|
6
6
|
import "cropperjs/dist/cropper.css";
|
|
7
|
-
import {
|
|
8
|
-
import {ObjectUtils} from "../../utils";
|
|
9
|
-
import {UrlUtils} from "../../utils";
|
|
7
|
+
import {HttpClient, ObjectUtils, UrlUtils} from "../../utils";
|
|
10
8
|
import type {FieldProps} from '../types';
|
|
11
9
|
|
|
12
10
|
export interface FieldUploadImageProps extends FieldProps<string> {
|
|
@@ -93,7 +91,7 @@ function readDims(url: string): Promise<Dims> {
|
|
|
93
91
|
|
|
94
92
|
function compressToFile(source: Blob, options: {maxWidth?: number; maxHeight?: number; width?: number; height?: number; quality?: number}): Promise<File> {
|
|
95
93
|
return new Promise((resolve, reject) => {
|
|
96
|
-
new Compressor(source, {
|
|
94
|
+
new Compressor(source, { // NOSONAR (typescript:S1848) — compressorjs 通过构造函数副作用异步压缩,回调内 resolve/reject,实例无需保留
|
|
97
95
|
maxWidth: options.maxWidth,
|
|
98
96
|
maxHeight: options.maxHeight,
|
|
99
97
|
width: options.width,
|
|
@@ -141,9 +139,9 @@ async function compressToTarget(source: Blob, maxWidth: number | undefined, targ
|
|
|
141
139
|
return best;
|
|
142
140
|
}
|
|
143
141
|
|
|
144
|
-
export class FieldUploadImage extends React.Component<FieldUploadImageProps, FieldUploadImageState> {
|
|
142
|
+
export class FieldUploadImage extends React.Component<FieldUploadImageProps, FieldUploadImageState> { // NOSONAR: state 构造器中按 props 初始化,无法全部 readonly
|
|
145
143
|
|
|
146
|
-
state: FieldUploadImageState = {
|
|
144
|
+
state: FieldUploadImageState = { // NOSONAR: 构造器中按 props 初始化,无法 readonly
|
|
147
145
|
// 传入的参数
|
|
148
146
|
maxCount: 1,
|
|
149
147
|
thumbWidth: 300,
|
|
@@ -169,10 +167,10 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
169
167
|
constructor(props: FieldUploadImageProps) {
|
|
170
168
|
super(props);
|
|
171
169
|
ObjectUtils.copyPropertyIfPresent(props, this.state);
|
|
172
|
-
if (
|
|
170
|
+
if (props.value) this.state.objectNames = props.value.split(',');
|
|
173
171
|
}
|
|
174
172
|
|
|
175
|
-
componentDidUpdate(prevProps: FieldUploadImageProps, prevState: FieldUploadImageState) {
|
|
173
|
+
componentDidUpdate(prevProps: FieldUploadImageProps, prevState: FieldUploadImageState) { // NOSONAR: 顺序同步多个 props 到 state,拆分反而更难读
|
|
176
174
|
const next: Partial<FieldUploadImageState> = {};
|
|
177
175
|
if (this.props.maxCount !== prevProps.maxCount) next.maxCount = this.props.maxCount;
|
|
178
176
|
if (this.props.thumbWidth !== prevProps.thumbWidth) next.thumbWidth = this.props.thumbWidth;
|
|
@@ -199,7 +197,7 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
199
197
|
if (prevState.cropRatio !== this.state.cropRatio && this.cropperRef) {
|
|
200
198
|
if (!this.state.cropRatio) {
|
|
201
199
|
const box = this.cropperRef.getCropBoxData();
|
|
202
|
-
this.cropperRef.setAspectRatio(NaN);
|
|
200
|
+
this.cropperRef.setAspectRatio(Number.NaN);
|
|
203
201
|
this.cropperRef.setCropBoxData(box);
|
|
204
202
|
} else {
|
|
205
203
|
this.cropperRef.setAspectRatio(this.state.cropRatio.width / this.state.cropRatio.height);
|
|
@@ -229,7 +227,7 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
229
227
|
const init = () => {
|
|
230
228
|
if (this.cropperRef) this.cropperRef.destroy();
|
|
231
229
|
this.cropperRef = new Cropper(el, {
|
|
232
|
-
aspectRatio: this.state.cropRatio ? this.state.cropRatio.width / this.state.cropRatio.height : NaN,
|
|
230
|
+
aspectRatio: this.state.cropRatio ? this.state.cropRatio.width / this.state.cropRatio.height : Number.NaN,
|
|
233
231
|
viewMode: 1,
|
|
234
232
|
autoCropArea: 0.85,
|
|
235
233
|
dragMode: 'move',
|
|
@@ -293,7 +291,7 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
293
291
|
this.previewUrlsRef = [cUrl];
|
|
294
292
|
this.setState({preview: {cUrl, cFile, cSize: cFile.size, cdims}, tool: undefined});
|
|
295
293
|
message.success(`已压缩:${cdims.width} x ${cdims.height} / ${formatSize(cFile.size)}`);
|
|
296
|
-
} catch
|
|
294
|
+
} catch {
|
|
297
295
|
message.error('压缩失败');
|
|
298
296
|
}
|
|
299
297
|
};
|
|
@@ -320,7 +318,7 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
320
318
|
this.setState({originalDims: dims});
|
|
321
319
|
// 默认展示原图,不做压缩处理,用户可点击「压缩」按钮手动压缩
|
|
322
320
|
await this.loadOriginalPreview(file, url, dims);
|
|
323
|
-
} catch
|
|
321
|
+
} catch {
|
|
324
322
|
message.error('读取图片失败');
|
|
325
323
|
this.closeModal();
|
|
326
324
|
return;
|
|
@@ -365,22 +363,34 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
365
363
|
fd.append('file', this.state.preview.cFile);
|
|
366
364
|
fd.append('thumb', tFile);
|
|
367
365
|
fd.append('isPublic', String(this.state.isPublic));
|
|
368
|
-
const rs = await
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
366
|
+
const rs = await HttpClient.post('admin/sysFile/uploadImage', fd, null, {toastError: false});
|
|
367
|
+
this.setState(
|
|
368
|
+
(prevState) => ({objectNames: [...prevState.objectNames, rs.data.objectName]}),
|
|
369
|
+
() => this.props.onChange?.(this.state.objectNames.join(','))
|
|
370
|
+
);
|
|
372
371
|
this.closeModal();
|
|
373
372
|
} catch (e) {
|
|
374
|
-
message.error(
|
|
373
|
+
message.error(HttpClient.errToMsg(e));
|
|
375
374
|
} finally {
|
|
376
375
|
this.setState({uploading: false});
|
|
377
376
|
}
|
|
378
377
|
};
|
|
379
378
|
|
|
380
379
|
private removeImage = (name: string) => {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
380
|
+
this.setState(
|
|
381
|
+
(prevState) => ({objectNames: prevState.objectNames.filter((n) => n !== name)}),
|
|
382
|
+
() => this.props.onChange?.(this.state.objectNames.join(','))
|
|
383
|
+
);
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
private renderCanvas = (preview, canvasImg, originalUrl) => {
|
|
387
|
+
if (this.state.tool === 'crop') {
|
|
388
|
+
return <img key="crop-canvas" ref={this.imgRef} src={preview?.cUrl || originalUrl} style={{maxWidth: '100%', maxHeight: '100%'}} alt="待裁切"/>;
|
|
389
|
+
}
|
|
390
|
+
if (canvasImg?.url) {
|
|
391
|
+
return <img src={canvasImg.url} style={{maxWidth: '100%', maxHeight: '100%'}} alt="预览"/>;
|
|
392
|
+
}
|
|
393
|
+
return <div style={{color: '#999'}}>生成中...</div>;
|
|
384
394
|
};
|
|
385
395
|
|
|
386
396
|
render() {
|
|
@@ -479,13 +489,7 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
479
489
|
flex: 1, minHeight: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
480
490
|
background: '#f5f5f5', borderRadius: 8, border: '1px dashed #d9d9d9', position: 'relative', overflow: 'hidden',
|
|
481
491
|
}}>
|
|
482
|
-
{
|
|
483
|
-
<img key="crop-canvas" ref={this.imgRef} src={preview?.cUrl || originalUrl} style={{maxWidth: '100%', maxHeight: '100%'}} alt="待裁切"/>
|
|
484
|
-
) : canvasImg?.url ? (
|
|
485
|
-
<img src={canvasImg.url} style={{maxWidth: '100%', maxHeight: '100%'}} alt="预览"/>
|
|
486
|
-
) : (
|
|
487
|
-
<div style={{color: '#999'}}>生成中...</div>
|
|
488
|
-
)}
|
|
492
|
+
{this.renderCanvas(preview, canvasImg, originalUrl)}
|
|
489
493
|
</div>
|
|
490
494
|
</div>
|
|
491
495
|
|
|
@@ -506,7 +510,6 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
506
510
|
{tool !== 'crop' && <Divider style={{margin: '0 0 12px'}}/>}
|
|
507
511
|
<div style={{fontWeight: 600, marginBottom: 8}}>{tool === 'crop' ? '裁切信息' : '图片信息'}</div>
|
|
508
512
|
{tool === 'crop' ? (
|
|
509
|
-
<>
|
|
510
513
|
<Radio.Group
|
|
511
514
|
value={cropRatio ? formatRatio(cropRatio.width, cropRatio.height) : 'free'}
|
|
512
515
|
onChange={(e) => {
|
|
@@ -521,7 +524,6 @@ export class FieldUploadImage extends React.Component<FieldUploadImageProps, Fie
|
|
|
521
524
|
</Radio>
|
|
522
525
|
))}
|
|
523
526
|
</Radio.Group>
|
|
524
|
-
</>
|
|
525
527
|
) : (
|
|
526
528
|
<>
|
|
527
529
|
{canvasImg && (
|
|
@@ -10,7 +10,7 @@ interface PageFrameProps {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export class PageFrame extends React.Component<PageFrameProps> {
|
|
13
|
-
componentRef = React.createRef<
|
|
13
|
+
componentRef = React.createRef<{onShow?: () => void}>();
|
|
14
14
|
|
|
15
15
|
componentDidMount() {
|
|
16
16
|
this.callOnShowIfActive();
|
|
@@ -6,7 +6,7 @@ function safeDecodeURIComponent(s: string): string {
|
|
|
6
6
|
|
|
7
7
|
export interface RouteDef {
|
|
8
8
|
path: string;
|
|
9
|
-
component: ComponentType<
|
|
9
|
+
component: ComponentType<unknown>;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
interface InternalRoute extends RouteDef {
|
|
@@ -14,7 +14,7 @@ interface InternalRoute extends RouteDef {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface MatchResult {
|
|
17
|
-
component: ComponentType<
|
|
17
|
+
component: ComponentType<unknown>;
|
|
18
18
|
params: Record<string, string>;
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class ArrayUtils {
|
|
2
2
|
static contains<T>(arr: T[], item: T): boolean {
|
|
3
|
-
return arr.
|
|
3
|
+
return arr.includes(item);
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
static containsAny<T>(arr: T[], ...items: T[]): boolean {
|
|
@@ -10,7 +10,7 @@ export class ArrayUtils {
|
|
|
10
10
|
return false;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
static maxBy<T extends Record<K,
|
|
13
|
+
static maxBy<T extends Record<K, unknown>, K extends keyof T>(arr: T[], key: K): T | undefined {
|
|
14
14
|
if (arr == null || arr.length === 0) return undefined;
|
|
15
15
|
let maxElement: T | undefined = undefined;
|
|
16
16
|
let maxValue = -Infinity;
|
|
@@ -62,7 +62,7 @@ export class DateUtils {
|
|
|
62
62
|
static friendlyTotalTime(time: number | string | null): string | null {
|
|
63
63
|
if (time == null || time === '-') return null;
|
|
64
64
|
let seconds: number;
|
|
65
|
-
if (typeof time === 'string') seconds = parseInt(time, 10) / 1000;
|
|
65
|
+
if (typeof time === 'string') seconds = Number.parseInt(time, 10) / 1000;
|
|
66
66
|
else seconds = time / 1000;
|
|
67
67
|
seconds = Math.floor(seconds);
|
|
68
68
|
if (seconds < 60) return seconds + '秒';
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
type EventCallback = (...args:
|
|
1
|
+
type EventCallback = (...args: unknown[]) => void;
|
|
2
2
|
|
|
3
3
|
interface EventEntry {
|
|
4
4
|
fn: EventCallback & { __callback?: EventCallback };
|
|
5
|
-
ctx?:
|
|
5
|
+
ctx?: unknown;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export class EventBus {
|
|
9
9
|
private static __stack: Record<string, EventEntry[]> = {};
|
|
10
10
|
|
|
11
|
-
static on<T extends
|
|
11
|
+
static on<T extends unknown[] = []>(name: string, callback: (...args: T) => void, ctx?: unknown): () => void {
|
|
12
12
|
if (!EventBus.__stack[name]) {
|
|
13
13
|
EventBus.__stack[name] = [];
|
|
14
14
|
}
|
|
@@ -16,9 +16,9 @@ export class EventBus {
|
|
|
16
16
|
return () => EventBus.off(name, callback);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
static once<T extends
|
|
19
|
+
static once<T extends unknown[] = []>(name: string, callback: (...args: T) => void, ctx?: unknown): () => void {
|
|
20
20
|
const unsubscribe = () => EventBus.off(name, callback);
|
|
21
|
-
const listener: EventEntry['fn'] = (...args:
|
|
21
|
+
const listener: EventEntry['fn'] = (...args: unknown[]) => {
|
|
22
22
|
EventBus.off(name, listener);
|
|
23
23
|
callback.apply(ctx, args);
|
|
24
24
|
};
|
|
@@ -28,7 +28,7 @@ export class EventBus {
|
|
|
28
28
|
return unsubscribe;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
static emit<T extends
|
|
31
|
+
static emit<T extends unknown[] = []>(name: string, ...args: T): void {
|
|
32
32
|
const list = EventBus.__stack[name];
|
|
33
33
|
if (list !== undefined) {
|
|
34
34
|
list.slice(0).forEach(entry => {
|
|
@@ -37,7 +37,7 @@ export class EventBus {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
static off<T extends
|
|
40
|
+
static off<T extends unknown[] = []>(name: string, callback?: (...args: T) => void): void {
|
|
41
41
|
const list = EventBus.__stack[name];
|
|
42
42
|
if (list === undefined) return;
|
|
43
43
|
if (callback === undefined) {
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import type {AxiosProgressEvent, AxiosRequestConfig} from "axios";
|
|
3
|
+
import qs from 'qs';
|
|
4
|
+
import {message as messageApi} from "antd";
|
|
5
|
+
import {EventBus} from "./EventBus";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 单一 Promise 风格 HTTP 工具类(所有请求方法均返回 Promise,无 success/error 回调)
|
|
9
|
+
*
|
|
10
|
+
* 核心规则:
|
|
11
|
+
* - 请求方法返回 Promise<AjaxBody<T>>:成功 resolve 整个 AjaxResult 响应体,失败 reject(AjaxError {code, message})
|
|
12
|
+
* - 成功 resolve 的是原始响应体 {success, code, data, message, traceId, ...}(extData 等附加字段在顶层),
|
|
13
|
+
* 业务取数据用 rs.data,取提示文案用 rs.message
|
|
14
|
+
* - 成功且后端返回了 message 时自动 message.success(body.message),传 {toastSuccess: false} 可关闭
|
|
15
|
+
* - 失败默认自动 console.warn + message.error,并 reject(AjaxError);传 {toastError: false} 可完全静默(仅 reject)
|
|
16
|
+
* - 返回值被忽略也安全:内部已对返回的 Promise 挂 no-op catch,不会产生 unhandled rejection
|
|
17
|
+
* - reject 的 AjaxError {code, message}:业务失败 code 为后端业务码,网络异常 code 为 HTTP 状态码,message 始终为人类可读提示
|
|
18
|
+
* - HTTP 401 或业务 code=401 均广播 loginExpired(全局关注点),弹错仍按 toastError 决定
|
|
19
|
+
*
|
|
20
|
+
* 用法:
|
|
21
|
+
* ```ts
|
|
22
|
+
* // 提交:成功自动弹后端 message(如"更新成功"),失败自动弹错并 reject(可用 await 感知结果,如 FormModal 提交失败保持弹窗)
|
|
23
|
+
* await HttpClient.post('admin/sysUser/create', values);
|
|
24
|
+
*
|
|
25
|
+
* // 取返回数据与后端提示文案
|
|
26
|
+
* const {data, message} = await HttpClient.post('admin/sysUser/create', values);
|
|
27
|
+
*
|
|
28
|
+
* // 成功自控提示(关闭自动弹后由业务决定)
|
|
29
|
+
* await HttpClient.post('admin/sysUser/delete', {id}, null, {toastSuccess: false});
|
|
30
|
+
* messageApi.success('删除成功');
|
|
31
|
+
* reload();
|
|
32
|
+
*
|
|
33
|
+
* // 失败由调用方接管,不弹错(静默探测)
|
|
34
|
+
* HttpClient.get('admin/public/site-info', null, {toastError: false})
|
|
35
|
+
* .then(({data}) => render(data))
|
|
36
|
+
* .catch((e) => myHandle(e));
|
|
37
|
+
*
|
|
38
|
+
* // $.ajax 形态
|
|
39
|
+
* HttpClient.ajax({url: 'admin/sysUser/page', method: 'GET', params}).then((rs) => render(rs.data));
|
|
40
|
+
*
|
|
41
|
+
* // 下载(返回 Promise<Blob>,成功已触发浏览器保存)
|
|
42
|
+
* HttpClient.download({url: 'admin/sysUser/export', method: 'POST', data: {deptId: 1},
|
|
43
|
+
* onDownloadProgress: ({loaded, total}) => console.log(loaded, total)})
|
|
44
|
+
* .catch(() => {});
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
/** 统一的错误数据格式:业务失败与网络异常都包装成此结构传给 reject 的 error */
|
|
48
|
+
export interface AjaxError {
|
|
49
|
+
code?: number | string;
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 后端统一响应结构 AjaxResult;putExtData 附加字段在顶层,data 为业务数据 */
|
|
54
|
+
export interface AjaxBody<T = unknown> {
|
|
55
|
+
success: boolean;
|
|
56
|
+
code: number;
|
|
57
|
+
data: T;
|
|
58
|
+
message?: string;
|
|
59
|
+
traceId?: string;
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 请求选项:headers 自定义请求头;toastError=false 时失败不自动弹错(完全静默,仅 reject);toastSuccess=false 时成功不自动弹后端 message */
|
|
64
|
+
export interface RequestOptions {
|
|
65
|
+
headers?: AxiosRequestConfig['headers'];
|
|
66
|
+
toastError?: boolean;
|
|
67
|
+
toastSuccess?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface AjaxSettings {
|
|
71
|
+
url: string;
|
|
72
|
+
method?: 'GET' | 'POST';
|
|
73
|
+
params?: unknown;
|
|
74
|
+
data?: unknown;
|
|
75
|
+
headers?: AxiosRequestConfig['headers'];
|
|
76
|
+
toastError?: boolean;
|
|
77
|
+
toastSuccess?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface DownloadSettings {
|
|
81
|
+
url: string;
|
|
82
|
+
method?: 'GET' | 'POST';
|
|
83
|
+
params?: unknown;
|
|
84
|
+
data?: unknown;
|
|
85
|
+
fileName?: string;
|
|
86
|
+
onDownloadProgress?: (progress: {loaded: number; total: number}) => void;
|
|
87
|
+
toastError?: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const axiosInstance = axios.create({
|
|
91
|
+
baseURL: import.meta.env.VITE_SERVER_SERVLET_CONTEXT_PATH,
|
|
92
|
+
withCredentials: true,
|
|
93
|
+
headers: {'Content-Type': 'application/json'},
|
|
94
|
+
paramsSerializer: (params: Record<string, unknown>) => qs.stringify(params, {indices: false})
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
export class HttpClient {
|
|
98
|
+
static ajax<T = unknown>(settings: AjaxSettings): Promise<AjaxBody<T>> {
|
|
99
|
+
const {url, method = 'GET', params, data, headers, toastError, toastSuccess} = settings;
|
|
100
|
+
return HttpClient.coreRequest<T>({
|
|
101
|
+
url, method, params, data, headers, toastError, toastSuccess
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static get<T = unknown>(url: string, params: unknown = null, opts?: RequestOptions): Promise<AjaxBody<T>> {
|
|
106
|
+
return HttpClient.ajax<T>({url, method: 'GET', params, ...opts});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
static post<T = unknown>(url: string, data: unknown = null, params: unknown = null, opts?: RequestOptions): Promise<AjaxBody<T>> {
|
|
110
|
+
return HttpClient.ajax<T>({url, method: 'POST', data, params, ...opts});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static postForm<T = unknown>(url: string, data: unknown, opts?: RequestOptions): Promise<AjaxBody<T>> {
|
|
114
|
+
return HttpClient.ajax<T>({
|
|
115
|
+
url,
|
|
116
|
+
method: 'POST',
|
|
117
|
+
data: qs.stringify(data),
|
|
118
|
+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
|
119
|
+
...opts
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 下载文件(responseType: blob),返回 Promise<Blob>。
|
|
125
|
+
* 成功即触发浏览器保存并 resolve(blob);JSON 错误响应自动识别为失败并 reject;
|
|
126
|
+
* 文件名优先 settings.fileName,否则从 Content-Disposition 解析。
|
|
127
|
+
* 失败默认自动弹错,传 {toastError: false} 可静默(仅 reject)。
|
|
128
|
+
*/
|
|
129
|
+
static download(settings: DownloadSettings): Promise<Blob> {
|
|
130
|
+
const {url, method = 'GET', params, data, fileName, onDownloadProgress, toastError = true} = settings;
|
|
131
|
+
|
|
132
|
+
return new Promise<Blob>((resolve, reject) => {
|
|
133
|
+
axiosInstance({
|
|
134
|
+
url: HttpClient.prefixUrl(url),
|
|
135
|
+
method,
|
|
136
|
+
params,
|
|
137
|
+
data,
|
|
138
|
+
responseType: 'blob',
|
|
139
|
+
onDownloadProgress: (progressEvent: AxiosProgressEvent) => {
|
|
140
|
+
if (onDownloadProgress) {
|
|
141
|
+
onDownloadProgress({loaded: progressEvent.loaded, total: progressEvent.total});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}).then((response) => {
|
|
145
|
+
const blob: Blob = response.data;
|
|
146
|
+
|
|
147
|
+
if (blob.type === 'application/json') {
|
|
148
|
+
const reader = new FileReader();
|
|
149
|
+
reader.onerror = () => HttpClient.fail(url, toastError, reject, {message: '读取下载数据失败'}, '下载失败');
|
|
150
|
+
reader.onload = () => {
|
|
151
|
+
try {
|
|
152
|
+
const rs = JSON.parse(reader.result as string);
|
|
153
|
+
if (rs.code === 401) {
|
|
154
|
+
EventBus.emit('loginExpired');
|
|
155
|
+
}
|
|
156
|
+
HttpClient.fail(url, toastError, reject, {code: rs.code, message: rs.message || '下载失败'}, '下载失败');
|
|
157
|
+
} catch {
|
|
158
|
+
HttpClient.fail(url, toastError, reject, {message: '解析错误响应失败'}, '下载失败');
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
reader.readAsText(blob, 'utf-8');
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let filename = fileName;
|
|
166
|
+
if (!filename) {
|
|
167
|
+
const contentDisposition = response.headers['content-disposition'] || response.headers['Content-Disposition'];
|
|
168
|
+
if (contentDisposition) {
|
|
169
|
+
const match = /filename\*?=(?:['"]?)(?:UTF-8''|)(.+?)(?:['"]?$|;)/i.exec(contentDisposition);
|
|
170
|
+
let parsedName = match?.[1] ? match[1].trim() : 'download.file';
|
|
171
|
+
try {
|
|
172
|
+
parsedName = decodeURIComponent(parsedName.replaceAll('"', ''));
|
|
173
|
+
} catch {
|
|
174
|
+
parsedName = parsedName.replaceAll('"', '');
|
|
175
|
+
}
|
|
176
|
+
filename = parsedName;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const objectUrl = window.URL.createObjectURL(new Blob([blob]));
|
|
181
|
+
const link = document.createElement('a');
|
|
182
|
+
link.style.display = 'none';
|
|
183
|
+
link.href = objectUrl;
|
|
184
|
+
link.download = filename || 'download.file';
|
|
185
|
+
document.body.appendChild(link);
|
|
186
|
+
link.click();
|
|
187
|
+
link.remove();
|
|
188
|
+
window.URL.revokeObjectURL(objectUrl);
|
|
189
|
+
resolve(blob);
|
|
190
|
+
}).catch((e: unknown) => {
|
|
191
|
+
HttpClient.fail(url, toastError, reject, e, '下载失败');
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 从 axios 异常/Error/AjaxError 中提取人类可读错误消息。
|
|
198
|
+
* catch 收到的已是 AjaxError,直接取 e.message 即可;此方法供传原始异常的场景兜底。
|
|
199
|
+
*/
|
|
200
|
+
static errToMsg(e: unknown, defaultMsg = '操作失败'): string {
|
|
201
|
+
if (axios.isAxiosError(e)) {
|
|
202
|
+
const responseData = e.response?.data;
|
|
203
|
+
const status = e.response?.status;
|
|
204
|
+
if (status === 504) return '504 请求后端服务失败';
|
|
205
|
+
if (responseData?.message) return responseData.message;
|
|
206
|
+
if (e.message) return e.message;
|
|
207
|
+
return defaultMsg;
|
|
208
|
+
}
|
|
209
|
+
if (e instanceof Error) return e.message || defaultMsg;
|
|
210
|
+
if (e && typeof e === 'object' && (e as {message?: unknown}).message) return (e as {message: string}).message;
|
|
211
|
+
return defaultMsg;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 把任意异常(AxiosError / Error / AjaxError / 业务消息)统一包装成 AjaxError */
|
|
215
|
+
private static toAjaxError(e: unknown, defaultMsg = '操作失败'): AjaxError {
|
|
216
|
+
if (e && typeof e === 'object' && typeof (e as {message?: unknown}).message === 'string') {
|
|
217
|
+
const ajaxErr = e as {code?: number | string; message: string};
|
|
218
|
+
return {code: ajaxErr.code, message: ajaxErr.message};
|
|
219
|
+
}
|
|
220
|
+
return {message: HttpClient.errToMsg(e, defaultMsg)};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private static prefixUrl(url: string): string {
|
|
224
|
+
return url.startsWith('admin') ? '/' + url : url;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private static coreRequest<T = unknown>(settings: AjaxSettings): Promise<AjaxBody<T>> {
|
|
228
|
+
const {url, method, params, data, headers, toastError = true, toastSuccess = true} = settings;
|
|
229
|
+
const config: AxiosRequestConfig = {url: HttpClient.prefixUrl(url), method, params, data, headers};
|
|
230
|
+
|
|
231
|
+
// FormData 请求交由浏览器自动生成带 boundary 的 multipart Content-Type
|
|
232
|
+
if (typeof FormData !== 'undefined' && data instanceof FormData) {
|
|
233
|
+
config.headers = {...config.headers, 'Content-Type': undefined};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const promise = new Promise<AjaxBody<T>>((resolve, reject) => {
|
|
237
|
+
axiosInstance(config).then((response) => {
|
|
238
|
+
const body = response.data as AjaxBody<T>;
|
|
239
|
+
const {success: ok, message} = body;
|
|
240
|
+
if (ok == undefined) {
|
|
241
|
+
resolve(body);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (!ok) {
|
|
245
|
+
if (body.code === 401) {
|
|
246
|
+
EventBus.emit('loginExpired');
|
|
247
|
+
}
|
|
248
|
+
HttpClient.fail(url, toastError, reject, {code: body.code, message: message || '操作失败'});
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (toastSuccess && message) {
|
|
252
|
+
messageApi.success(message);
|
|
253
|
+
}
|
|
254
|
+
resolve(body);
|
|
255
|
+
}).catch((e: unknown) => {
|
|
256
|
+
HttpClient.fail(url, toastError, reject, e);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// 调用方忽略返回值时兜底,避免 unhandled rejection;await 的调用方仍能正常收到 reject
|
|
261
|
+
promise.catch(() => {});
|
|
262
|
+
return promise;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 统一的失败处理:HTTP 401 广播 loginExpired 并弹"登录过期",其余提取可读消息后按 toastError 决定是否弹错,最终 reject(AjaxError) */
|
|
266
|
+
private static fail(url: string, toastError: boolean, reject: (reason?: unknown) => void, e: unknown, label = '请求失败'): void {
|
|
267
|
+
if (axios.isAxiosError(e) && e.response?.status === 401) {
|
|
268
|
+
EventBus.emit('loginExpired');
|
|
269
|
+
const err: AjaxError = {code: 401, message: '登录过期'};
|
|
270
|
+
if (toastError) {
|
|
271
|
+
messageApi.error('登录过期');
|
|
272
|
+
}
|
|
273
|
+
reject(err);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const err = HttpClient.toAjaxError(e);
|
|
277
|
+
if (toastError) {
|
|
278
|
+
console.warn(`[HttpClient] ${label}: ${url}`, err.message);
|
|
279
|
+
messageApi.error(err.message);
|
|
280
|
+
}
|
|
281
|
+
reject(err);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {history} from '
|
|
2
|
-
import {StringUtils} from "
|
|
3
|
-
import {UrlUtils} from "
|
|
4
|
-
import {EventBus} from "
|
|
1
|
+
import {history} from '../router';
|
|
2
|
+
import {StringUtils} from "./StringUtils";
|
|
3
|
+
import {UrlUtils} from "./UrlUtils";
|
|
4
|
+
import {EventBus} from "./EventBus";
|
|
5
5
|
|
|
6
6
|
export class PageUtils {
|
|
7
7
|
static redirectToLogin(): void {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {GlobalData} from '
|
|
2
|
-
import {ArrayUtils} from '
|
|
1
|
+
import {GlobalData} from '../GlobalData';
|
|
2
|
+
import {ArrayUtils} from './ArrayUtils';
|
|
3
3
|
|
|
4
4
|
export class PermUtils {
|
|
5
5
|
static getPermissions(): string[] {
|
|
@@ -13,7 +13,7 @@ export class PermUtils {
|
|
|
13
13
|
const permissions = PermUtils.getPermissions();
|
|
14
14
|
if (permissions.length === 0) return false;
|
|
15
15
|
if (ArrayUtils.contains(permissions, "*")) return true;
|
|
16
|
-
return permissions.
|
|
16
|
+
return permissions.includes(perm);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
static isPermitted(p: string | null | undefined): boolean {
|