index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import PermissionButton from '@/components/PermissionButton';
  2. import usePermissions from '@/hooks/permission';
  3. import { PageContainer } from '@ant-design/pro-layout';
  4. import { Form, FormButtonGroup, FormItem } from '@formily/antd';
  5. import type { ISchema } from '@formily/json-schema';
  6. import { Card, Col, Input, message, Row } from 'antd';
  7. import { createSchemaField, observer } from '@formily/react';
  8. import { useEffect, useMemo } from 'react';
  9. import { createForm } from '@formily/core';
  10. import CertificateFile from './components/CertificateFile';
  11. import Standard from './components/Standard';
  12. import { service } from '@/pages/link/Certificate';
  13. import { useParams } from 'umi';
  14. const Detail = observer(() => {
  15. const params = useParams<{ id: string }>();
  16. const form = useMemo(
  17. () =>
  18. createForm({
  19. validateFirst: true,
  20. }),
  21. [],
  22. );
  23. useEffect(() => {
  24. if (params.id && params.id !== ':id') {
  25. service.detail(params.id).then((resp) => {
  26. if (resp.status === 200) {
  27. form.setValues(resp.result);
  28. }
  29. });
  30. }
  31. }, [params.id]);
  32. const SchemaField = createSchemaField({
  33. components: {
  34. FormItem,
  35. Input,
  36. CertificateFile,
  37. Standard,
  38. },
  39. });
  40. const schema: ISchema = {
  41. type: 'object',
  42. properties: {
  43. type: {
  44. type: 'string',
  45. title: '证书标准',
  46. required: true,
  47. default: 'common',
  48. 'x-decorator': 'FormItem',
  49. 'x-component': 'Standard',
  50. },
  51. name: {
  52. type: 'string',
  53. title: '证书名称',
  54. required: true,
  55. 'x-decorator': 'FormItem',
  56. 'x-component': 'Input',
  57. 'x-component-props': {
  58. placeholder: '请输入证书名称',
  59. },
  60. 'x-validator': [
  61. {
  62. max: 64,
  63. message: '最多可输入64个字符',
  64. },
  65. ],
  66. },
  67. 'configs.cert': {
  68. title: '证书文件',
  69. 'x-component': 'CertificateFile',
  70. 'x-decorator': 'FormItem',
  71. required: true,
  72. 'x-component-props': {
  73. rows: 3,
  74. placeholder:
  75. '证书私钥格式以"-----BEGIN (RSA|EC) PRIVATE KEY-----"开头,以"-----END(RSA|EC) PRIVATE KEY-----"结尾。',
  76. },
  77. },
  78. 'configs.key': {
  79. title: '证书私钥',
  80. 'x-component': 'Input.TextArea',
  81. 'x-decorator': 'FormItem',
  82. required: true,
  83. 'x-component-props': {
  84. rows: 3,
  85. placeholder:
  86. '证书私钥格式以"-----BEGIN (RSA|EC) PRIVATE KEY-----"开头,以"-----END(RSA|EC) PRIVATE KEY-----"结尾。',
  87. },
  88. },
  89. description: {
  90. title: '说明',
  91. 'x-component': 'Input.TextArea',
  92. 'x-decorator': 'FormItem',
  93. 'x-component-props': {
  94. rows: 3,
  95. showCount: true,
  96. maxLength: 200,
  97. placeholder: '请输入说明',
  98. },
  99. },
  100. },
  101. };
  102. const { getOtherPermission } = usePermissions('link/Certificate');
  103. return (
  104. <PageContainer>
  105. <Card>
  106. <Row gutter={24}>
  107. <Col span={12}>
  108. <Form form={form} layout="vertical">
  109. <SchemaField schema={schema} />
  110. <FormButtonGroup.Sticky>
  111. <FormButtonGroup.FormItem>
  112. <PermissionButton
  113. type="primary"
  114. isPermission={getOtherPermission(['add', 'update'])}
  115. onClick={async () => {
  116. const data: any = await form.submit();
  117. const response: any = data.id
  118. ? await service.update(data)
  119. : await service.save(data);
  120. if (response.status === 200) {
  121. message.success('操作成功');
  122. history.back();
  123. }
  124. }}
  125. >
  126. 保存
  127. </PermissionButton>
  128. </FormButtonGroup.FormItem>
  129. </FormButtonGroup.Sticky>
  130. </Form>
  131. </Col>
  132. </Row>
  133. </Card>
  134. </PageContainer>
  135. );
  136. });
  137. export default Detail;