@peng_kai/kit 0.1.17 → 0.2.0-beta.1

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.
Files changed (84) hide show
  1. package/admin/adminPlugin.ts +47 -0
  2. package/admin/components/filter/src/FilterDrawer.vue +153 -153
  3. package/admin/components/filter/src/FilterParam.vue +76 -78
  4. package/admin/components/filter/src/FilterReset.vue +2 -2
  5. package/admin/components/filter/src/useFilterParams.ts +75 -25
  6. package/admin/components/filter/src/useFilterQuery.ts +32 -16
  7. package/admin/components/rich-text/index.ts +2 -0
  8. package/admin/components/rich-text/src/RichText.vue +342 -0
  9. package/admin/components/rich-text/src/imageUploader.ts +34 -0
  10. package/admin/components/rich-text/src/type.d.ts +7 -0
  11. package/admin/components/scroll-nav/index.ts +1 -1
  12. package/admin/components/scroll-nav/src/ScrollNav.vue +59 -59
  13. package/admin/components/text/index.ts +13 -13
  14. package/admin/components/text/src/Amount.vue +121 -121
  15. package/admin/components/text/src/Datetime.vue +47 -48
  16. package/admin/components/text/src/Duration.vue +26 -26
  17. package/admin/components/text/src/Hash.vue +51 -51
  18. package/admin/components/text/src/createTagGetter.ts +13 -13
  19. package/admin/components/upload/index.ts +2 -0
  20. package/admin/components/upload/src/PictureCardUpload.vue +143 -0
  21. package/admin/components/upload/src/customRequests.ts +31 -0
  22. package/admin/defines/index.ts +1 -1
  23. package/admin/defines/route/helpers.ts +0 -1
  24. package/admin/defines/startup/defineStartup.ts +8 -1
  25. package/admin/defines/startup/index.ts +1 -1
  26. package/admin/defines/startup/{getStartups.ts → runStartup.ts} +16 -7
  27. package/admin/layout/large/Breadcrumb.vue +68 -69
  28. package/admin/layout/large/Content.vue +23 -24
  29. package/admin/layout/large/Menu.vue +68 -69
  30. package/admin/layout/large/PageTab.vue +70 -71
  31. package/admin/permission/index.ts +2 -4
  32. package/admin/permission/routerGuard.ts +41 -43
  33. package/admin/permission/vuePlugin.ts +46 -30
  34. package/admin/route-guards/collapseMenu.ts +3 -3
  35. package/admin/route-guards/index.ts +3 -3
  36. package/admin/route-guards/pageProgress.ts +27 -27
  37. package/admin/route-guards/pageTitle.ts +18 -19
  38. package/admin/{hooks/useMenu.ts → stores/createUseMenuStore.ts} +133 -128
  39. package/admin/{hooks/usePage.ts → stores/createUsePageStore.ts} +145 -141
  40. package/admin/stores/createUsePageTabStore.ts +43 -0
  41. package/admin/{permission/usePermission.ts → stores/createUsePermissionStore.ts} +57 -52
  42. package/admin/stores/index.ts +8 -0
  43. package/admin/styles/classCover.scss +8 -0
  44. package/admin/styles/globalCover.scss +54 -54
  45. package/antd/components/InputNumberRange.vue +59 -59
  46. package/antd/directives/formLabelAlign.ts +36 -36
  47. package/antd/hooks/useAntdDrawer.ts +73 -73
  48. package/antd/hooks/useAntdForm.helpers.ts +92 -8
  49. package/antd/hooks/useAntdForm.ts +55 -63
  50. package/antd/hooks/useAntdTable.ts +127 -115
  51. package/antd/index.ts +1 -1
  52. package/libs/a-calc.ts +1 -0
  53. package/libs/axios.ts +2 -0
  54. package/libs/bignumber.ts +2 -0
  55. package/libs/dayjs.ts +5 -0
  56. package/libs/echarts.ts +5 -0
  57. package/libs/localstorage-slim.ts +2 -0
  58. package/libs/lodash-es.ts +1 -0
  59. package/libs/pinia.ts +1 -0
  60. package/libs/vue-query.ts +1 -0
  61. package/libs/vueuse.ts +3 -0
  62. package/package.json +91 -58
  63. package/request/helpers.ts +68 -49
  64. package/request/interceptors/toLogin.ts +26 -26
  65. package/request/queryClient.ts +34 -21
  66. package/request/type.d.ts +92 -92
  67. package/stylelint.config.cjs +7 -7
  68. package/tsconfig.json +50 -50
  69. package/utils/upload/AwsS3.ts +68 -0
  70. package/utils/upload/fileHandlers.ts +27 -0
  71. package/utils/upload/index.ts +2 -0
  72. package/vite/index.d.ts +1 -0
  73. package/vite/index.mjs +27 -0
  74. package/vue/components/echarts/index.ts +1 -0
  75. package/vue/components/echarts/src/ECharts.vue +48 -0
  76. package/vue/components/index.ts +1 -0
  77. package/vue/components/infinite-query/index.ts +1 -1
  78. package/vue/components/infinite-query/src/InfiniteQuery.vue +199 -205
  79. package/vue/components/infinite-query/src/useCreateTrigger.ts +39 -39
  80. package/vue/components/test/KitTest.vue +9 -0
  81. package/vue/components/test/testStore.ts +11 -0
  82. package/admin/hooks/index.ts +0 -5
  83. package/admin/hooks/usePageTab.ts +0 -35
  84. package/kitDependencies.ts +0 -43
@@ -1,115 +1,127 @@
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;
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
+ const defaultPageSizeOptions = ['10', '20', '50', '100'];
14
+
15
+ export function useAntdTable<
16
+ UQRR extends UseQueryReturnType<any, any>,
17
+ QP extends Partial<{ page?: string | number, page_size?: string | number }>,
18
+ >(uqrt: UQRR, queryParams: QP = ({} as any)) {
19
+ type RecordType = GetRecordType<UQRR>;
20
+ type LocalTableProps = TableProps<RecordType>;
21
+ type LocalColumnsType = NonNullable<LocalTableProps['columns']>;
22
+ type LocalTableRowSelection = NonNullable<LocalTableProps['rowSelection']>;
23
+
24
+ const { data, isFetching, isLoading } = uqrt;
25
+ const filters = ref<Record<string, FilterValue>>();
26
+ const sorter = ref<ISorter>();
27
+ const sorters = ref<ISorter[]>();
28
+
29
+ /* pageSizeOptions 初始化 */
30
+ const pageSizeOptions = [...defaultPageSizeOptions];
31
+ const initPageSize = String(queryParams?.page_size ?? '10');
32
+
33
+ if (!pageSizeOptions.includes(initPageSize)) {
34
+ pageSizeOptions.push(initPageSize);
35
+ pageSizeOptions.sort((p, n) => Number(p) - Number(n));
36
+ }
37
+
38
+ const onChange: ComponentProps<typeof Table>['onChange'] = (_pagination, _filters, _sorter, extra) => {
39
+ if (extra.action === 'paginate') {
40
+ const page = queryParams.page_size !== _pagination.pageSize ? 1 : _pagination.current;
41
+ Object.assign(queryParams, { page, page_size: _pagination.pageSize ?? 10 });
42
+ }
43
+ else if (extra.action === 'filter') {
44
+ filters.value = _filters;
45
+ }
46
+ else if (extra.action === 'sort') {
47
+ if (Array.isArray(_sorter)) {
48
+ sorter.value = pick(_sorter[0], ['field', 'order']) as any;
49
+ sorters.value = _sorter.map(item => pick(item, ['field', 'order'])) as any;
50
+ }
51
+ else {
52
+ sorter.value = pick(_sorter, ['field', 'order']) as any;
53
+ sorters.value = [{ ...sorter.value }];
54
+ }
55
+ }
56
+ };
57
+ const defineColumns = (columnsGetter: () => LocalColumnsType) => computed(columnsGetter);
58
+ const defineRowSelection = (rowSelectionGetter: () => LocalTableRowSelection) => {
59
+ const rowSelection = reactive(rowSelectionGetter());
60
+
61
+ rowSelection.selectedRowKeys ??= [];
62
+ rowSelection.onChange ??= keys => rowSelection.selectedRowKeys = keys;
63
+
64
+ return rowSelection;
65
+ };
66
+
67
+ const tableProps = computed<LocalTableProps>(() => {
68
+ const { list, pagination } = data.value ?? {};
69
+
70
+ return {
71
+ dataSource: list,
72
+ pagination: {
73
+ disabled: isFetching.value,
74
+ current: Number(queryParams.page ?? 1),
75
+ pageSize: Number(queryParams.page_size ?? 10),
76
+ total: pagination?.total ?? 0,
77
+ showSizeChanger: true,
78
+ showQuickJumper: true,
79
+ pageSizeOptions,
80
+ showTotal: total => `共 ${total} 条`,
81
+ },
82
+ loading: isLoading.value,
83
+ scroll: { x: 'max-content' },
84
+ sticky: true,
85
+ onChange: onChange as any,
86
+ };
87
+ });
88
+ const dataIndexs = new Proxy({} as Record<keyof RecordType, string>, {
89
+ get(_, p) {
90
+ return p;
91
+ },
92
+ });
93
+ const bodyCellType = {} as {
94
+ index: number
95
+ text: any
96
+ value: any
97
+ record: RecordType
98
+ column: ColumnType<RecordType>
99
+ };
100
+
101
+ return {
102
+ /** ATable 的预设 Props */
103
+ tableProps,
104
+ /* 过滤的字段 */
105
+ filters,
106
+ /* 排序的字段 */
107
+ sorter,
108
+ /* 多维排序的字段 */
109
+ sorters,
110
+ /** 【类型辅助】基于接口数据类型推导出的 dataIndex,供 columns 的 dataIndex 使用 */
111
+ dataIndexs,
112
+ /** 【类型辅助】bodyCell 插槽数据的精确类型描述 */
113
+ bodyCellType,
114
+ /** 【类型辅助】用于定义出类型精确的 columns */
115
+ defineColumns,
116
+ /** 【类型辅助】用于定义出类型精确的 rowSelection */
117
+ defineRowSelection,
118
+ /** 内置的 ATable onChange 事件,默认在 `tableProps` 中 */
119
+ onChange,
120
+ };
121
+ }
122
+
123
+ type GetRecordType<T> = T extends UseQueryReturnType<infer D, any>
124
+ ? D extends Api.PageData
125
+ ? NonNullable<D['list']>[0]
126
+ : never
127
+ : never;
package/antd/index.ts CHANGED
@@ -5,4 +5,4 @@ export { useAntdForm } from './hooks/useAntdForm';
5
5
  export { useAntdTheme } from './hooks/useAntdTheme';
6
6
  export { createAntdModal } from './hooks/createAntdModal';
7
7
  export { default as InputNumberRange } from './components/InputNumberRange.vue';
8
- export type { TField, RecordToSchemas } from './hooks/useAntdForm.ts';
8
+ export type { SchemaConfig, ItemSchema } from './hooks/useAntdForm.ts';
package/libs/a-calc.ts ADDED
@@ -0,0 +1 @@
1
+ export * from 'a-calc';
package/libs/axios.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default } from 'axios';
2
+ export * from 'axios';
@@ -0,0 +1,2 @@
1
+ export { default } from 'bignumber.js';
2
+ export { BigNumber } from 'bignumber.js';
package/libs/dayjs.ts ADDED
@@ -0,0 +1,5 @@
1
+ import dayjs from 'dayjs';
2
+
3
+ export { isDayjs, unix, locale, extend } from 'dayjs';
4
+ export type { Dayjs, PluginFunc, UnitType, UnitTypeLong, UnitTypeLongPlural, UnitTypeShort, QUnitType, ConfigType, ConfigTypeMap, OpUnitType, OptionType, ManipulateType } from 'dayjs';
5
+ export default dayjs;
@@ -0,0 +1,5 @@
1
+ import echarts from 'echarts';
2
+
3
+ export type * from 'echarts/types/dist/echarts';
4
+
5
+ export default echarts;
@@ -0,0 +1,2 @@
1
+ export { default } from 'localstorage-slim';
2
+ export * from 'localstorage-slim';
@@ -0,0 +1 @@
1
+ export * from 'lodash-es';
package/libs/pinia.ts ADDED
@@ -0,0 +1 @@
1
+ export * from 'pinia';
@@ -0,0 +1 @@
1
+ export * from '@tanstack/vue-query';
package/libs/vueuse.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from '@vueuse/components';
2
+ export * from '@vueuse/core';
3
+ export * from '@vueuse/router';
package/package.json CHANGED
@@ -1,58 +1,91 @@
1
- {
2
- "name": "@peng_kai/kit",
3
- "type": "module",
4
- "version": "0.1.17",
5
- "description": "",
6
- "author": "",
7
- "license": "ISC",
8
- "keywords": [],
9
- "main": "index.js",
10
- "scripts": {
11
- "dev:js": "tsx ./admin/scripts/deploy.ts",
12
- "lint": "eslint .",
13
- "lint:fix": "eslint . --fix"
14
- },
15
- "peerDependencies": {
16
- "@tanstack/vue-query": "4.37.1",
17
- "@vueuse/core": "10.7.2",
18
- "ant-design-vue": "4.1.1",
19
- "axios": "1.6.5",
20
- "bignumber.js": "9.1.2",
21
- "dayjs": "1.11.10",
22
- "lodash-es": "4.17.21",
23
- "vue": "3.4.15",
24
- "vue-router": "4.2.5"
25
- },
26
- "dependencies": {
27
- "@babel/generator": "^7.23.6",
28
- "@babel/parser": "^7.23.6",
29
- "@babel/traverse": "^7.23.7",
30
- "@babel/types": "^7.23.6",
31
- "@tanstack/vue-query": "^4.37.1",
32
- "@vueuse/core": "^10.7.2",
33
- "a-calc": "^1.3.8",
34
- "ant-design-vue": "^4.1.1",
35
- "archiver": "^6.0.1",
36
- "axios": "^1.6.5",
37
- "bignumber.js": "^9.1.2",
38
- "chokidar": "^3.5.3",
39
- "dayjs": "^1.11.10",
40
- "execa": "^8.0.1",
41
- "fast-glob": "^3.3.2",
42
- "lodash-es": "^4.17.21",
43
- "nprogress": "^0.2.0",
44
- "tsx": "^4.7.0",
45
- "vue": "^3.4.15",
46
- "vue-router": "^4.2.5"
47
- },
48
- "devDependencies": {
49
- "@peng_kai/lint": "^0.1.0",
50
- "@types/archiver": "^6.0.2",
51
- "@types/lodash-es": "^4.17.12",
52
- "@types/node": "18",
53
- "@types/nprogress": "^0.2.3",
54
- "type-fest": "^4.9.0",
55
- "typescript": "^5.3.3",
56
- "vue-component-type-helpers": "^1.8.27"
57
- }
58
- }
1
+ {
2
+ "name": "@peng_kai/kit",
3
+ "type": "module",
4
+ "version": "0.2.0-beta.1",
5
+ "description": "",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "keywords": [],
9
+ "main": "index.js",
10
+ "scripts": {
11
+ "dev:js": "tsx ./admin/scripts/deploy.ts",
12
+ "lint": "eslint .",
13
+ "lint:fix": "eslint . --fix"
14
+ },
15
+ "peerDependencies": {
16
+ "ant-design-vue": "4.1.2",
17
+ "vue": "3.4.19",
18
+ "vue-router": "4.2.5"
19
+ },
20
+ "dependencies": {
21
+ "@aws-sdk/client-s3": "^3.515.0",
22
+ "@aws-sdk/lib-storage": "^3.515.0",
23
+ "@babel/generator": "^7.23.6",
24
+ "@babel/parser": "^7.23.9",
25
+ "@babel/traverse": "^7.23.9",
26
+ "@babel/types": "^7.23.9",
27
+ "@ckeditor/ckeditor5-adapter-ckfinder": "^41.1.0",
28
+ "@ckeditor/ckeditor5-alignment": "^41.1.0",
29
+ "@ckeditor/ckeditor5-autoformat": "^41.1.0",
30
+ "@ckeditor/ckeditor5-basic-styles": "^41.1.0",
31
+ "@ckeditor/ckeditor5-block-quote": "^41.1.0",
32
+ "@ckeditor/ckeditor5-build-classic": "^41.1.0",
33
+ "@ckeditor/ckeditor5-code-block": "^41.1.0",
34
+ "@ckeditor/ckeditor5-document-outline": "^41.1.0",
35
+ "@ckeditor/ckeditor5-editor-classic": "^41.1.0",
36
+ "@ckeditor/ckeditor5-essentials": "^41.1.0",
37
+ "@ckeditor/ckeditor5-font": "^41.1.0",
38
+ "@ckeditor/ckeditor5-heading": "^41.1.0",
39
+ "@ckeditor/ckeditor5-highlight": "^41.1.0",
40
+ "@ckeditor/ckeditor5-horizontal-line": "^41.1.0",
41
+ "@ckeditor/ckeditor5-html-embed": "^41.1.0",
42
+ "@ckeditor/ckeditor5-html-support": "^41.1.0",
43
+ "@ckeditor/ckeditor5-image": "^41.1.0",
44
+ "@ckeditor/ckeditor5-import-word": "^41.1.0",
45
+ "@ckeditor/ckeditor5-indent": "^41.1.0",
46
+ "@ckeditor/ckeditor5-link": "^41.1.0",
47
+ "@ckeditor/ckeditor5-list": "^41.1.0",
48
+ "@ckeditor/ckeditor5-media-embed": "^41.1.0",
49
+ "@ckeditor/ckeditor5-paragraph": "^41.1.0",
50
+ "@ckeditor/ckeditor5-remove-format": "^41.1.0",
51
+ "@ckeditor/ckeditor5-show-blocks": "^41.1.0",
52
+ "@ckeditor/ckeditor5-source-editing": "^41.1.0",
53
+ "@ckeditor/ckeditor5-table": "^41.1.0",
54
+ "@ckeditor/ckeditor5-theme-lark": "^41.1.0",
55
+ "@ckeditor/ckeditor5-typing": "^41.1.0",
56
+ "@ckeditor/ckeditor5-upload": "^41.1.0",
57
+ "@ckeditor/ckeditor5-vue": "^5.1.0",
58
+ "@ckeditor/ckeditor5-word-count": "^41.1.0",
59
+ "@tanstack/vue-query": "^5.21.4",
60
+ "@vueuse/components": "^10.7.2",
61
+ "@vueuse/core": "^10.7.2",
62
+ "@vueuse/router": "^10.7.2",
63
+ "a-calc": "^1.3.8",
64
+ "ant-design-vue": "^4.1.2",
65
+ "archiver": "^6.0.1",
66
+ "axios": "^1.6.7",
67
+ "bignumber.js": "^9.1.2",
68
+ "chokidar": "^3.6.0",
69
+ "dayjs": "^1.11.10",
70
+ "echarts": "^5.4.3",
71
+ "execa": "^8.0.1",
72
+ "fast-glob": "^3.3.2",
73
+ "localstorage-slim": "^2.7.0",
74
+ "lodash-es": "^4.17.21",
75
+ "nprogress": "^0.2.0",
76
+ "pinia": "^2.1.7",
77
+ "tsx": "^4.7.1",
78
+ "vue": "^3.4.19",
79
+ "vue-router": "^4.2.5"
80
+ },
81
+ "devDependencies": {
82
+ "@peng_kai/lint": "^0.1.0",
83
+ "@types/archiver": "^6.0.2",
84
+ "@types/lodash-es": "^4.17.12",
85
+ "@types/node": "18.19.15",
86
+ "@types/nprogress": "^0.2.3",
87
+ "type-fest": "^4.10.2",
88
+ "typescript": "^5.3.3",
89
+ "vue-component-type-helpers": "^1.8.27"
90
+ }
91
+ }
@@ -1,49 +1,68 @@
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, getBaseUrlByEnv, 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
+ function getBaseUrlByEnv(service: string, env: Record<string, string>) {
31
+ let serviceName = '';
32
+
33
+ if (/^https?:\/\//.test(service)) {
34
+ const fileName = service.split('/').pop();
35
+ serviceName = fileName?.split('.').shift() ?? '';
36
+ }
37
+ else {
38
+ serviceName = service;
39
+ }
40
+
41
+ if (!serviceName)
42
+ return '';
43
+
44
+ const [prefix, target] = env[`VITE_HTTP_PREFIX_${serviceName}`]?.split(' => ') ?? ['', ''];
45
+
46
+ return (prefix && target) ? prefix : '';
47
+ }
48
+
49
+ class ApiError<TResp = any> extends Error {
50
+ public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
51
+ return !!error?.isApiError;
52
+ }
53
+
54
+ /**
55
+ * 将HTTP status和Api code统一到一个code里
56
+ */
57
+ public code: number;
58
+ public msg: string;
59
+ public data: TResp;
60
+ public isApiError = true;
61
+
62
+ public constructor(info: { code: number, msg: string, data: TResp }) {
63
+ super(info.msg);
64
+ this.code = info.code;
65
+ this.msg = info.msg;
66
+ this.data = info.data;
67
+ }
68
+ }
@@ -1,26 +1,26 @@
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
- }
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
+ }