neo-cmp-cli 1.2.23 → 1.2.26

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,621 @@
1
+ /**
2
+ * @file Object Form 对象表单组件
3
+ * @description 基于 Neo 平台的 XObject 实体对象数据表单组件,用于新增数据
4
+ */
5
+ import * as React from 'react';
6
+ import {
7
+ Form,
8
+ Input,
9
+ Select,
10
+ Button,
11
+ message,
12
+ Card,
13
+ DatePicker,
14
+ InputNumber,
15
+ Space,
16
+ Spin,
17
+ Empty,
18
+ Row,
19
+ Col,
20
+ } from 'antd';
21
+ import {
22
+ SaveOutlined,
23
+ ReloadOutlined,
24
+ CheckCircleOutlined,
25
+ } from '@ant-design/icons';
26
+ import moment from 'moment';
27
+ // @ts-ignore
28
+ import { xObject } from 'neo-open-api'; // Neo Open API
29
+ // @ts-ignore
30
+ import isEqual from 'lodash/isEqual';
31
+ import './style.scss';
32
+
33
+ const { Option } = Select;
34
+
35
+ /**
36
+ * 组件属性接口
37
+ */
38
+ interface ObjectFormProps {
39
+ /** XObject 实体对象的 API Key,用于标识要操作的数据对象 */
40
+ xObjectDataApi: {
41
+ xObjectApiKey: string;
42
+ fields?: string[];
43
+ fieldDescList?: any[];
44
+ };
45
+ /** Neo 平台传递的数据,包含系统信息等 */
46
+ data?: any;
47
+ /** 表单标题 */
48
+ formTitle?: string;
49
+ /** 是否显示重置按钮 */
50
+ showResetButton?: boolean;
51
+ /** 表单列数(1列或2列) */
52
+ columnCount?: 1 | 2;
53
+ /** 提交成功后的回调 */
54
+ onSuccess?: (data: any) => void;
55
+ /** 是否为自定义实体对象 */
56
+ custom?: boolean;
57
+ }
58
+
59
+ /**
60
+ * 字段信息接口
61
+ */
62
+ interface FieldInfo {
63
+ /** 字段名称 */
64
+ name: string;
65
+ /** 字段显示标签 */
66
+ label: string;
67
+ /** 字段 API Key */
68
+ apiKey: string;
69
+ /** 字段类型 */
70
+ type: string;
71
+ /** 字段项类型 */
72
+ itemType: string;
73
+ /** 复选框选项列表 */
74
+ checkitem: any[];
75
+ /** 下拉选择选项列表 */
76
+ selectitem?: any[];
77
+ /** 是否必填 */
78
+ required: boolean;
79
+ }
80
+
81
+ /**
82
+ * 组件状态接口
83
+ */
84
+ interface ObjectFormState {
85
+ /** 表单标题 */
86
+ title?: string;
87
+ /** 字段列表 */
88
+ fieldList: FieldInfo[];
89
+ /** 加载状态 */
90
+ loading: boolean;
91
+ /** 提交中状态 */
92
+ submitting: boolean;
93
+ /** 错误信息 */
94
+ error: string | null;
95
+ /** 表单实例 */
96
+ form: any;
97
+ /** 业务类型列表 */
98
+ entityTypeList: any[];
99
+ /** 提交成功状态 */
100
+ submitSuccess: boolean;
101
+ }
102
+
103
+ /**
104
+ * Object Form 对象表单组件
105
+ * 支持对 Neo 平台的 XObject 实体对象进行新增操作
106
+ */
107
+ export default class ObjectForm extends React.PureComponent<
108
+ ObjectFormProps,
109
+ ObjectFormState
110
+ > {
111
+ constructor(props: ObjectFormProps) {
112
+ super(props);
113
+ const { xObjectDataApi } = props;
114
+ const { xObjectApiKey } = xObjectDataApi || {};
115
+
116
+ // 初始化组件状态
117
+ this.state = {
118
+ fieldList: [],
119
+ loading: false,
120
+ submitting: false,
121
+ error: null,
122
+ form: null,
123
+ entityTypeList: [],
124
+ submitSuccess: false,
125
+ };
126
+
127
+ if (xObjectApiKey) {
128
+ // 初始化字段列表和业务类型列表
129
+ this.getEntityTypeList(xObjectApiKey);
130
+ this.loadFieldList();
131
+ }
132
+
133
+ // 绑定方法上下文
134
+ this.loadFieldList = this.loadFieldList.bind(this);
135
+ this.handleSubmit = this.handleSubmit.bind(this);
136
+ this.handleReset = this.handleReset.bind(this);
137
+ }
138
+
139
+ /**
140
+ * 组件更新后执行
141
+ * 当 xObjectApiKey 发生变化时重新加载字段列表
142
+ */
143
+ async componentDidUpdate(prevProps: ObjectFormProps) {
144
+ const { xObjectApiKey, fields } = this.props.xObjectDataApi || {};
145
+ const { xObjectApiKey: prevXObjectApiKey, fields: prevFields } =
146
+ prevProps.xObjectDataApi || {};
147
+
148
+ if (
149
+ xObjectApiKey !== prevXObjectApiKey ||
150
+ !isEqual(fields, prevFields)
151
+ ) {
152
+ if (xObjectApiKey) {
153
+ await this.loadFieldList();
154
+ await this.getEntityTypeList(xObjectApiKey);
155
+ } else {
156
+ this.setState({
157
+ fieldList: [],
158
+ });
159
+ }
160
+ }
161
+ }
162
+
163
+ /**
164
+ * 获取业务类型列表
165
+ * 用于下拉选择框的选项数据
166
+ */
167
+ async getEntityTypeList(xObjectApiKey?: string) {
168
+ const { xObjectDataApi } = this.props;
169
+ const curXObjectApiKey = xObjectApiKey || xObjectDataApi?.xObjectApiKey;
170
+ try {
171
+ const result = await xObject.getEntityTypeList(curXObjectApiKey);
172
+ if (result && result.status) {
173
+ const records = result.data || [];
174
+ this.setState({
175
+ entityTypeList: records,
176
+ });
177
+ }
178
+ } catch (error) {
179
+ console.error('获取业务类型列表失败:', error);
180
+ }
181
+ }
182
+
183
+ /**
184
+ * 加载字段列表
185
+ * 从 Neo 平台获取 XObject 的字段描述信息
186
+ */
187
+ async loadFieldList() {
188
+ const { xObjectDataApi } = this.props || {};
189
+
190
+ this.setState({ loading: true });
191
+
192
+ try {
193
+ // 方式一:直接从 props.xObjectDataApi 中获取字段描述
194
+ if (xObjectDataApi && xObjectDataApi.fieldDescList) {
195
+ this.setState({
196
+ fieldList: xObjectDataApi.fieldDescList,
197
+ loading: false,
198
+ });
199
+ } else {
200
+ // 方式二:自行通过 OpenAPI SDK 获取字段描述
201
+ if (!xObjectDataApi.xObjectApiKey) {
202
+ this.setState({ loading: false });
203
+ return;
204
+ }
205
+ const resultData = await xObject.getDesc(xObjectDataApi.xObjectApiKey);
206
+ if (resultData && resultData.status) {
207
+ const result = resultData.data || {};
208
+ const fieldList = result.fields || [];
209
+ this.setState({
210
+ fieldList,
211
+ title: result.label,
212
+ loading: false,
213
+ });
214
+ } else {
215
+ this.setState({
216
+ error: '获取字段列表失败',
217
+ loading: false,
218
+ });
219
+ }
220
+ }
221
+ } catch (error) {
222
+ console.error('获取字段列表失败:', error);
223
+ message.error('获取字段列表失败');
224
+ this.setState({
225
+ error: '获取字段列表失败',
226
+ loading: false,
227
+ });
228
+ }
229
+ }
230
+
231
+ /**
232
+ * 处理表单提交
233
+ * 执行新增操作
234
+ */
235
+ handleSubmit() {
236
+ this.state.form?.validateFields().then(async (values: any) => {
237
+ const { xObjectApiKey } = this.props.xObjectDataApi || {};
238
+ const { fieldList } = this.state;
239
+
240
+ if (!xObjectApiKey) {
241
+ message.error('请先选择要操作的对象');
242
+ return;
243
+ }
244
+
245
+ this.setState({ submitting: true });
246
+
247
+ try {
248
+ // 处理日期格式的字段,转换为时间戳
249
+ Object.keys(values).forEach((fieldKey: any) => {
250
+ const field = fieldList.find(
251
+ (field: any) => field.apiKey === fieldKey,
252
+ );
253
+ if (
254
+ field &&
255
+ values[fieldKey] &&
256
+ (field.type === 'date' ||
257
+ field.type === 'datetime' ||
258
+ field.type === 'time')
259
+ ) {
260
+ values[fieldKey] = new Date(values[fieldKey]).getTime();
261
+ }
262
+ });
263
+
264
+ // 调用新增接口
265
+ const response = await xObject.create(xObjectApiKey, {
266
+ data: values,
267
+ method: 'POST',
268
+ });
269
+
270
+ if (response && (response.code === 200 || response.code === '200')) {
271
+ message.success('创建成功');
272
+ this.setState({
273
+ submitting: false,
274
+ submitSuccess: true,
275
+ });
276
+
277
+ // 重置表单
278
+ this.state.form?.resetFields();
279
+
280
+ // 1秒后隐藏成功提示
281
+ setTimeout(() => {
282
+ this.setState({ submitSuccess: false });
283
+ }, 2000);
284
+
285
+ // 调用成功回调
286
+ if (this.props.onSuccess) {
287
+ this.props.onSuccess(response.data);
288
+ }
289
+ } else {
290
+ message.error(response?.message || '创建失败');
291
+ this.setState({ submitting: false });
292
+ }
293
+ } catch (error) {
294
+ console.error('创建失败:', error);
295
+ message.error('创建失败');
296
+ this.setState({ submitting: false });
297
+ }
298
+ });
299
+ }
300
+
301
+ /**
302
+ * 处理表单重置
303
+ */
304
+ handleReset() {
305
+ this.state.form?.resetFields();
306
+ this.setState({ submitSuccess: false });
307
+ message.info('表单已重置');
308
+ }
309
+
310
+ /**
311
+ * 渲染表单内容
312
+ * 根据字段类型动态生成对应的输入组件
313
+ * @returns 表单 JSX 元素
314
+ */
315
+ renderForm() {
316
+ const { fieldList, entityTypeList, submitSuccess } = this.state;
317
+ const { xObjectApiKey, fields } = this.props.xObjectDataApi || {};
318
+ const { columnCount = 1 } = this.props;
319
+
320
+ let curFieldList = fieldList;
321
+
322
+ // 如果指定了 fields,则只显示指定的字段
323
+ if (fields && fields.length > 0) {
324
+ curFieldList = fieldList.filter((field) => fields.includes(field.apiKey));
325
+ }
326
+
327
+ // 如果没有选择对象,显示提示信息
328
+ if (!xObjectApiKey) {
329
+ return (
330
+ <Empty
331
+ description="请先选择要操作的对象"
332
+ image={Empty.PRESENTED_IMAGE_SIMPLE}
333
+ />
334
+ );
335
+ }
336
+
337
+ // 如果字段列表为空,显示提示信息
338
+ if (curFieldList.length === 0) {
339
+ return (
340
+ <Empty
341
+ description="暂无可用字段"
342
+ image={Empty.PRESENTED_IMAGE_SIMPLE}
343
+ />
344
+ );
345
+ }
346
+
347
+ const formItemLayout = columnCount === 2 ? {
348
+ labelCol: { span: 8 },
349
+ wrapperCol: { span: 16 }
350
+ } : {
351
+ labelCol: { span: 4 },
352
+ wrapperCol: { span: 20 }
353
+ };
354
+
355
+ return (
356
+ <Form
357
+ ref={(form) => this.setState({ form })}
358
+ layout="horizontal"
359
+ {...formItemLayout}
360
+ >
361
+ <Row gutter={16}>
362
+ {curFieldList.map((field) => {
363
+ // 跳过 ID 字段,不需要在表单中显示
364
+ if (field.apiKey === 'id') return null;
365
+
366
+ let inputComponent;
367
+ const selectitem = field.selectitem || [];
368
+ const checkitem = field.checkitem || [];
369
+
370
+ // 根据字段类型生成对应的输入组件
371
+ switch (field.type) {
372
+ case 'entityType':
373
+ case 'entitytype':
374
+ // 业务类型选择器
375
+ inputComponent = (
376
+ <Select
377
+ placeholder="请选择业务类型"
378
+ allowClear
379
+ >
380
+ {entityTypeList.map((item) => (
381
+ <Option
382
+ key={item.apiKey}
383
+ value={item.id}
384
+ disabled={!item.active}
385
+ >
386
+ {item.label}
387
+ </Option>
388
+ ))}
389
+ </Select>
390
+ );
391
+ break;
392
+ case 'picklist':
393
+ // 单选下拉框
394
+ inputComponent = (
395
+ <Select
396
+ placeholder={`请选择${field.label}`}
397
+ allowClear
398
+ >
399
+ {selectitem.map((item) => (
400
+ <Option key={item.apiKey} value={item.id}>
401
+ {item.label}
402
+ </Option>
403
+ ))}
404
+ </Select>
405
+ );
406
+ break;
407
+ case 'textarea':
408
+ // 多行文本输入框
409
+ inputComponent = (
410
+ <Input.TextArea
411
+ placeholder={`请输入${field.label}`}
412
+ rows={3}
413
+ showCount
414
+ maxLength={500}
415
+ />
416
+ );
417
+ break;
418
+ case 'multipicklist':
419
+ // 多选下拉框
420
+ inputComponent = (
421
+ <Select
422
+ placeholder={`请选择${field.label}`}
423
+ mode="multiple"
424
+ allowClear
425
+ >
426
+ {checkitem.map((item) => (
427
+ <Option key={item.apiKey} value={item.id}>
428
+ {item.label}
429
+ </Option>
430
+ ))}
431
+ </Select>
432
+ );
433
+ break;
434
+ case 'datetime':
435
+ // 日期时间选择器
436
+ inputComponent = (
437
+ <DatePicker
438
+ placeholder={`请选择${field.label}`}
439
+ format={'YYYY/MM/DD HH:mm:ss'}
440
+ showTime={true}
441
+ style={{ width: '100%' }}
442
+ />
443
+ );
444
+ break;
445
+ case 'date':
446
+ // 日期选择器
447
+ inputComponent = (
448
+ <DatePicker
449
+ placeholder={`请选择${field.label}`}
450
+ format={'YYYY/MM/DD'}
451
+ style={{ width: '100%' }}
452
+ />
453
+ );
454
+ break;
455
+ case 'time':
456
+ // 时间选择器
457
+ inputComponent = (
458
+ <DatePicker
459
+ placeholder={`请选择${field.label}`}
460
+ format={'HH:mm:ss'}
461
+ showTime={true}
462
+ style={{ width: '100%' }}
463
+ />
464
+ );
465
+ break;
466
+ case 'int':
467
+ case 'float':
468
+ // 数字输入框
469
+ inputComponent = (
470
+ <InputNumber
471
+ placeholder={`请输入${field.label}`}
472
+ style={{ width: '100%' }}
473
+ />
474
+ );
475
+ break;
476
+ case 'email':
477
+ // 邮箱输入框
478
+ inputComponent = (
479
+ <Input
480
+ placeholder={`请输入${field.label}`}
481
+ type="email"
482
+ />
483
+ );
484
+ break;
485
+ case 'url':
486
+ // URL 输入框
487
+ inputComponent = (
488
+ <Input
489
+ placeholder={`请输入${field.label}`}
490
+ type="url"
491
+ />
492
+ );
493
+ break;
494
+ case 'phone':
495
+ // 电话号码输入框
496
+ inputComponent = (
497
+ <Input
498
+ placeholder={`请输入${field.label}`}
499
+ type="tel"
500
+ />
501
+ );
502
+ break;
503
+ default:
504
+ // 默认文本输入框
505
+ inputComponent = (
506
+ <Input
507
+ placeholder={`请输入${field.label}`}
508
+ maxLength={200}
509
+ />
510
+ );
511
+ }
512
+
513
+ const colSpan = columnCount === 2 ? 12 : 24;
514
+
515
+ return (
516
+ <Col span={colSpan} key={field.apiKey}>
517
+ <Form.Item
518
+ name={field.apiKey}
519
+ label={field.label}
520
+ required={field.required ?? false}
521
+ rules={[
522
+ {
523
+ required: field.required,
524
+ message: `请${field.type === 'picklist' || field.type === 'multipicklist' || field.type === 'entityType' || field.type === 'entitytype' || field.type === 'date' || field.type === 'datetime' || field.type === 'time' ? '选择' : '输入'}${field.label}`,
525
+ },
526
+ ]}
527
+ >
528
+ {inputComponent}
529
+ </Form.Item>
530
+ </Col>
531
+ );
532
+ })}
533
+ </Row>
534
+
535
+ {submitSuccess && (
536
+ <div className="submit-success-message">
537
+ <CheckCircleOutlined /> 数据已成功提交
538
+ </div>
539
+ )}
540
+ </Form>
541
+ );
542
+ }
543
+
544
+ /**
545
+ * 渲染组件
546
+ * @returns 组件 JSX 元素
547
+ */
548
+ render() {
549
+ const { title, loading, submitting, error } = this.state;
550
+ const { formTitle, showResetButton = true, data } = this.props;
551
+
552
+ // 测试输出
553
+ console.log('this.props:', this.props);
554
+
555
+ const curAmisData = data || {};
556
+ const systemInfo = curAmisData.__NeoSystemInfo || {};
557
+ const { xObjectApiKey } = this.props.xObjectDataApi || {};
558
+
559
+ const displayTitle = formTitle || title || '新增数据';
560
+
561
+ return (
562
+ <div className="object-form-container">
563
+ <Card>
564
+ <div className="form-header">
565
+ <h3 className="form-title">
566
+ {displayTitle}
567
+ {systemInfo.tenantName ? `【${systemInfo.tenantName}】` : ''}
568
+ </h3>
569
+ </div>
570
+
571
+ <div className="form-content">
572
+ <Spin spinning={loading} tip="加载字段信息...">
573
+ {error ? (
574
+ <Empty
575
+ image={Empty.PRESENTED_IMAGE_SIMPLE}
576
+ description={
577
+ <div>
578
+ <div style={{ color: '#ff4d4f', marginBottom: 8 }}>
579
+ {error}
580
+ </div>
581
+ <Button type="primary" onClick={this.loadFieldList}>
582
+ 重新加载
583
+ </Button>
584
+ </div>
585
+ }
586
+ />
587
+ ) : (
588
+ <>
589
+ {this.renderForm()}
590
+ <div className="form-actions">
591
+ <Space size="middle">
592
+ <Button
593
+ type="primary"
594
+ icon={<SaveOutlined />}
595
+ onClick={this.handleSubmit}
596
+ loading={submitting}
597
+ disabled={!xObjectApiKey}
598
+ >
599
+ {submitting ? '提交中...' : '提交'}
600
+ </Button>
601
+ {showResetButton && (
602
+ <Button
603
+ icon={<ReloadOutlined />}
604
+ onClick={this.handleReset}
605
+ disabled={submitting}
606
+ >
607
+ 重置
608
+ </Button>
609
+ )}
610
+ </Space>
611
+ </div>
612
+ </>
613
+ )}
614
+ </Spin>
615
+ </div>
616
+ </Card>
617
+ </div>
618
+ );
619
+ }
620
+ }
621
+