@peng_kai/kit 0.1.10 → 0.1.12
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 +78 -78
- 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 +124 -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/globalCover.scss +54 -54
- package/admin/styles/index.scss +4 -0
- package/antd/components/InputNumberRange.vue +59 -53
- package/antd/directives/formLabelAlign.ts +36 -36
- package/antd/hooks/useAntdDrawer.ts +73 -73
- package/antd/hooks/useAntdModal.ts +13 -2
- package/antd/hooks/useAntdTable.ts +115 -82
- package/package.json +55 -55
- package/request/helpers.ts +49 -49
- package/request/interceptors/formatPaging.ts +3 -4
- package/request/interceptors/popupMessage.ts +5 -4
- package/request/interceptors/toLogin.ts +26 -15
- package/request/queryClient.ts +32 -8
- package/request/request.ts +0 -1
- package/request/type.d.ts +92 -92
- package/stylelint.config.cjs +7 -7
- package/tsconfig.json +50 -50
- package/utils/index.ts +1 -1
- 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
|
@@ -1,82 +1,115 @@
|
|
|
1
|
-
import { computed, reactive } from 'vue';
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
1
|
+
import { computed, reactive, ref } from 'vue';
|
|
2
|
+
import { pick } from 'lodash-es';
|
|
3
|
+
import type { UseQueryReturnType } from '@tanstack/vue-query';
|
|
4
|
+
import type { Table, TableProps } from 'ant-design-vue';
|
|
5
|
+
import type { ColumnType, FilterValue } from 'ant-design-vue/es/table/interface';
|
|
6
|
+
import type { ComponentProps } from 'vue-component-type-helpers';
|
|
7
|
+
|
|
8
|
+
interface ISorter {
|
|
9
|
+
field?: string
|
|
10
|
+
order?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function useAntdTable<
|
|
14
|
+
UQRR extends UseQueryReturnType<any, any>,
|
|
15
|
+
QP extends Partial<{ page?: string | number, page_size?: string | number }>,
|
|
16
|
+
>(uqrt: UQRR, queryParams: QP = ({} as any)) {
|
|
17
|
+
type RecordType = GetRecordType<UQRR>;
|
|
18
|
+
type LocalTableProps = TableProps<RecordType>;
|
|
19
|
+
type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
|
|
20
|
+
type LocalTableRowSelection = NonNullable<LocalTableProps['rowSelection']>;
|
|
21
|
+
|
|
22
|
+
const { data, isFetching, isLoading } = uqrt;
|
|
23
|
+
const filters = ref<Record<string, FilterValue>>();
|
|
24
|
+
const sorter = ref<ISorter>();
|
|
25
|
+
const sorters = ref<ISorter[]>();
|
|
26
|
+
|
|
27
|
+
const onChange: ComponentProps<typeof Table>['onChange'] = (_pagination, _filters, _sorter, extra) => {
|
|
28
|
+
if (extra.action === 'paginate') {
|
|
29
|
+
const page = queryParams.page_size !== _pagination.pageSize ? 1 : _pagination.current;
|
|
30
|
+
Object.assign(queryParams, { page, page_size: _pagination.pageSize ?? 10 });
|
|
31
|
+
}
|
|
32
|
+
else if (extra.action === 'filter') {
|
|
33
|
+
filters.value = _filters;
|
|
34
|
+
}
|
|
35
|
+
else if (extra.action === 'sort') {
|
|
36
|
+
if (Array.isArray(_sorter)) {
|
|
37
|
+
sorter.value = pick(_sorter[0], ['field', 'order']) as any;
|
|
38
|
+
sorters.value = _sorter.map(item => pick(item, ['field', 'order'])) as any;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
sorter.value = pick(_sorter, ['field', 'order']) as any;
|
|
42
|
+
sorters.value = [{ ...sorter.value }];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const defineColumns = (columnsGetter: () => LocalColumnsType) => computed(columnsGetter);
|
|
47
|
+
const defineRowSelection = (rowSelectionGetter: () => LocalTableRowSelection) => {
|
|
48
|
+
const rowSelection = reactive(rowSelectionGetter());
|
|
49
|
+
|
|
50
|
+
rowSelection.selectedRowKeys ??= [];
|
|
51
|
+
rowSelection.onChange ??= keys => rowSelection.selectedRowKeys = keys;
|
|
52
|
+
|
|
53
|
+
return rowSelection;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const tableProps = computed<LocalTableProps>(() => {
|
|
57
|
+
const { list, pagination } = data.value ?? {};
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
dataSource: list,
|
|
61
|
+
pagination: {
|
|
62
|
+
disabled: isFetching.value,
|
|
63
|
+
current: Number(queryParams.page ?? 1),
|
|
64
|
+
pageSize: Number(queryParams.page_size ?? 10),
|
|
65
|
+
total: pagination?.total ?? 0,
|
|
66
|
+
showSizeChanger: true,
|
|
67
|
+
showQuickJumper: true,
|
|
68
|
+
showTotal: total => `共 ${total} 条`,
|
|
69
|
+
},
|
|
70
|
+
loading: isLoading.value,
|
|
71
|
+
scroll: { x: 'max-content' },
|
|
72
|
+
sticky: true,
|
|
73
|
+
onChange: onChange as any,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const dataIndexs = new Proxy({} as Record<keyof RecordType, string>, {
|
|
77
|
+
get(_, p) {
|
|
78
|
+
return p;
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
const bodyCellType = {} as {
|
|
82
|
+
index: number
|
|
83
|
+
text: any
|
|
84
|
+
value: any
|
|
85
|
+
record: RecordType
|
|
86
|
+
column: ColumnType<RecordType>
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
/** ATable 的预设 Props */
|
|
91
|
+
tableProps,
|
|
92
|
+
/* 过滤的字段 */
|
|
93
|
+
filters,
|
|
94
|
+
/* 排序的字段 */
|
|
95
|
+
sorter,
|
|
96
|
+
/* 多维排序的字段 */
|
|
97
|
+
sorters,
|
|
98
|
+
/** 【类型辅助】基于接口数据类型推导出的 dataIndex,供 columns 的 dataIndex 使用 */
|
|
99
|
+
dataIndexs,
|
|
100
|
+
/** 【类型辅助】bodyCell 插槽数据的精确类型描述 */
|
|
101
|
+
bodyCellType,
|
|
102
|
+
/** 【类型辅助】用于定义出类型精确的 columns */
|
|
103
|
+
defineColumns,
|
|
104
|
+
/** 【类型辅助】用于定义出类型精确的 rowSelection */
|
|
105
|
+
defineRowSelection,
|
|
106
|
+
/** 内置的 ATable onChange 事件,默认在 `tableProps` 中 */
|
|
107
|
+
onChange,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type GetRecordType<T> = T extends UseQueryReturnType<infer D, any>
|
|
112
|
+
? D extends Api.PageData
|
|
113
|
+
? NonNullable<D['list']>[0]
|
|
114
|
+
: never
|
|
115
|
+
: never;
|
package/package.json
CHANGED
|
@@ -1,55 +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.
|
|
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.
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@peng_kai/kit",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.12",
|
|
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.8",
|
|
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.8",
|
|
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
|
+
}
|
|
@@ -12,10 +12,9 @@ export function formatPaging(): Parameters<AxiosInterceptorManager<any>['use']>
|
|
|
12
12
|
return resp;
|
|
13
13
|
|
|
14
14
|
if (result.pagination) {
|
|
15
|
-
result.data
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
15
|
+
const originData = result.data;
|
|
16
|
+
result.data = originData.list ? { ...originData } : { list: originData };
|
|
17
|
+
result.data.pagination = result.pagination;
|
|
19
18
|
|
|
20
19
|
Reflect.deleteProperty(result, 'pagination');
|
|
21
20
|
}
|
|
@@ -19,7 +19,8 @@ export function popupMessage(popup: (type: 'error' | 'success', message: string)
|
|
|
19
19
|
if (typeof successMessageConfig !== 'string')
|
|
20
20
|
successMsg = resp?.data?.msg;
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
if (typeof successMsg === 'string' && successMsg !== '')
|
|
23
|
+
popup('success', successMsg);
|
|
23
24
|
|
|
24
25
|
return resp;
|
|
25
26
|
},
|
|
@@ -32,7 +33,7 @@ export function popupMessage(popup: (type: 'error' | 'success', message: string)
|
|
|
32
33
|
let errorMsg: boolean | string = errorMessageConfig;
|
|
33
34
|
|
|
34
35
|
if (axios.isAxiosError(error)) {
|
|
35
|
-
|
|
36
|
+
// 请求超时
|
|
36
37
|
if (isTimeout(error))
|
|
37
38
|
errorMsg = 'Request Timeout';
|
|
38
39
|
else if (typeof errorMessageConfig !== 'string')
|
|
@@ -42,10 +43,10 @@ export function popupMessage(popup: (type: 'error' | 'success', message: string)
|
|
|
42
43
|
errorMsg = error.message;
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
if (typeof errorMsg === 'string')
|
|
46
|
+
if (typeof errorMsg === 'string' && errorMsg !== '')
|
|
46
47
|
popup('error', errorMsg);
|
|
47
48
|
|
|
48
49
|
throw error;
|
|
49
50
|
},
|
|
50
51
|
];
|
|
51
|
-
}
|
|
52
|
+
}
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
import type { AxiosInterceptorManager } from 'axios';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
window.location.href
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import type { AxiosInterceptorManager } from 'axios';
|
|
2
|
+
|
|
3
|
+
const REDIRECT_KEY = 'redirect';
|
|
4
|
+
|
|
5
|
+
export function toLogin(authPath = `${window.location.origin}/auth/login`, code = 15001): Parameters<AxiosInterceptorManager<any>['use']> {
|
|
6
|
+
return [
|
|
7
|
+
undefined,
|
|
8
|
+
(err) => {
|
|
9
|
+
if (err.code !== code)
|
|
10
|
+
throw err;
|
|
11
|
+
|
|
12
|
+
const currentURL = new URL(window.location.href);
|
|
13
|
+
const targetURL = new URL(authPath);
|
|
14
|
+
|
|
15
|
+
if (currentURL.searchParams.has(REDIRECT_KEY))
|
|
16
|
+
targetURL.searchParams.set(REDIRECT_KEY, currentURL.searchParams.get(REDIRECT_KEY)!);
|
|
17
|
+
else
|
|
18
|
+
targetURL.searchParams.set(REDIRECT_KEY, window.location.href);
|
|
19
|
+
|
|
20
|
+
if (currentURL.toString() === targetURL.toString())
|
|
21
|
+
return;
|
|
22
|
+
|
|
23
|
+
window.location.href = targetURL.toString();
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
}
|
package/request/queryClient.ts
CHANGED
|
@@ -1,24 +1,48 @@
|
|
|
1
|
-
import type { QueryClientConfig } from '@tanstack/vue-query';
|
|
1
|
+
import type { QueryClientConfig, QueryObserverOptions } from '@tanstack/vue-query';
|
|
2
|
+
import { ApiCode } from './helpers';
|
|
2
3
|
|
|
3
4
|
/** 不需要重试的 api code */
|
|
4
5
|
export const ignoreRetryCodes = [
|
|
6
|
+
ApiCode.UNKNOWN,
|
|
5
7
|
11000, // 操作失败
|
|
6
8
|
11001, // 操作过于频繁
|
|
7
9
|
11002, // 权限不足
|
|
8
10
|
11003, // 缺少必要参数
|
|
9
11
|
15001, // 未登录
|
|
12
|
+
15003, // 用户不存在
|
|
13
|
+
/^3{2}$/, // HTTP status 3xx
|
|
14
|
+
/^4{2}$/, // HTTP status 4xx
|
|
15
|
+
/^5{2}$/, // HTTP status 5xx
|
|
10
16
|
];
|
|
11
17
|
|
|
18
|
+
/**
|
|
19
|
+
* 构建用于查询的重试函数。
|
|
20
|
+
* @param ignoreList 一个数组,包含要在重试期间忽略的错误代码的数字、字符串或正则表达式。
|
|
21
|
+
* @param additionPresetIgnore 一个布尔值,指示是否包括额外的预设忽略代码。默认为 true。
|
|
22
|
+
* @returns 根据失败计数和错误代码确定是否重试的重试函数。
|
|
23
|
+
*/
|
|
24
|
+
export function buildRetryFn(ignoreList: Array<number | string | RegExp>, additionPresetIgnore = true): NonNullable<QueryObserverOptions['retry']> {
|
|
25
|
+
const _ignoreList = [...ignoreList, ...(additionPresetIgnore ? ignoreRetryCodes : [])];
|
|
26
|
+
return (failureCount, error: any) => {
|
|
27
|
+
if (error?.code === undefined)
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
const ignored = _ignoreList.some((code) => {
|
|
31
|
+
if (code instanceof RegExp)
|
|
32
|
+
return code.test(String(error?.code));
|
|
33
|
+
|
|
34
|
+
return code === error?.code;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return ignored ? false : failureCount < 2;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
12
41
|
export const presetOptions: QueryClientConfig = {
|
|
13
42
|
defaultOptions: {
|
|
14
43
|
queries: {
|
|
15
44
|
refetchOnWindowFocus: false,
|
|
16
|
-
retry
|
|
17
|
-
if (ignoreRetryCodes.includes(error?.code))
|
|
18
|
-
return false;
|
|
19
|
-
|
|
20
|
-
return failureCount < 3;
|
|
21
|
-
},
|
|
45
|
+
retry: buildRetryFn(ignoreRetryCodes),
|
|
22
46
|
getNextPageParam: (lastPage: any) => {
|
|
23
47
|
const { pagination } = lastPage ?? {};
|
|
24
48
|
|
|
@@ -39,4 +63,4 @@ export const presetOptions: QueryClientConfig = {
|
|
|
39
63
|
},
|
|
40
64
|
},
|
|
41
65
|
},
|
|
42
|
-
};
|
|
66
|
+
};
|