zy-react-library 1.0.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.
@@ -0,0 +1,162 @@
1
+ import { tools } from "@cqsjjb/jjb-common-lib";
2
+ import { useAntdTable } from "ahooks";
3
+
4
+ const { query } = tools.router;
5
+
6
+ /**
7
+ * 获取数据
8
+ */
9
+ function getService(service, getExtraParams = {}, transform) {
10
+ // 获取额外的参数
11
+ const extraParams = (typeof getExtraParams === "function" ? getExtraParams() : getExtraParams) || {};
12
+ // 获取数据
13
+ return async ({ current, pageSize }, formData = {}) => {
14
+ // 如果提供了 transform 函数,则在请求之前调用它
15
+ let transformedFormData = formData;
16
+ if (typeof transform === "function") {
17
+ const transformResult = transform(formData);
18
+ // 如果 transform 函数有返回值,则将其与原始表单数据合并,transform 的优先级更高
19
+ if (transformResult && typeof transformResult === "object") {
20
+ transformedFormData = { ...formData, ...transformResult };
21
+ }
22
+ }
23
+
24
+ // 发起请求
25
+ const res = await service({
26
+ pageIndex: current,
27
+ pageSize,
28
+ ...transformedFormData,
29
+ ...extraParams,
30
+ });
31
+ // 返回数据
32
+ return {
33
+ list: res.data || [],
34
+ total: res.totalCount || 0,
35
+ ...res,
36
+ };
37
+ };
38
+ }
39
+
40
+ /**
41
+ * 将搜索表单项和分页参数保存到 URL 中
42
+ */
43
+ function setQuery(searchForm, pagination) {
44
+ // 将对象转换为键值对字符串格式
45
+ const getJoinString = (data) => {
46
+ const keys = [];
47
+ const values = [];
48
+ Object.entries(data).forEach(([key, value]) => {
49
+ if (value) {
50
+ keys.push(key);
51
+ if (Array.isArray(value)) {
52
+ // 数组值使用方括号包裹,并用竖线分隔
53
+ values.push(`[${value.join("|")}]`);
54
+ }
55
+ else {
56
+ values.push(value);
57
+ }
58
+ }
59
+ });
60
+ return { keys: keys.join(","), values: values.join(",") };
61
+ };
62
+
63
+ // 获取搜索表单和分页数据的键值对字符串
64
+ const searchFormData = getJoinString(searchForm);
65
+ const paginationData = getJoinString(pagination);
66
+
67
+ // 将数据存储到 URL 查询参数中
68
+ query.searchFormKeys = searchFormData.keys;
69
+ query.searchFormValues = searchFormData.values;
70
+ query.paginationKeys = paginationData.keys;
71
+ query.paginationValues = paginationData.values;
72
+ }
73
+
74
+ /**
75
+ * 从 URL 中获取查询参数
76
+ */
77
+ function getQuery(keysStr, valuesStr) {
78
+ // 将键值字符串分割为数组
79
+ const keys = keysStr ? keysStr.split(",") : [];
80
+ const values = valuesStr ? valuesStr.split(",") : [];
81
+
82
+ // 构建结果对象
83
+ const resultMap = {};
84
+ keys.forEach((key, index) => {
85
+ if (values[index]) {
86
+ // 处理数组值(方括号包裹的值)
87
+ if (values[index].startsWith("[") && values[index].endsWith("]")) {
88
+ const arrayContent = values[index].substring(1, values[index].length - 1);
89
+ resultMap[key] = arrayContent ? arrayContent.split("|") : [];
90
+ }
91
+ else {
92
+ // 处理普通值
93
+ resultMap[key] = values[index];
94
+ }
95
+ }
96
+ });
97
+ return resultMap;
98
+ }
99
+
100
+ /**
101
+ * 自定义 useTable,继承 ahooks 的 useAntdTable,根据需求进行扩展
102
+ */
103
+ function useTable(service, options) {
104
+ // 获取额外参数和转换函数
105
+ const { params: extraParams, transform, ...restOptions } = options || {};
106
+
107
+ // 获取配置项
108
+ const {
109
+ useStorageQueryCriteria = true,
110
+ usePagination = true,
111
+ defaultType = "advance",
112
+ defaultCurrent = 1,
113
+ defaultPageSize = 10,
114
+ defaultPagination = { current: defaultCurrent, pageSize: defaultPageSize },
115
+ ...restRestOptions
116
+ } = restOptions;
117
+
118
+ // 获取存储的查询条件
119
+ const storageQueryCriteriaSearchForm = useStorageQueryCriteria ? getQuery(query.searchFormKeys, query.searchFormValues) : {};
120
+ const storageQueryCriteriaPagination = useStorageQueryCriteria && usePagination ? getQuery(query.paginationKeys, query.paginationValues) : {};
121
+
122
+ // 确定实际使用的搜索表单和分页参数
123
+ const actualSearchForm = Object.keys(storageQueryCriteriaSearchForm).length > 0 ? storageQueryCriteriaSearchForm : {};
124
+ /** @type {{current: number, pageSize: number}} */
125
+ const actualPagination = usePagination ? Object.keys(storageQueryCriteriaPagination).length > 0 ? storageQueryCriteriaPagination : defaultPagination : {};
126
+
127
+ // 调用 ahooks 的 useAntdTable
128
+ const res = useAntdTable(
129
+ getService(service, extraParams, transform),
130
+ {
131
+ ...restRestOptions,
132
+ defaultParams: [actualPagination, actualSearchForm],
133
+ defaultType,
134
+ onSuccess: (data, params) => {
135
+ // 执行成功回调,为了保留 ahooks 的 onSuccess 回调
136
+ restOptions.onSuccess && restOptions.onSuccess(data, params);
137
+ // 存储查询条件和分页到 URL
138
+ useStorageQueryCriteria && setQuery(
139
+ params[1] ?? {},
140
+ usePagination
141
+ ? { current: res.tableProps.pagination.current, pageSize: res.tableProps.pagination.pageSize }
142
+ : {},
143
+ );
144
+ },
145
+ },
146
+ );
147
+
148
+ // 执行回调函数
149
+ restOptions.callback && restOptions.callback(res?.data?.list || [], res?.data || {});
150
+
151
+ // 返回结果
152
+ return {
153
+ ...res,
154
+ tableProps: {
155
+ ...res.tableProps,
156
+ pagination: usePagination ? { ...res.tableProps.pagination, showQuickJumper: true, showSizeChanger: true } : false,
157
+ },
158
+ getData: res.search.submit,
159
+ };
160
+ }
161
+
162
+ export default useTable;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "zy-react-library",
3
+ "private": false,
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "description": "",
7
+ "author": "LiuJiaNan",
8
+ "license": "MIT",
9
+ "files": [
10
+ "components",
11
+ "enum",
12
+ "hooks",
13
+ "regular",
14
+ "utils",
15
+ "README.md"
16
+ ],
17
+ "engines": {
18
+ "node": ">=18.0.0"
19
+ },
20
+ "scripts": {
21
+ "postinstall": "echo 'Thanks for using our component library!'"
22
+ },
23
+ "dependencies": {
24
+ "@ant-design/icons": "^6.1.0",
25
+ "@ant-design/pro-components": "^2.8.10",
26
+ "@cqsjjb/jjb-common-lib": "latest",
27
+ "ahooks": "^3.9.5",
28
+ "antd": "^5.27.6",
29
+ "dayjs": "^1.11.18",
30
+ "lodash-es": "^4.17.21",
31
+ "react": "^18.3.1"
32
+ }
33
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * 匹配中国手机号码,可包含国家代码86,支持各种运营商号段。
3
+ */
4
+ export const PHONE: RegExp;
5
+
6
+ /**
7
+ * 匹配中国大陆的统一社会信用代码。
8
+ */
9
+ export const UNIFIED_SOCIAL_CREDIT_CODE: RegExp;
10
+
11
+ /**
12
+ * 匹配中国大陆的身份证号码,包括15位和18位号码,并验证最后一位校验码。
13
+ */
14
+ export const ID_NUMBER: RegExp;
15
+
16
+ /**
17
+ * 匹配中国大陆的移动电话号码,不包含国家代码。
18
+ */
19
+ export const MOBILE_PHONE: RegExp;
20
+
21
+ /**
22
+ * 匹配浮点数,允许整数、一位或两位小数,以及零的情况。
23
+ */
24
+ export const FLOATING_POINT_NUMBER: RegExp;
25
+
26
+ /**
27
+ * 两位小数。
28
+ */
29
+ export const TWO_DECIMAL_PLACES: RegExp;
30
+
31
+ /**
32
+ * 一位小数(非必须)。
33
+ */
34
+ export const ONE_DECIMAL_PLACES: RegExp;
35
+
36
+ /**
37
+ * 匹配中国大陆的车牌号码。
38
+ */
39
+ export const LICENSE_PLATE_NUMBER: RegExp;
40
+
41
+ /**
42
+ * 匹配强密码,要求至少8个字符,包含大小写字母、数字和特殊字符。
43
+ */
44
+ export const STRONG_PASSWORD: RegExp;
45
+
46
+ /**
47
+ * 匹配完整的HTML标签,包括开始标签和结束标签。
48
+ */
49
+ export const HTML_TAG: RegExp;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 匹配中国手机号码,可包含国家代码86,支持各种运营商号段。
3
+ */
4
+ export const PHONE
5
+ = /^(?:(?:\+|00)86)?1(?:3\d|4[5-7|9]|5[0-3|5-9]|6[5-7]|7[0-8]|8\d|9[1|89])\d{8}$/;
6
+
7
+ /**
8
+ * 匹配中国大陆的统一社会信用代码。
9
+ */
10
+ export const UNIFIED_SOCIAL_CREDIT_CODE
11
+ = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
12
+
13
+ /**
14
+ * 匹配中国大陆的身份证号码,包括15位和18位号码,并验证最后一位校验码。
15
+ */
16
+ export const ID_NUMBER
17
+ = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[12]\d|30|31)\d{3}[\dX]$/i;
18
+
19
+ /**
20
+ * 匹配中国大陆的移动电话号码,不包含国家代码。
21
+ */
22
+ export const MOBILE_PHONE
23
+ = /^(13\d|14[579]|15[0-3,5-9]|166|17[0135-8]|18\d|19[89])\d{8}$/;
24
+
25
+ /**
26
+ * 匹配浮点数,允许整数、一位或两位小数,以及零的情况。
27
+ */
28
+ export const FLOATING_POINT_NUMBER
29
+ = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^(0)$)|(^\d\.\d(\d)?$)/;
30
+
31
+ /**
32
+ * 两位小数。
33
+ */
34
+ export const TWO_DECIMAL_PLACES = /^\d+\.\d{2}$/;
35
+
36
+ /**
37
+ * 一位小数(非必须)。
38
+ */
39
+ export const ONE_DECIMAL_PLACES = /^\d+(\.\d?)?$/;
40
+
41
+ /**
42
+ * 匹配中国大陆的车牌号码。
43
+ */
44
+ export const LICENSE_PLATE_NUMBER
45
+ = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳])$/;
46
+
47
+ /**
48
+ * 匹配强密码,要求至少8个字符,包含大小写字母、数字和特殊字符。
49
+ */
50
+ export const STRONG_PASSWORD
51
+ = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,}$/;
52
+
53
+ /**
54
+ * 匹配完整的HTML标签,包括开始标签和结束标签。
55
+ */
56
+ export const HTML_TAG = /<[^>]*>/g;
@@ -0,0 +1,265 @@
1
+ import type { BasePaginationConfig } from "../hooks/useTable";
2
+
3
+ // 定义 getDataType 函数可能返回的所有类型
4
+ type DataType
5
+ = | "String"
6
+ | "Number"
7
+ | "Boolean"
8
+ | "Symbol"
9
+ | "Undefined"
10
+ | "Null"
11
+ | "Object"
12
+ | "Array"
13
+ | "Function"
14
+ | "Date"
15
+ | "RegExp"
16
+ | "Error"
17
+ | "Map"
18
+ | "Set"
19
+ | "WeakMap"
20
+ | "WeakSet"
21
+ | "ArrayBuffer"
22
+ | "DataView"
23
+ | "Promise"
24
+ | "Generator"
25
+ | "GeneratorFunction"
26
+ | "AsyncFunction"
27
+ | "Arguments"
28
+ | "Math"
29
+ | "JSON"
30
+ | "Window"
31
+ | "HTMLDocument"
32
+ | string; // 允许其他可能的类型
33
+
34
+ // 为 findCharIndex 函数定义接口类型
35
+ interface FindCharIndexOptions {
36
+ /** 查找的字符串 */
37
+ str: string;
38
+ /** 查找的字符 */
39
+ char: string;
40
+ /** 第几次出现 */
41
+ num: number;
42
+ }
43
+
44
+ // 为 paging 函数定义接口类型
45
+ interface PagingOptions {
46
+ /** 分页的数组 */
47
+ list: any[];
48
+ /** 当前页 */
49
+ currentPage: number | string;
50
+ /** 每页条数 */
51
+ pageSize: number | string;
52
+ }
53
+
54
+ // 为 addingPrefixToFile 函数定义接口类型
55
+ interface AddingPrefixToFileOptions {
56
+ /** 附件路径字段名 */
57
+ pathKey?: string;
58
+ /** 附件名称字段名 */
59
+ nameKey?: string;
60
+ /** 附件id字段名 */
61
+ idKey?: string;
62
+ }
63
+
64
+ // 为 getLabelName 函数定义接口类型
65
+ interface GetLabelNameOptions {
66
+ /** 状态 */
67
+ status: number | string;
68
+ /** 翻译的数组 */
69
+ list: any[];
70
+ /** id字段名 */
71
+ idKey?: string;
72
+ /** name字段名 */
73
+ nameKey?: string;
74
+ }
75
+
76
+ // 为 getSelectAppointItemList 函数定义接口类型
77
+ interface GetSelectAppointItemListOptions {
78
+ /** 获取的数组 */
79
+ list: any[];
80
+ /** 获取的值 */
81
+ value: any[];
82
+ /** 获取的id字段名 */
83
+ idKey?: string;
84
+ }
85
+
86
+ // 为 listTransTree 函数定义接口类型
87
+ interface ListTransTreeOptions {
88
+ /** 需要转换的json */
89
+ json: any[];
90
+ /** id字段 */
91
+ idKey: string;
92
+ /** 父级id字段 */
93
+ parentIdKey: string;
94
+ /** 子级字段 */
95
+ childrenKey: string;
96
+ }
97
+
98
+ // 为 isEmptyToWhether 函数定义接口类型
99
+ interface IsEmptyToWhetherOptions {
100
+ /** 真值时显示的文本 */
101
+ yesText?: string;
102
+ /** 假值时显示的文本 */
103
+ noText?: string;
104
+ /** 判断为真的值 */
105
+ yesValue?: string | number;
106
+ }
107
+
108
+ /**
109
+ * 计算序号
110
+ */
111
+ export function serialNumber(
112
+ pagination: BasePaginationConfig,
113
+ index: number
114
+ ): number;
115
+
116
+ /**
117
+ * 字符串数组转数组
118
+ */
119
+ export function toArrayString(value: string): Array<string>;
120
+
121
+ /**
122
+ * 判断文件后缀名是否符合
123
+ */
124
+ export function interceptTheSuffix(name: string, suffix: string): boolean;
125
+
126
+ /**
127
+ * 图片转base64
128
+ */
129
+ export function image2Base64(imgUrl: string): Promise<string>;
130
+
131
+ /**
132
+ * 图片转base64 (File对象版本)
133
+ */
134
+ export function image2Base642(file: File): Promise<string>;
135
+
136
+ /**
137
+ * 判断图片是否可访问成功
138
+ */
139
+ export function checkImgExists(imgUrl: string): Promise<any>;
140
+
141
+ /**
142
+ * 获取数据类型
143
+ */
144
+ export function getDataType(data: any): DataType;
145
+
146
+ /**
147
+ * 数组去重
148
+ */
149
+ export function ArrayDeduplication<T extends number | string>(arr: T[]): T[];
150
+
151
+ /**
152
+ * 数组对象去重
153
+ */
154
+ export function arrayObjectDeduplication<T>(arr: T[], key: string): T[];
155
+
156
+ /**
157
+ * 查找字符串中指定的值第几次出现的位置
158
+ */
159
+ export function findCharIndex(options: FindCharIndexOptions): number;
160
+
161
+ /**
162
+ * 生成指定两个值之间的随机数
163
+ */
164
+ export function randoms(min: number, max: number): number;
165
+
166
+ /**
167
+ * 千位分隔符
168
+ */
169
+ export function numFormat(num: number | string): string;
170
+
171
+ /**
172
+ * 验证是否为空
173
+ */
174
+ export function isEmpty(value: any): boolean;
175
+
176
+ /**
177
+ * 获取url参数
178
+ */
179
+ export function getUrlParam(key: string): string;
180
+
181
+ /**
182
+ * 数据分页
183
+ */
184
+ export function paging<T>(options: PagingOptions): T[];
185
+
186
+ /**
187
+ * 获取文件后缀
188
+ */
189
+ export function getFileSuffix(name: string): string;
190
+
191
+ /**
192
+ * 获取文件名称
193
+ */
194
+ export function getFileName(name: string): string;
195
+
196
+ /**
197
+ * 读取txt文档
198
+ */
199
+ export function readTxtDocument(filePah: string): Promise<string>;
200
+
201
+ /**
202
+ * 将秒转换成时分秒
203
+ */
204
+ export function secondConversion(second: string | number): string;
205
+
206
+ /**
207
+ * 附件添加前缀
208
+ */
209
+ export function addingPrefixToFile<T extends Record<string, any>>(
210
+ list: T[],
211
+ options?: AddingPrefixToFileOptions
212
+ ): (T & { url: string; name: string; imgFilesId: any })[];
213
+
214
+ /**
215
+ * 翻译状态
216
+ */
217
+ export function getLabelName<T>(options: GetLabelNameOptions): string | undefined;
218
+
219
+ /**
220
+ * 计算文件大小
221
+ */
222
+ export function calculateFileSize(size: number | string): string;
223
+
224
+ /**
225
+ * 根据身份证号获取出生日期和性别
226
+ */
227
+ export function idCardGetDateAndGender(idCard: string): { sex: "1" | "0"; date: string };
228
+
229
+ /**
230
+ * 获取select中指定项组成的数组
231
+ */
232
+ export function getSelectAppointItemList<T>(options: GetSelectAppointItemListOptions): T[];
233
+
234
+ /**
235
+ * json转换为树形结构
236
+ */
237
+ export function listTransTree<T>(options: ListTransTreeOptions): T[];
238
+
239
+ /**
240
+ * 将值转换为"是"/"否"显示文本
241
+ */
242
+ export function isEmptyToWhether(
243
+ value: any,
244
+ options?: IsEmptyToWhetherOptions
245
+ ): string;
246
+
247
+ /**
248
+ * 生成指定长度的guid
249
+ */
250
+ export function createGuid(len?: number): string;
251
+
252
+ /**
253
+ * 获取序号列
254
+ */
255
+ export function getIndexColumn(pagination: false | BasePaginationConfig): {
256
+ title: string;
257
+ key: string;
258
+ width: number;
259
+ render: (...args: any[]) => number;
260
+ };
261
+
262
+ /**
263
+ * 获取文件url
264
+ */
265
+ export function getFileUrl(): string;