SaveModal.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Form, Input, message, Modal, Select } from 'antd';
  2. import { useEffect, useState } from 'react';
  3. import { service } from './index';
  4. import { useRequest } from 'ahooks';
  5. import { OperatorList, TypeList } from '@/pages/iot-card/data';
  6. type SaveType = {
  7. type: 'add' | 'edit';
  8. data?: any;
  9. onCancel: () => void;
  10. onOk: () => void;
  11. };
  12. const Save = (props: SaveType) => {
  13. const [form] = Form.useForm();
  14. const [loading, setLoading] = useState(false);
  15. const { data: platformList, run: platformRun } = useRequest(service.queryPlatformNoPage, {
  16. manual: true,
  17. formatResult(result) {
  18. return result.result;
  19. },
  20. });
  21. useEffect(() => {
  22. platformRun({
  23. sorts: [{ name: 'createTime', order: 'desc' }],
  24. terms: [{ column: 'state', value: 'enabled' }],
  25. });
  26. if (props.type === 'edit' && form) {
  27. form.setFieldsValue(props.data);
  28. }
  29. }, []);
  30. const submit = async () => {
  31. const formData = await form.validateFields();
  32. if (formData) {
  33. setLoading(true);
  34. const resp =
  35. props.type === 'add' ? await service.add(formData) : await service.edit(formData);
  36. setLoading(false);
  37. if (resp.status === 200) {
  38. message.success('操作成功');
  39. props?.onOk();
  40. }
  41. }
  42. };
  43. const isValidateId = async (id: string) => {
  44. const res = await service.validateId(id);
  45. if (res.status === 200) {
  46. if (res.result?.passed) {
  47. return '';
  48. } else {
  49. return res.result.reason;
  50. }
  51. } else {
  52. return '请输入输入正确的ICCID';
  53. }
  54. };
  55. return (
  56. <Modal
  57. title={props.type === 'add' ? '新增' : '编辑'}
  58. visible={true}
  59. width={600}
  60. onCancel={props.onCancel}
  61. onOk={submit}
  62. confirmLoading={loading}
  63. >
  64. <Form
  65. form={form}
  66. labelCol={{
  67. style: { width: 100 },
  68. }}
  69. layout={'vertical'}
  70. >
  71. <Form.Item
  72. label={'卡号'}
  73. name={'id'}
  74. required
  75. rules={[
  76. { required: true, message: '请输入卡号' },
  77. { max: 64, message: '最多可输入64个字符' },
  78. () => ({
  79. async validator(_, value) {
  80. if (value) {
  81. const validateId = await isValidateId(value);
  82. if (validateId === '') {
  83. return Promise.resolve();
  84. } else {
  85. return Promise.reject(new Error(`${validateId}`));
  86. }
  87. } else {
  88. return Promise.reject(new Error('请输入输入正确的卡号'));
  89. }
  90. },
  91. }),
  92. ]}
  93. >
  94. <Input placeholder={'请输入卡号'} disabled={props.type === 'edit'} />
  95. </Form.Item>
  96. <Form.Item
  97. label={'ICCID'}
  98. name={'iccId'}
  99. required
  100. rules={[
  101. { required: true, message: '请输入ICCID' },
  102. { max: 64, message: '最多可输入64个字符' },
  103. ]}
  104. >
  105. <Input placeholder={'请输入ICCID'} disabled={props.type === 'edit'} />
  106. </Form.Item>
  107. <Form.Item
  108. label={'平台对接'}
  109. name={'platformConfigId'}
  110. required
  111. rules={[{ required: true, message: '请选择平台对接' }]}
  112. >
  113. <Select
  114. showSearch
  115. placeholder={'请选择平台对接'}
  116. disabled={props.type === 'edit'}
  117. fieldNames={{ label: 'name', value: 'id' }}
  118. options={platformList}
  119. filterOption={(input, option) => {
  120. if (option?.name) {
  121. return option.name.includes(input);
  122. }
  123. return false;
  124. }}
  125. />
  126. </Form.Item>
  127. <Form.Item label={'运营商'} name={'operatorName'}>
  128. <Select
  129. showSearch
  130. placeholder={'请选择运营商'}
  131. options={OperatorList}
  132. filterOption={(input, option) => {
  133. if (option?.label) {
  134. return option.label.includes(input);
  135. }
  136. return false;
  137. }}
  138. />
  139. </Form.Item>
  140. <Form.Item
  141. label={'类型'}
  142. name={'cardType'}
  143. required
  144. rules={[{ required: true, message: '请选择类型' }]}
  145. >
  146. <Select
  147. showSearch
  148. placeholder={'请选择类型'}
  149. disabled={props.type === 'edit'}
  150. options={TypeList}
  151. filterOption={(input, option) => {
  152. if (option?.label) {
  153. return option.label.includes(input);
  154. }
  155. return false;
  156. }}
  157. />
  158. </Form.Item>
  159. <Form.Item label={'说明'} name={'describe'}>
  160. <Input.TextArea showCount maxLength={200} placeholder="请输入说明" />
  161. </Form.Item>
  162. </Form>
  163. </Modal>
  164. );
  165. };
  166. export default Save;