@peng_kai/kit 0.1.16 → 0.2.0-beta.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.
Files changed (67) hide show
  1. package/admin/adminPlugin.ts +47 -0
  2. package/admin/components/filter/src/FilterParam.vue +76 -78
  3. package/admin/components/filter/src/FilterReset.vue +2 -2
  4. package/admin/components/filter/src/useFilterParams.ts +75 -25
  5. package/admin/components/filter/src/useFilterQuery.ts +32 -16
  6. package/admin/components/rich-text/index.ts +2 -0
  7. package/admin/components/rich-text/src/RichText.vue +342 -0
  8. package/admin/components/rich-text/src/imageUploader.ts +34 -0
  9. package/admin/components/rich-text/src/type.d.ts +7 -0
  10. package/admin/components/text/src/Datetime.vue +47 -48
  11. package/admin/components/upload/index.ts +2 -0
  12. package/admin/components/upload/src/PictureCardUpload.vue +143 -0
  13. package/admin/components/upload/src/customRequests.ts +31 -0
  14. package/admin/defines/index.ts +1 -1
  15. package/admin/defines/route/helpers.ts +0 -1
  16. package/admin/defines/startup/defineStartup.ts +8 -1
  17. package/admin/defines/startup/index.ts +1 -1
  18. package/admin/defines/startup/{getStartups.ts → runStartup.ts} +16 -7
  19. package/admin/layout/large/Breadcrumb.vue +68 -69
  20. package/admin/layout/large/Content.vue +23 -24
  21. package/admin/layout/large/Menu.vue +68 -69
  22. package/admin/layout/large/PageTab.vue +70 -71
  23. package/admin/permission/index.ts +2 -4
  24. package/admin/permission/routerGuard.ts +41 -43
  25. package/admin/permission/vuePlugin.ts +46 -30
  26. package/admin/route-guards/collapseMenu.ts +3 -3
  27. package/admin/route-guards/pageTitle.ts +18 -19
  28. package/admin/scripts/deploy.ts +67 -0
  29. package/admin/{hooks/useMenu.ts → stores/createUseMenuStore.ts} +133 -128
  30. package/admin/{hooks/usePage.ts → stores/createUsePageStore.ts} +145 -141
  31. package/admin/stores/createUsePageTabStore.ts +43 -0
  32. package/admin/{permission/usePermission.ts → stores/createUsePermissionStore.ts} +57 -52
  33. package/admin/stores/index.ts +8 -0
  34. package/admin/styles/classCover.scss +8 -0
  35. package/antd/hooks/useAntdForm.helpers.ts +92 -8
  36. package/antd/hooks/useAntdForm.ts +55 -63
  37. package/antd/hooks/useAntdTable.ts +12 -0
  38. package/antd/index.ts +1 -1
  39. package/libs/a-calc.ts +1 -0
  40. package/libs/axios.ts +2 -0
  41. package/libs/bignumber.ts +2 -0
  42. package/libs/dayjs.ts +5 -0
  43. package/libs/echarts.ts +5 -0
  44. package/libs/localstorage-slim.ts +2 -0
  45. package/libs/lodash-es.ts +1 -0
  46. package/libs/pinia.ts +1 -0
  47. package/libs/vue-query.ts +1 -0
  48. package/libs/vueuse.ts +3 -0
  49. package/package.json +97 -53
  50. package/request/helpers.ts +20 -1
  51. package/request/interceptors/checkCode.ts +3 -3
  52. package/request/interceptors/returnResultType.ts +2 -2
  53. package/request/queryClient.ts +34 -21
  54. package/utils/upload/AwsS3.ts +76 -0
  55. package/utils/upload/fileHandlers.ts +27 -0
  56. package/utils/upload/index.ts +2 -0
  57. package/vite/index.d.ts +1 -0
  58. package/vite/index.mjs +27 -0
  59. package/vue/components/echarts/index.ts +1 -0
  60. package/vue/components/echarts/src/ECharts.vue +48 -0
  61. package/vue/components/index.ts +1 -0
  62. package/vue/components/infinite-query/src/InfiniteQuery.vue +2 -8
  63. package/vue/components/test/KitTest.vue +9 -0
  64. package/vue/components/test/testStore.ts +11 -0
  65. package/admin/hooks/index.ts +0 -5
  66. package/admin/hooks/usePageTab.ts +0 -35
  67. package/kitDependencies.ts +0 -43
@@ -1,28 +1,45 @@
1
- import { reactiveComputed } from '@vueuse/core';
1
+ /**
2
+ * TODO:
3
+ * 1. 分组表单排序
4
+ */
5
+
2
6
  import type { FormInstance, FormItemProps, RuleObject } from 'ant-design-vue/es/form';
3
7
  import type { FormProps } from 'ant-design-vue';
4
- import { computed, ref } from 'vue';
5
- import type { ComputedRef, UnwrapRef } from 'vue';
6
- import { GROUP_SEP, buildGroupFieldName, getGroupIndex } from './useAntdForm.helpers';
8
+ import { type MaybeRefOrGetter, computed, reactive, ref, toRef, watch, watchEffect } from 'vue';
9
+ import { mapValues } from 'lodash-es';
10
+ import { GROUP_SEP, buildGroupField, getGroupIndex } from './useAntdForm.helpers';
7
11
 
8
12
  export { useAntdForm };
9
- export type { TField, RecordToSchemas };
13
+ export type { SchemaConfig, ItemSchema };
14
+
15
+ type SchemaConfig<T> = {
16
+ [P in keyof T]: {
17
+ value: MaybeRefOrGetter<T[P]>
18
+ show?: (state: T) => boolean
19
+ rules?: RuleObject[] | ((state: T) => Array<RuleObject | undefined> | undefined)
20
+ }
21
+ };
22
+ type ItemSchema = SchemaConfig<any>[string];
23
+
24
+ interface Options<S, TS> {
25
+ transform?: (state: S) => TS
26
+ }
27
+
28
+ function useAntdForm<S extends Record<string, any>, TS = S>(schemas: MaybeRefOrGetter<SchemaConfig<S>>, options?: Options<S, TS>) {
29
+ const schemasR = toRef(schemas);
10
30
 
11
- function useAntdForm<
12
- F extends Record<string, TField>,
13
- S extends GetFormState<F>,
14
- STF = S,
15
- >(
16
- schemas: F,
17
- _options?: {
18
- transform?: (state: S) => STF
19
- },
20
- ) {
31
+ const state = ref({} as S);
32
+ const stateTF = computed<TS>(() => options?.transform?.(<any>state.value) ?? <any>state.value);
33
+ const show = computed(() => mapValues(state.value, (_, k): boolean => schemasR.value[k].show?.(state.value) ?? true));
34
+ const props = computed<FormProps>(() => ({ model: state.value, ref: (c: any) => (formRef.value = c) }));
35
+ const itemProps = computed(() => mapValues(state.value, (_, k): FormItemProps => {
36
+ const r = schemasR.value[k].rules;
37
+ const rules = typeof r === 'function' ? r(state.value)?.filter((i: any) => !!i) : r;
38
+
39
+ return { name: k, rules };
40
+ }));
21
41
  const formRef = ref<FormInstance>();
22
42
  const $form = {
23
- get clearValidate() {
24
- return formRef.value?.clearValidate;
25
- },
26
43
  get getFieldsValue() {
27
44
  return formRef.value?.getFieldsValue;
28
45
  },
@@ -38,64 +55,39 @@ function useAntdForm<
38
55
  get validateFields() {
39
56
  return formRef.value?.validateFields;
40
57
  },
41
- setField(name: string, schema: TField) {
42
- (schemas as any)[name] = schema;
58
+ get clearValidate() {
59
+ return formRef.value?.clearValidate;
43
60
  },
44
- addGroup(groupName: string, groupSchemas: Record<string, TField>) {
45
- const i = getGroupIndex(groupName, schemas) + 1;
61
+ addGroup(groupName: string, groupSchemas: Record<string, ItemSchema>) {
62
+ const i = getGroupIndex(groupName, schemasR.value) + 1;
46
63
 
47
64
  Object.keys(groupSchemas).forEach((key) => {
48
- const name = buildGroupFieldName(groupName, i, key);
49
- (schemas as any)[name] = groupSchemas[key];
65
+ const name = buildGroupField(groupName, i, key);
66
+ schemasR.value[name] = groupSchemas[key];
50
67
  });
51
68
  },
52
69
  removeGroup(groupName: string, index?: number) {
53
- const i = index ?? getGroupIndex(groupName, schemas);
70
+ const i = index ?? getGroupIndex(groupName, schemasR.value);
54
71
 
55
- Object.keys(schemas).forEach((key) => {
72
+ Object.keys(schemasR.value).forEach((key) => {
56
73
  if (key.startsWith(groupName + GROUP_SEP + i))
57
- Reflect.deleteProperty(schemas, key);
74
+ Reflect.deleteProperty(schemasR.value, key);
58
75
  });
59
76
  },
60
77
  };
61
- const state = reactiveComputed(() => {
62
- return Object.fromEntries(Object.entries(schemas).map(([k, v]) => [k, v.value])) as S;
63
- });
64
- const props = reactiveComputed<FormProps>(() => {
65
- return {
66
- model: state,
67
- ref: (c: any) => (formRef.value = c),
68
- };
69
- });
70
- const itemProps = reactiveComputed(() => {
71
- const propsTuple = Object.entries(schemas).map(([key, value]): [string, FormItemProps] => [
72
- key,
73
- { name: key, rules: value.rules },
74
- ]);
75
- const props = Object.fromEntries(propsTuple) as Record<keyof S, FormItemProps>;
76
78
 
77
- return props;
78
- });
79
- const stateTF = _options?.transform ? computed(() => _options.transform!(state)) : computed(() => state);
79
+ watch(itemProps, () => $form.clearValidate?.(), { flush: 'post', deep: true });
80
80
 
81
- return {
82
- $form,
83
- state,
84
- stateTF: stateTF as ComputedRef<STF>,
85
- props,
86
- itemProps,
87
- };
88
- }
81
+ watchEffect(() => {
82
+ const _state: any = {};
89
83
 
90
- interface TField<V = any> {
91
- value: V
92
- rules?: RuleObject[]
93
- }
84
+ for (const k in schemasR.value) {
85
+ const item = schemasR.value[k];
86
+ _state[k] = (k in state.value) ? toRef(state.value, k) : toRef(item, 'value');
87
+ }
94
88
 
95
- type RecordToSchemas<T extends Record<string, unknown>> = {
96
- [K in keyof T]: TField<T[K]>
97
- };
89
+ state.value = _state;
90
+ });
98
91
 
99
- type GetFormState<C extends Record<string, TField>> = {
100
- [K in keyof C]: UnwrapRef<C[K]['value']>;
101
- };
92
+ return reactive({ state, stateTF, show, props, itemProps, $form });
93
+ }
@@ -10,6 +10,8 @@ interface ISorter {
10
10
  order?: string
11
11
  }
12
12
 
13
+ const defaultPageSizeOptions = ['10', '20', '50', '100'];
14
+
13
15
  export function useAntdTable<
14
16
  UQRR extends UseQueryReturnType<any, any>,
15
17
  QP extends Partial<{ page?: string | number, page_size?: string | number }>,
@@ -24,6 +26,15 @@ export function useAntdTable<
24
26
  const sorter = ref<ISorter>();
25
27
  const sorters = ref<ISorter[]>();
26
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
+
27
38
  const onChange: ComponentProps<typeof Table>['onChange'] = (_pagination, _filters, _sorter, extra) => {
28
39
  if (extra.action === 'paginate') {
29
40
  const page = queryParams.page_size !== _pagination.pageSize ? 1 : _pagination.current;
@@ -65,6 +76,7 @@ export function useAntdTable<
65
76
  total: pagination?.total ?? 0,
66
77
  showSizeChanger: true,
67
78
  showQuickJumper: true,
79
+ pageSizeOptions,
68
80
  showTotal: total => `共 ${total} 条`,
69
81
  },
70
82
  loading: isLoading.value,
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,53 +1,97 @@
1
- {
2
- "name": "@peng_kai/kit",
3
- "type": "module",
4
- "version": "0.1.16",
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.7.2",
17
- "ant-design-vue": "4.1.1",
18
- "axios": "1.6.5",
19
- "bignumber.js": "9.1.2",
20
- "dayjs": "1.11.10",
21
- "lodash-es": "4.17.21",
22
- "vue": "3.4.15",
23
- "vue-router": "4.2.5"
24
- },
25
- "dependencies": {
26
- "@babel/generator": "^7.23.6",
27
- "@babel/parser": "^7.23.6",
28
- "@babel/traverse": "^7.23.7",
29
- "@babel/types": "^7.23.6",
30
- "@tanstack/vue-query": "^4.37.1",
31
- "@vueuse/core": "^10.7.2",
32
- "a-calc": "^1.3.8",
33
- "ant-design-vue": "^4.1.1",
34
- "axios": "^1.6.5",
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.4.15",
42
- "vue-router": "^4.2.5"
43
- },
44
- "devDependencies": {
45
- "@peng_kai/lint": "^0.1.0",
46
- "@types/lodash-es": "^4.17.12",
47
- "@types/node": "18",
48
- "@types/nprogress": "^0.2.3",
49
- "type-fest": "^4.9.0",
50
- "typescript": "^5.3.3",
51
- "vue-component-type-helpers": "^1.8.27"
52
- }
53
- }
1
+ {
2
+ "name": "@peng_kai/kit",
3
+ "type": "module",
4
+ "version": "0.2.0-beta.0",
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": "5.20.1",
17
+ "@vueuse/core": "10.7.2",
18
+ "ant-design-vue": "4.1.2",
19
+ "axios": "1.6.7",
20
+ "bignumber.js": "9.1.2",
21
+ "dayjs": "1.11.10",
22
+ "lodash-es": "4.17.21",
23
+ "vue": "3.4.18",
24
+ "vue-router": "4.2.5"
25
+ },
26
+ "dependencies": {
27
+ "@aws-sdk/client-s3": "^3.511.0",
28
+ "@aws-sdk/lib-storage": "^3.511.0",
29
+ "@babel/generator": "^7.23.6",
30
+ "@babel/parser": "^7.23.9",
31
+ "@babel/traverse": "^7.23.9",
32
+ "@babel/types": "^7.23.9",
33
+ "@ckeditor/ckeditor5-adapter-ckfinder": "^41.0.0",
34
+ "@ckeditor/ckeditor5-alignment": "^41.0.0",
35
+ "@ckeditor/ckeditor5-autoformat": "^41.0.0",
36
+ "@ckeditor/ckeditor5-basic-styles": "^41.0.0",
37
+ "@ckeditor/ckeditor5-block-quote": "^41.0.0",
38
+ "@ckeditor/ckeditor5-build-classic": "^41.0.0",
39
+ "@ckeditor/ckeditor5-code-block": "^41.0.0",
40
+ "@ckeditor/ckeditor5-document-outline": "^41.0.0",
41
+ "@ckeditor/ckeditor5-editor-classic": "^41.0.0",
42
+ "@ckeditor/ckeditor5-essentials": "^41.0.0",
43
+ "@ckeditor/ckeditor5-font": "^41.0.0",
44
+ "@ckeditor/ckeditor5-heading": "^41.0.0",
45
+ "@ckeditor/ckeditor5-highlight": "^41.0.0",
46
+ "@ckeditor/ckeditor5-horizontal-line": "^41.0.0",
47
+ "@ckeditor/ckeditor5-html-embed": "^41.0.0",
48
+ "@ckeditor/ckeditor5-html-support": "^41.0.0",
49
+ "@ckeditor/ckeditor5-image": "^41.0.0",
50
+ "@ckeditor/ckeditor5-import-word": "^41.0.0",
51
+ "@ckeditor/ckeditor5-indent": "^41.0.0",
52
+ "@ckeditor/ckeditor5-link": "^41.0.0",
53
+ "@ckeditor/ckeditor5-list": "^41.0.0",
54
+ "@ckeditor/ckeditor5-media-embed": "^41.0.0",
55
+ "@ckeditor/ckeditor5-paragraph": "^41.1.0",
56
+ "@ckeditor/ckeditor5-remove-format": "^41.0.0",
57
+ "@ckeditor/ckeditor5-show-blocks": "^41.0.0",
58
+ "@ckeditor/ckeditor5-source-editing": "^41.0.0",
59
+ "@ckeditor/ckeditor5-table": "^41.0.0",
60
+ "@ckeditor/ckeditor5-theme-lark": "^41.0.0",
61
+ "@ckeditor/ckeditor5-typing": "^41.0.0",
62
+ "@ckeditor/ckeditor5-upload": "^41.0.0",
63
+ "@ckeditor/ckeditor5-vue": "^5.1.0",
64
+ "@ckeditor/ckeditor5-word-count": "^41.0.0",
65
+ "@tanstack/vue-query": "^5.20.1",
66
+ "@vueuse/components": "^10.7.2",
67
+ "@vueuse/core": "^10.7.2",
68
+ "@vueuse/router": "^10.7.2",
69
+ "a-calc": "^1.3.8",
70
+ "ant-design-vue": "^4.1.2",
71
+ "archiver": "^6.0.1",
72
+ "axios": "^1.6.7",
73
+ "bignumber.js": "^9.1.2",
74
+ "chokidar": "^3.6.0",
75
+ "dayjs": "^1.11.10",
76
+ "echarts": "^5.4.3",
77
+ "execa": "^8.0.1",
78
+ "fast-glob": "^3.3.2",
79
+ "localstorage-slim": "^2.7.0",
80
+ "lodash-es": "^4.17.21",
81
+ "nprogress": "^0.2.0",
82
+ "pinia": "^2.1.7",
83
+ "tsx": "^4.7.1",
84
+ "vue": "^3.4.18",
85
+ "vue-router": "^4.2.5"
86
+ },
87
+ "devDependencies": {
88
+ "@peng_kai/lint": "^0.1.0",
89
+ "@types/archiver": "^6.0.2",
90
+ "@types/lodash-es": "^4.17.12",
91
+ "@types/node": "18.19.15",
92
+ "@types/nprogress": "^0.2.3",
93
+ "type-fest": "^4.10.2",
94
+ "typescript": "^5.3.3",
95
+ "vue-component-type-helpers": "^1.8.27"
96
+ }
97
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AxiosInstance } from 'axios';
2
2
 
3
- export { isTimeout, getServices, ApiCode, ApiError };
3
+ export { isTimeout, getServices, getBaseUrlByEnv, ApiCode, ApiError };
4
4
 
5
5
  enum ApiCode {
6
6
  NORMAL = 0, // 正常
@@ -27,6 +27,25 @@ function isTimeout(error: any) {
27
27
  return error?.message?.toLowerCase().includes('timeout');
28
28
  }
29
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
+
30
49
  class ApiError<TResp = any> extends Error {
31
50
  public static is<ErrorResp = any>(error: any): error is ApiError<ErrorResp> {
32
51
  return !!error?.isApiError;
@@ -9,13 +9,13 @@ export function checkCode(): Parameters<AxiosInterceptorManager<any>['use']> {
9
9
  return [
10
10
  (resp) => {
11
11
  if (
12
- resp.config.checkCode
12
+ resp?.config?.checkCode
13
13
  && !resp.config.ignoreCode?.includes(resp?.data?.code)
14
14
  && resp?.data?.code !== ApiCode.NORMAL
15
15
  ) {
16
16
  throw new axios.AxiosError(
17
- resp?.data?.msg ?? 'Unknown Error',
18
- resp?.data?.code ?? ApiCode.UNKNOWN,
17
+ resp.data?.msg ?? 'Unknown Error',
18
+ resp.data?.code ?? ApiCode.UNKNOWN,
19
19
  resp.config,
20
20
  resp.request,
21
21
  resp,
@@ -7,7 +7,7 @@ import { ApiCode, ApiError, isTimeout } from '../helpers';
7
7
  export function returnResultType(): Parameters<AxiosInterceptorManager<any>['use']> {
8
8
  return [
9
9
  (resp) => {
10
- const { resultType } = resp.config;
10
+ const { resultType } = resp?.config;
11
11
 
12
12
  if (resultType === 'api')
13
13
  return resp.data;
@@ -30,4 +30,4 @@ export function returnResultType(): Parameters<AxiosInterceptorManager<any>['use
30
30
  throw new ApiError({ code, msg, data });
31
31
  },
32
32
  ];
33
- }
33
+ }
@@ -1,8 +1,11 @@
1
1
  import type { QueryClientConfig, QueryObserverOptions } from '@tanstack/vue-query';
2
2
  import { ApiCode } from './helpers';
3
3
 
4
+ export { keepPreviousData } from '@tanstack/vue-query';
5
+ export { ignoreRetryCodes, buildRetryFn, presetOptions, getNextPageParam, getPreviousPageParam };
6
+
4
7
  /** 不需要重试的 api code */
5
- export const ignoreRetryCodes = [
8
+ const ignoreRetryCodes = [
6
9
  ApiCode.UNKNOWN,
7
10
  11000, // 操作失败
8
11
  11001, // 操作过于频繁
@@ -21,7 +24,7 @@ export const ignoreRetryCodes = [
21
24
  * @param additionPresetIgnore 一个布尔值,指示是否包括额外的预设忽略代码。默认为 true。
22
25
  * @returns 根据失败计数和错误代码确定是否重试的重试函数。
23
26
  */
24
- export function buildRetryFn(ignoreList: Array<number | string | RegExp>, additionPresetIgnore = true): NonNullable<QueryObserverOptions['retry']> {
27
+ function buildRetryFn(ignoreList: Array<number | string | RegExp>, additionPresetIgnore = true): NonNullable<QueryObserverOptions['retry']> {
25
28
  const _ignoreList = [...ignoreList, ...(additionPresetIgnore ? ignoreRetryCodes : [])];
26
29
  return (failureCount, error: any) => {
27
30
  if (error?.code === undefined)
@@ -38,29 +41,39 @@ export function buildRetryFn(ignoreList: Array<number | string | RegExp>, additi
38
41
  };
39
42
  }
40
43
 
41
- export const presetOptions: QueryClientConfig = {
44
+ const presetOptions: QueryClientConfig = {
42
45
  defaultOptions: {
43
46
  queries: {
44
47
  refetchOnWindowFocus: false,
45
48
  retry: buildRetryFn(ignoreRetryCodes),
46
- getNextPageParam: (lastPage: any) => {
47
- const { pagination } = lastPage ?? {};
48
-
49
- if (pagination?.has_more) {
50
- return {
51
- page: (pagination.page ?? 0) + 1,
52
- page_size: pagination?.page_size ?? 10,
53
- };
54
- }
55
- },
56
- getPreviousPageParam: (lastPage: any) => {
57
- const { pagination } = lastPage ?? {};
58
-
59
- return {
60
- page: (pagination.page ?? 1) - 1,
61
- page_size: pagination?.page_size ?? 10,
62
- };
63
- },
64
49
  },
65
50
  },
66
51
  };
52
+
53
+ /**
54
+ * useInfiniteQuery 中的 getNextPageParam 预设
55
+ */
56
+ function getNextPageParam(lastPage: any) {
57
+ const { pagination } = lastPage ?? {};
58
+
59
+ if (pagination?.has_more) {
60
+ return {
61
+ page: (pagination.page ?? 0) + 1,
62
+ page_size: pagination?.page_size ?? 10,
63
+ };
64
+ }
65
+ }
66
+
67
+ /**
68
+ * useInfiniteQuery 中的 getPreviousPageParam 预设
69
+ */
70
+ function getPreviousPageParam(firstPage: any) {
71
+ const { pagination } = firstPage ?? {};
72
+
73
+ if (pagination?.has_more) {
74
+ return {
75
+ page: (pagination.page ?? 0) + 1,
76
+ page_size: pagination?.page_size ?? 10,
77
+ };
78
+ }
79
+ }
@@ -0,0 +1,76 @@
1
+ import { S3Client } from '@aws-sdk/client-s3';
2
+ import { Upload } from '@aws-sdk/lib-storage';
3
+ import type { FileHandler } from './fileHandlers';
4
+
5
+ export type TGetParams = () => Promise<{
6
+ domain: string
7
+ bucket: string
8
+ region: string
9
+ accessKeyId: string
10
+ secretAccessKey: string
11
+ sessionToken: string
12
+ }>;
13
+
14
+ export class AwsS3 {
15
+ public domain = '';
16
+ public bucket = '';
17
+ public region = '';
18
+
19
+ private s3Client: S3Client | undefined = undefined;
20
+ private lastRefreshParams = 0;
21
+
22
+ public constructor(private getParams: TGetParams, private fileHandler?: FileHandler) {
23
+ }
24
+
25
+ public async upload(file: File, root?: string) {
26
+ const s3Client = await this.refreshClient();
27
+ const filePath = await this.genFilePath(root || 'unclassified');
28
+ const fileName = await (this.fileHandler?.genFileName(file) ?? this.genFileName(file));
29
+ const fullPath = `${filePath}/${fileName}`;
30
+
31
+ const uploader = new Upload({
32
+ client: s3Client!,
33
+ params: { Bucket: this.bucket, Key: fullPath, Body: file },
34
+ leavePartsOnError: false,
35
+ });
36
+
37
+ return { uploader, fileURL: `${this.domain}/${fullPath}` };
38
+ }
39
+
40
+ private genFilePath(root: string) {
41
+ const now = new Date();
42
+ const year = now.getFullYear();
43
+ const month = (now.getMonth() + 1).toString().padStart(2, '0');
44
+ const day = now.getDate().toString().padStart(2, '0');
45
+ const filePath = `${root}/${year}/${month}/${day}`;
46
+
47
+ return filePath;
48
+ }
49
+
50
+ private genFileName(file: File) {
51
+ return file.name;
52
+ }
53
+
54
+ private async refreshClient() {
55
+ const needRefresh = (Date.now() - this.lastRefreshParams) > 5 * 60 * 1000;
56
+
57
+ if (!needRefresh)
58
+ return this.s3Client;
59
+
60
+ const params = await this.getParams();
61
+ this.domain = params.domain;
62
+ this.bucket = params.bucket;
63
+ this.region = params.region;
64
+ this.s3Client = new S3Client({
65
+ region: this.region,
66
+ credentials: {
67
+ accessKeyId: params.accessKeyId,
68
+ secretAccessKey: params.secretAccessKey,
69
+ sessionToken: params.sessionToken,
70
+ },
71
+ });
72
+ this.lastRefreshParams = Date.now();
73
+
74
+ return this.s3Client;
75
+ }
76
+ }
@@ -0,0 +1,27 @@
1
+ export abstract class FileHandler {
2
+ abstract genFileName(file: File): Promise<string>;
3
+ }
4
+
5
+ export class ImageHandler implements FileHandler {
6
+ public genFileName(file: File) {
7
+ const fileNameParts = file.name.split('.');
8
+ const ext = fileNameParts.pop() ?? '';
9
+ const fileName = fileNameParts.join('.');
10
+ const now = new Date();
11
+ const timestamp = now.getTime().toString(36);
12
+ const randomStr = [...Array(5)].map(() => Math.random().toString(36)[2]).join('');
13
+
14
+ return new Promise<string>((resolve, reject) => {
15
+ const img = new Image();
16
+ const imgUrl = URL.createObjectURL(file);
17
+
18
+ img.onload = () => {
19
+ URL.revokeObjectURL(imgUrl);
20
+ resolve(`${fileName}_${randomStr}@${timestamp}@${img.width}x${img.height}.${ext}`);
21
+ };
22
+
23
+ img.onerror = reject;
24
+ img.src = imgUrl;
25
+ });
26
+ }
27
+ }
@@ -0,0 +1,2 @@
1
+ export { AwsS3, type TGetParams } from './AwsS3';
2
+ export { ImageHandler, type FileHandler } from './fileHandlers';
@@ -0,0 +1 @@
1
+ export function getProxy(env: Record<string, string>): Record<string, any>;