index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { message, Modal } from 'antd';
  2. import type { FirmwareItem } from '@/pages/device/Firmware/typings';
  3. import { createSchemaField } from '@formily/react';
  4. import { Form, FormGrid, FormItem, Input, Select } from '@formily/antd';
  5. import type { Field } from '@formily/core';
  6. import { createForm } from '@formily/core';
  7. import type { ISchema } from '@formily/json-schema';
  8. import FUpload from '@/components/Upload';
  9. import { action } from '@formily/reactive';
  10. import { service } from '@/pages/device/Firmware';
  11. import type { Response } from '@/utils/typings';
  12. import { useRef } from 'react';
  13. import type { ProductItem } from '@/pages/device/Product/typings';
  14. interface Props {
  15. data?: FirmwareItem;
  16. close: () => void;
  17. visible: boolean;
  18. }
  19. const Save = (props: Props) => {
  20. const { data, close, visible } = props;
  21. const form = createForm({
  22. validateFirst: true,
  23. initialValues: data,
  24. });
  25. const products = useRef<ProductItem[]>([]);
  26. const useAsyncDataSource = (services: (arg0: Field) => Promise<any>) => (field: Field) => {
  27. field.loading = true;
  28. services(field).then(
  29. action.bound!((list: any) => {
  30. field.dataSource = list.result.map((item: any) => ({ label: item.name, value: item.id }));
  31. products.current = list.result;
  32. field.loading = false;
  33. }),
  34. );
  35. };
  36. const loadData = async () => service.queryProduct();
  37. const SchemaField = createSchemaField({
  38. components: {
  39. FormItem,
  40. FormGrid,
  41. Input,
  42. FUpload,
  43. Select,
  44. },
  45. });
  46. const save = async () => {
  47. const values: FirmwareItem = await form.submit();
  48. const product = products.current?.find((item) => item.id === values.productId);
  49. values.productName = product?.name || '';
  50. const resp = (await service.save(values)) as Response<FirmwareItem>;
  51. if (resp.status === 200) {
  52. message.success('保存成功!');
  53. } else {
  54. message.error('保存失败!');
  55. }
  56. };
  57. const schema: ISchema = {
  58. type: 'object',
  59. properties: {
  60. grid: {
  61. type: 'void',
  62. 'x-component': 'FormGrid',
  63. 'x-component-props': {
  64. minColumns: 2,
  65. maxColumns: 2,
  66. },
  67. properties: {
  68. productId: {
  69. title: '产品',
  70. 'x-decorator': 'FormItem',
  71. 'x-component': 'Select',
  72. 'x-reactions': ['{{useAsyncDataSource(loadData)}}'],
  73. },
  74. name: {
  75. title: '名称',
  76. 'x-decorator': 'FormItem',
  77. 'x-component': 'Input',
  78. },
  79. version: {
  80. title: '版本号',
  81. 'x-decorator': 'FormItem',
  82. 'x-component': 'Input',
  83. },
  84. versionOrder: {
  85. title: '版本序号',
  86. 'x-decorator': 'FormItem',
  87. 'x-component': 'Input',
  88. },
  89. signMethod: {
  90. title: '签名方式',
  91. 'x-decorator': 'FormItem',
  92. 'x-component': 'Select',
  93. enum: [
  94. { label: 'MD5', value: 'MD5' },
  95. { label: 'SHA256', value: 'SHA256' },
  96. ],
  97. },
  98. sign: {
  99. title: '签名',
  100. 'x-decorator': 'FormItem',
  101. 'x-component': 'Input',
  102. },
  103. '{url,size}': {
  104. title: '文件上传',
  105. 'x-decorator': 'FormItem',
  106. 'x-component': 'FUpload',
  107. 'x-component-props': {
  108. type: 'file',
  109. },
  110. },
  111. },
  112. },
  113. },
  114. };
  115. return (
  116. <Modal
  117. maskClosable={false}
  118. width="50vw"
  119. title="新增固件版本"
  120. onCancel={() => close()}
  121. onOk={() => save()}
  122. visible={visible}
  123. >
  124. <Form form={form} labelCol={5} wrapperCol={16}>
  125. <SchemaField schema={schema} scope={{ useAsyncDataSource, loadData }} />
  126. </Form>
  127. </Modal>
  128. );
  129. };
  130. export default Save;