@peng_kai/kit 0.1.4 → 0.1.6
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/admin/components/filter/src/FilterDrawer.vue +153 -153
- package/admin/components/filter/src/FilterParam.vue +76 -76
- package/admin/components/filter/src/FilterReset.vue +2 -2
- package/admin/components/filter/src/useFilterParams.ts +9 -0
- package/admin/components/scroll-nav/index.ts +1 -1
- package/admin/components/scroll-nav/src/ScrollNav.vue +59 -59
- package/admin/components/text/index.ts +13 -13
- package/admin/components/text/src/Amount.vue +117 -117
- package/admin/components/text/src/Datetime.vue +48 -48
- package/admin/components/text/src/Duration.vue +26 -26
- package/admin/components/text/src/Hash.vue +51 -51
- package/admin/components/text/src/createTagGetter.ts +13 -13
- package/admin/hooks/useMenu.ts +128 -128
- package/admin/hooks/usePage.ts +141 -141
- package/admin/hooks/usePageTab.ts +35 -35
- package/admin/layout/large/Breadcrumb.vue +69 -69
- package/admin/layout/large/Content.vue +24 -24
- package/admin/layout/large/Menu.vue +69 -69
- package/admin/layout/large/PageTab.vue +71 -71
- package/admin/permission/index.ts +4 -4
- package/admin/permission/routerGuard.ts +43 -43
- package/admin/permission/usePermission.ts +52 -52
- package/admin/permission/vuePlugin.ts +30 -30
- package/admin/route-guards/index.ts +3 -3
- package/admin/route-guards/pageProgress.ts +27 -27
- package/admin/route-guards/pageTitle.ts +19 -19
- package/admin/styles/classCover.scss +59 -2
- package/admin/styles/globalCover.scss +54 -54
- package/admin/types/assist.ts +2 -2
- package/antd/components/InputNumberRange.vue +53 -53
- package/antd/directives/formLabelAlign.ts +36 -36
- package/antd/hooks/useAntdDrawer.ts +73 -73
- package/antd/hooks/useAntdTable.ts +82 -71
- package/package.json +55 -54
- package/request/helpers.ts +49 -49
- package/request/interceptors/toLogin.ts +15 -15
- package/request/type.d.ts +92 -92
- package/stylelint.config.cjs +7 -7
- package/tsconfig.json +50 -50
- package/utils/date.ts +44 -0
- package/utils/index.ts +14 -8
- package/utils/number.ts +49 -0
- package/vue/components/infinite-query/index.ts +1 -1
- package/vue/components/infinite-query/src/InfiniteQuery.vue +205 -205
- package/vue/components/infinite-query/src/useCreateTrigger.ts +39 -39
- package/vue/hooks/useComponentRef.ts +7 -1
- package/vue/hooks/useIsDark.ts +5 -0
- package/vue/hooks/useIsMounted.ts +3 -0
- package/vue/hooks/useTeleportTarget.ts +35 -2
- package/vue/index.ts +1 -1
- package/pnpm-lock.yaml +0 -5242
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { Button as AButton, Drawer as ADrawer, Space as ASpace } from 'ant-design-vue';
|
|
2
|
-
import { defineComponent, h, isProxy, reactive, toRef, toRefs } from 'vue';
|
|
3
|
-
import type { Component } from 'vue';
|
|
4
|
-
import type { ButtonProps, DrawerProps } from 'ant-design-vue';
|
|
5
|
-
import type { ComponentProps } from 'vue-component-type-helpers';
|
|
6
|
-
import type { Writable } from 'type-fest';
|
|
7
|
-
import { useComponentRef } from '../../vue';
|
|
8
|
-
|
|
9
|
-
const defaultDrawerProps: DrawerProps = { open: false, destroyOnClose: true };
|
|
10
|
-
|
|
11
|
-
interface IComponentConfig<Comp extends Component> {
|
|
12
|
-
is: Comp
|
|
13
|
-
props?: Writable<ComponentProps<Comp>>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function useAntdDrawer<Comp extends Component>(
|
|
17
|
-
comp: IComponentConfig<Comp> | Comp,
|
|
18
|
-
drawerProps = defaultDrawerProps,
|
|
19
|
-
) {
|
|
20
|
-
const _comp = ({ props: {}, ...((comp as any)?.is ? comp : { is: comp }) }) as Required<IComponentConfig<Comp>>;
|
|
21
|
-
const compProps = reactive(_comp.props);
|
|
22
|
-
const compRef = useComponentRef(_comp.is);
|
|
23
|
-
const _drawerProps: DrawerProps = reactive({
|
|
24
|
-
...defaultDrawerProps,
|
|
25
|
-
...isProxy(drawerProps) ? toRefs(drawerProps) : drawerProps,
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
const open = (newBodyProps?: Partial<typeof compProps>, newAntdModalProps?: Omit<Partial<DrawerProps>, 'open'>) => {
|
|
29
|
-
Object.assign(_drawerProps, newAntdModalProps);
|
|
30
|
-
Object.assign(compProps, newBodyProps);
|
|
31
|
-
_drawerProps.open = true;
|
|
32
|
-
};
|
|
33
|
-
const close = () => {
|
|
34
|
-
_drawerProps.open = false;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
setup() {
|
|
39
|
-
const cancelBtnProps: ButtonProps = reactive({ onClick: close });
|
|
40
|
-
const confirmBtnProps: ButtonProps = reactive({
|
|
41
|
-
type: 'primary',
|
|
42
|
-
loading: toRef(() => (compRef as any)?.loading),
|
|
43
|
-
onClick: () => (compRef as any)?.confirm?.(),
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return { cancelBtnProps, confirmBtnProps };
|
|
47
|
-
},
|
|
48
|
-
render() {
|
|
49
|
-
const { cancelBtnProps, confirmBtnProps } = this;
|
|
50
|
-
|
|
51
|
-
return h(ASpace, {}, () => [
|
|
52
|
-
h(AButton, cancelBtnProps, () => '取消'),
|
|
53
|
-
h(AButton, confirmBtnProps, () => '确定'),
|
|
54
|
-
]);
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
const PresetComponent = defineComponent({
|
|
58
|
-
render() {
|
|
59
|
-
return h(ADrawer, _drawerProps, {
|
|
60
|
-
default: () => h(_comp.is, compProps as any),
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
_drawerProps.
|
|
66
|
-
_drawerProps['onUpdate:open'] = (visiable) => {
|
|
67
|
-
_drawerProps.open = visiable;
|
|
68
|
-
};
|
|
69
|
-
(compProps as any).ref = compRef;
|
|
70
|
-
(compProps as any).onClose = close;
|
|
71
|
-
|
|
72
|
-
return { PresetComponent, drawerProps: _drawerProps, open, close };
|
|
73
|
-
}
|
|
1
|
+
import { Button as AButton, Drawer as ADrawer, Space as ASpace } from 'ant-design-vue';
|
|
2
|
+
import { defineComponent, h, isProxy, reactive, toRef, toRefs } from 'vue';
|
|
3
|
+
import type { Component } from 'vue';
|
|
4
|
+
import type { ButtonProps, DrawerProps } from 'ant-design-vue';
|
|
5
|
+
import type { ComponentProps } from 'vue-component-type-helpers';
|
|
6
|
+
import type { Writable } from 'type-fest';
|
|
7
|
+
import { useComponentRef } from '../../vue';
|
|
8
|
+
|
|
9
|
+
const defaultDrawerProps: DrawerProps = { open: false, destroyOnClose: true, rootClassName: 'antd-cover__basic-drawer' };
|
|
10
|
+
|
|
11
|
+
interface IComponentConfig<Comp extends Component> {
|
|
12
|
+
is: Comp
|
|
13
|
+
props?: Writable<ComponentProps<Comp>>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function useAntdDrawer<Comp extends Component>(
|
|
17
|
+
comp: IComponentConfig<Comp> | Comp,
|
|
18
|
+
drawerProps = defaultDrawerProps,
|
|
19
|
+
) {
|
|
20
|
+
const _comp = ({ props: {}, ...((comp as any)?.is ? comp : { is: comp }) }) as Required<IComponentConfig<Comp>>;
|
|
21
|
+
const compProps = reactive(_comp.props);
|
|
22
|
+
const compRef = useComponentRef(_comp.is);
|
|
23
|
+
const _drawerProps: DrawerProps = reactive({
|
|
24
|
+
...defaultDrawerProps,
|
|
25
|
+
...isProxy(drawerProps) ? toRefs(drawerProps) : drawerProps,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const open = (newBodyProps?: Partial<typeof compProps>, newAntdModalProps?: Omit<Partial<DrawerProps>, 'open'>) => {
|
|
29
|
+
Object.assign(_drawerProps, newAntdModalProps);
|
|
30
|
+
Object.assign(compProps, newBodyProps);
|
|
31
|
+
_drawerProps.open = true;
|
|
32
|
+
};
|
|
33
|
+
const close = () => {
|
|
34
|
+
_drawerProps.open = false;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const DrawerFooter = defineComponent({
|
|
38
|
+
setup() {
|
|
39
|
+
const cancelBtnProps: ButtonProps = reactive({ onClick: close });
|
|
40
|
+
const confirmBtnProps: ButtonProps = reactive({
|
|
41
|
+
type: 'primary',
|
|
42
|
+
loading: toRef(() => (compRef as any)?.loading),
|
|
43
|
+
onClick: () => (compRef as any)?.confirm?.(),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return { cancelBtnProps, confirmBtnProps };
|
|
47
|
+
},
|
|
48
|
+
render() {
|
|
49
|
+
const { cancelBtnProps, confirmBtnProps } = this;
|
|
50
|
+
|
|
51
|
+
return h(ASpace, {}, () => [
|
|
52
|
+
h(AButton, cancelBtnProps, () => '取消'),
|
|
53
|
+
h(AButton, confirmBtnProps, () => '确定'),
|
|
54
|
+
]);
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const PresetComponent = defineComponent({
|
|
58
|
+
render() {
|
|
59
|
+
return h(ADrawer, _drawerProps, {
|
|
60
|
+
default: () => h(_comp.is, compProps as any),
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
_drawerProps.footer = _drawerProps.footer === undefined ? h(DrawerFooter) : _drawerProps.footer;
|
|
66
|
+
_drawerProps['onUpdate:open'] = (visiable) => {
|
|
67
|
+
_drawerProps.open = visiable;
|
|
68
|
+
};
|
|
69
|
+
(compProps as any).ref = compRef;
|
|
70
|
+
(compProps as any).onClose = close;
|
|
71
|
+
|
|
72
|
+
return { PresetComponent, drawerProps: _drawerProps, open, close };
|
|
73
|
+
}
|
|
@@ -1,71 +1,82 @@
|
|
|
1
|
-
import { computed } from 'vue';
|
|
2
|
-
import type { UseQueryReturnType } from '@tanstack/vue-query';
|
|
3
|
-
import type { Table, TableProps } from 'ant-design-vue';
|
|
4
|
-
import type { ColumnType } from 'ant-design-vue/es/table/interface';
|
|
5
|
-
import type { ComponentProps } from 'vue-component-type-helpers';
|
|
6
|
-
|
|
7
|
-
export function useAntdTable<
|
|
8
|
-
UQRR extends UseQueryReturnType<any, any>,
|
|
9
|
-
QP extends Partial<{ page?: string | number, page_size?: string | number }>,
|
|
10
|
-
>(uqrt: UQRR, queryParams: QP = ({} as any)) {
|
|
11
|
-
type RecordType = GetRecordType<UQRR>;
|
|
12
|
-
type LocalTableProps = TableProps<RecordType>;
|
|
13
|
-
type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
1
|
+
import { computed, reactive } from 'vue';
|
|
2
|
+
import type { UseQueryReturnType } from '@tanstack/vue-query';
|
|
3
|
+
import type { Table, TableProps } from 'ant-design-vue';
|
|
4
|
+
import type { ColumnType } from 'ant-design-vue/es/table/interface';
|
|
5
|
+
import type { ComponentProps } from 'vue-component-type-helpers';
|
|
6
|
+
|
|
7
|
+
export function useAntdTable<
|
|
8
|
+
UQRR extends UseQueryReturnType<any, any>,
|
|
9
|
+
QP extends Partial<{ page?: string | number, page_size?: string | number }>,
|
|
10
|
+
>(uqrt: UQRR, queryParams: QP = ({} as any)) {
|
|
11
|
+
type RecordType = GetRecordType<UQRR>;
|
|
12
|
+
type LocalTableProps = TableProps<RecordType>;
|
|
13
|
+
type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
|
|
14
|
+
type LocalTableRowSelection = NonNullable<LocalTableProps['rowSelection']>;
|
|
15
|
+
|
|
16
|
+
const { data, isFetching, isLoading } = uqrt;
|
|
17
|
+
|
|
18
|
+
const onPaginationChange: ComponentProps<typeof Table>['onChange'] = (pagination) => {
|
|
19
|
+
const page = queryParams.page_size !== pagination.pageSize ? 1 : pagination.current;
|
|
20
|
+
Object.assign(queryParams, { page, page_size: pagination.pageSize ?? 10 });
|
|
21
|
+
};
|
|
22
|
+
const defineColumns = (columnsGetter: () => LocalColumnsType) => computed(columnsGetter);
|
|
23
|
+
const defineRowSelection = (rowSelectionGetter: () => LocalTableRowSelection) => {
|
|
24
|
+
const rowSelection = reactive(rowSelectionGetter());
|
|
25
|
+
|
|
26
|
+
rowSelection.selectedRowKeys ??= [];
|
|
27
|
+
rowSelection.onChange ??= keys => rowSelection.selectedRowKeys = keys;
|
|
28
|
+
|
|
29
|
+
return rowSelection;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const tableProps = computed<LocalTableProps>(() => {
|
|
33
|
+
const { list, pagination } = data.value ?? {};
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
dataSource: list,
|
|
37
|
+
pagination: {
|
|
38
|
+
disabled: isFetching.value,
|
|
39
|
+
current: Number(queryParams.page ?? 1),
|
|
40
|
+
pageSize: Number(queryParams.page_size ?? 10),
|
|
41
|
+
total: pagination?.total ?? 0,
|
|
42
|
+
showTotal: total => `共 ${total} 条`,
|
|
43
|
+
},
|
|
44
|
+
loading: isLoading.value,
|
|
45
|
+
scroll: { x: 'max-content' },
|
|
46
|
+
sticky: true,
|
|
47
|
+
onChange: onPaginationChange as any,
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
const dataIndexs = new Proxy({} as Record<keyof RecordType, string>, {
|
|
51
|
+
get(_, p) {
|
|
52
|
+
return p;
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
const bodyCellType = {} as {
|
|
56
|
+
index: number
|
|
57
|
+
text: any
|
|
58
|
+
value: any
|
|
59
|
+
record: RecordType
|
|
60
|
+
column: ColumnType<RecordType>
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
/** ATable 的预设 Props */
|
|
65
|
+
tableProps,
|
|
66
|
+
/** 【类型辅助】基于接口数据类型推导出的 dataIndex,供 columns 的 dataIndex 使用 */
|
|
67
|
+
dataIndexs,
|
|
68
|
+
/** 【类型辅助】bodyCell 插槽数据的精确类型描述 */
|
|
69
|
+
bodyCellType,
|
|
70
|
+
/** 【类型辅助】用于定义出类型精确的 columns */
|
|
71
|
+
defineColumns,
|
|
72
|
+
/** 【类型辅助】用于定义出类型精确的 rowSelection */
|
|
73
|
+
defineRowSelection,
|
|
74
|
+
onPaginationChange,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type GetRecordType<T> = T extends UseQueryReturnType<infer D, any>
|
|
79
|
+
? D extends Api.PageData
|
|
80
|
+
? NonNullable<D['list']>[0]
|
|
81
|
+
: never
|
|
82
|
+
: never;
|
package/package.json
CHANGED
|
@@ -1,54 +1,55 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@peng_kai/kit",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
5
|
-
"description": "",
|
|
6
|
-
"author": "",
|
|
7
|
-
"license": "ISC",
|
|
8
|
-
"keywords": [],
|
|
9
|
-
"main": "index.js",
|
|
10
|
-
"scripts": {
|
|
11
|
-
"lint": "eslint .",
|
|
12
|
-
"lint:fix": "eslint . --fix"
|
|
13
|
-
},
|
|
14
|
-
"peerDependencies": {
|
|
15
|
-
"@tanstack/vue-query": "4.37.1",
|
|
16
|
-
"@vueuse/core": "10.6.1",
|
|
17
|
-
"ant-design-vue": "4.0.7",
|
|
18
|
-
"axios": "1.6.2",
|
|
19
|
-
"bignumber.js": "9.1.2",
|
|
20
|
-
"dayjs": "1.11.10",
|
|
21
|
-
"lodash-es": "4.17.21",
|
|
22
|
-
"vue": "3.3.8",
|
|
23
|
-
"vue-router": "4.2.5"
|
|
24
|
-
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@babel/generator": "^7.23.5",
|
|
27
|
-
"@babel/parser": "^7.23.5",
|
|
28
|
-
"@babel/traverse": "^7.23.5",
|
|
29
|
-
"@babel/types": "^7.23.5",
|
|
30
|
-
"@tanstack/vue-query": "^4.37.1",
|
|
31
|
-
"@vueuse/core": "^10.6.1",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"vue
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"@
|
|
46
|
-
"@
|
|
47
|
-
"@types/
|
|
48
|
-
"@types/
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@peng_kai/kit",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.6",
|
|
5
|
+
"description": "",
|
|
6
|
+
"author": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint .",
|
|
12
|
+
"lint:fix": "eslint . --fix"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@tanstack/vue-query": "4.37.1",
|
|
16
|
+
"@vueuse/core": "10.6.1",
|
|
17
|
+
"ant-design-vue": "4.0.7",
|
|
18
|
+
"axios": "1.6.2",
|
|
19
|
+
"bignumber.js": "9.1.2",
|
|
20
|
+
"dayjs": "1.11.10",
|
|
21
|
+
"lodash-es": "4.17.21",
|
|
22
|
+
"vue": "3.3.8",
|
|
23
|
+
"vue-router": "4.2.5"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@babel/generator": "^7.23.5",
|
|
27
|
+
"@babel/parser": "^7.23.5",
|
|
28
|
+
"@babel/traverse": "^7.23.5",
|
|
29
|
+
"@babel/types": "^7.23.5",
|
|
30
|
+
"@tanstack/vue-query": "^4.37.1",
|
|
31
|
+
"@vueuse/core": "^10.6.1",
|
|
32
|
+
"a-calc": "^1.3.8",
|
|
33
|
+
"ant-design-vue": "^4.0.7",
|
|
34
|
+
"axios": "^1.6.2",
|
|
35
|
+
"bignumber.js": "^9.1.2",
|
|
36
|
+
"chokidar": "^3.5.3",
|
|
37
|
+
"dayjs": "^1.11.10",
|
|
38
|
+
"fast-glob": "^3.3.2",
|
|
39
|
+
"lodash-es": "^4.17.21",
|
|
40
|
+
"nprogress": "^0.2.0",
|
|
41
|
+
"vue": "^3.3.8",
|
|
42
|
+
"vue-router": "^4.2.5"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@antfu/eslint-config": "^1.2.1",
|
|
46
|
+
"@peng_kai/lint": "^0.1.0",
|
|
47
|
+
"@types/lodash-es": "^4.17.11",
|
|
48
|
+
"@types/node": "18",
|
|
49
|
+
"@types/nprogress": "^0.2.3",
|
|
50
|
+
"@unocss/eslint-plugin": "^0.57.7",
|
|
51
|
+
"type-fest": "^4.8.1",
|
|
52
|
+
"typescript": "^5.2.2",
|
|
53
|
+
"vue-component-type-helpers": "^1.8.22"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/request/helpers.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import type { AxiosInstance } from 'axios';
|
|
2
|
-
|
|
3
|
-
export { isTimeout, getServices, ApiCode, ApiError };
|
|
4
|
-
|
|
5
|
-
enum ApiCode {
|
|
6
|
-
NORMAL = 0, // 正常
|
|
7
|
-
UNKNOWN = -1, // 未知错误
|
|
8
|
-
TIMEOUT = -2, // 请求超时
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function getServices() {
|
|
12
|
-
const serversModule = getServices.modules;
|
|
13
|
-
const servers: Record<string, { server: AxiosInstance } | undefined> = {};
|
|
14
|
-
|
|
15
|
-
for (const [key, module] of Object.entries(serversModule)) {
|
|
16
|
-
const name = key.match(/\/([0-9a-zA-Z]+)\.ts$/)?.[1];
|
|
17
|
-
|
|
18
|
-
if (name)
|
|
19
|
-
servers[`${name}.api`] = module as any;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return servers;
|
|
23
|
-
}
|
|
24
|
-
getServices.modules = {} as any;
|
|
25
|
-
|
|
26
|
-
function isTimeout(error: any) {
|
|
27
|
-
return error?.message?.toLowerCase().includes('timeout');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
class ApiError<TResp = any> extends Error {
|
|
31
|
-
public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
|
|
32
|
-
return !!error?.isApiError;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 将HTTP status和Api code统一到一个code里
|
|
37
|
-
*/
|
|
38
|
-
public code: number;
|
|
39
|
-
public msg: string;
|
|
40
|
-
public data: TResp;
|
|
41
|
-
public isApiError = true;
|
|
42
|
-
|
|
43
|
-
public constructor(info: { code: number, msg: string, data: TResp }) {
|
|
44
|
-
super(info.msg);
|
|
45
|
-
this.code = info.code;
|
|
46
|
-
this.msg = info.msg;
|
|
47
|
-
this.data = info.data;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
export { isTimeout, getServices, ApiCode, ApiError };
|
|
4
|
+
|
|
5
|
+
enum ApiCode {
|
|
6
|
+
NORMAL = 0, // 正常
|
|
7
|
+
UNKNOWN = -1, // 未知错误
|
|
8
|
+
TIMEOUT = -2, // 请求超时
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getServices() {
|
|
12
|
+
const serversModule = getServices.modules;
|
|
13
|
+
const servers: Record<string, { server: AxiosInstance } | undefined> = {};
|
|
14
|
+
|
|
15
|
+
for (const [key, module] of Object.entries(serversModule)) {
|
|
16
|
+
const name = key.match(/\/([0-9a-zA-Z]+)\.ts$/)?.[1];
|
|
17
|
+
|
|
18
|
+
if (name)
|
|
19
|
+
servers[`${name}.api`] = module as any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return servers;
|
|
23
|
+
}
|
|
24
|
+
getServices.modules = {} as any;
|
|
25
|
+
|
|
26
|
+
function isTimeout(error: any) {
|
|
27
|
+
return error?.message?.toLowerCase().includes('timeout');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class ApiError<TResp = any> extends Error {
|
|
31
|
+
public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
|
|
32
|
+
return !!error?.isApiError;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 将HTTP status和Api code统一到一个code里
|
|
37
|
+
*/
|
|
38
|
+
public code: number;
|
|
39
|
+
public msg: string;
|
|
40
|
+
public data: TResp;
|
|
41
|
+
public isApiError = true;
|
|
42
|
+
|
|
43
|
+
public constructor(info: { code: number, msg: string, data: TResp }) {
|
|
44
|
+
super(info.msg);
|
|
45
|
+
this.code = info.code;
|
|
46
|
+
this.msg = info.msg;
|
|
47
|
+
this.data = info.data;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type { AxiosInterceptorManager } from 'axios';
|
|
2
|
-
|
|
3
|
-
export function toLogin(authPath = `${window.location.origin}/auth/login`, code = 15001): Parameters<AxiosInterceptorManager<any>['use']> {
|
|
4
|
-
return [
|
|
5
|
-
undefined,
|
|
6
|
-
(err) => {
|
|
7
|
-
if (err.code !== code)
|
|
8
|
-
throw err;
|
|
9
|
-
|
|
10
|
-
const url = new URL(authPath);
|
|
11
|
-
url.searchParams.set('redirect', window.location.href);
|
|
12
|
-
window.location.href = url.toString();
|
|
13
|
-
},
|
|
14
|
-
];
|
|
15
|
-
}
|
|
1
|
+
import type { AxiosInterceptorManager } from 'axios';
|
|
2
|
+
|
|
3
|
+
export function toLogin(authPath = `${window.location.origin}/auth/login`, code = 15001): Parameters<AxiosInterceptorManager<any>['use']> {
|
|
4
|
+
return [
|
|
5
|
+
undefined,
|
|
6
|
+
(err) => {
|
|
7
|
+
if (err.code !== code)
|
|
8
|
+
throw err;
|
|
9
|
+
|
|
10
|
+
const url = new URL(authPath);
|
|
11
|
+
url.searchParams.set('redirect', window.location.href);
|
|
12
|
+
window.location.href = url.toString();
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
}
|