| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import { Form, Input, message, Modal, Select } from 'antd';
- import { useEffect, useState } from 'react';
- import { service } from './index';
- import { useRequest } from 'ahooks';
- import { OperatorList, TypeList } from '@/pages/iot-card/data';
- type SaveType = {
- type: 'add' | 'edit';
- data?: any;
- onCancel: () => void;
- onOk: () => void;
- };
- const Save = (props: SaveType) => {
- const [form] = Form.useForm();
- const [loading, setLoading] = useState(false);
- const { data: platformList, run: platformRun } = useRequest(service.queryPlatformNoPage, {
- manual: true,
- formatResult(result) {
- return result.result;
- },
- });
- useEffect(() => {
- platformRun({
- sorts: [{ name: 'createTime', order: 'desc' }],
- terms: [{ column: 'state', value: 'enabled' }],
- });
- if (props.type === 'edit' && form) {
- form.setFieldsValue(props.data);
- }
- }, []);
- const submit = async () => {
- const formData = await form.validateFields();
- if (formData) {
- setLoading(true);
- const resp =
- props.type === 'add' ? await service.add(formData) : await service.edit(formData);
- setLoading(false);
- if (resp.status === 200) {
- message.success('操作成功');
- props?.onOk();
- }
- }
- };
- const isValidateId = async (id: string) => {
- const res = await service.validateId(id);
- if (res.status === 200) {
- if (res.result?.passed) {
- return '';
- } else {
- return res.result.reason;
- }
- } else {
- return '请输入输入正确的ICCID';
- }
- };
- return (
- <Modal
- title={props.type === 'add' ? '新增' : '编辑'}
- visible={true}
- width={600}
- onCancel={props.onCancel}
- onOk={submit}
- confirmLoading={loading}
- >
- <Form
- form={form}
- labelCol={{
- style: { width: 100 },
- }}
- layout={'vertical'}
- >
- <Form.Item
- label={'卡号'}
- name={'id'}
- required
- rules={[
- { required: true, message: '请输入卡号' },
- { max: 64, message: '最多可输入64个字符' },
- () => ({
- async validator(_, value) {
- if (value) {
- const validateId = await isValidateId(value);
- if (validateId === '') {
- return Promise.resolve();
- } else {
- return Promise.reject(new Error(`${validateId}`));
- }
- } else {
- return Promise.reject(new Error('请输入输入正确的卡号'));
- }
- },
- }),
- ]}
- >
- <Input placeholder={'请输入卡号'} disabled={props.type === 'edit'} />
- </Form.Item>
- <Form.Item
- label={'ICCID'}
- name={'iccId'}
- required
- rules={[
- { required: true, message: '请输入ICCID' },
- { max: 64, message: '最多可输入64个字符' },
- ]}
- >
- <Input placeholder={'请输入ICCID'} disabled={props.type === 'edit'} />
- </Form.Item>
- <Form.Item
- label={'平台对接'}
- name={'platformConfigId'}
- required
- rules={[{ required: true, message: '请选择平台对接' }]}
- >
- <Select
- showSearch
- placeholder={'请选择平台对接'}
- disabled={props.type === 'edit'}
- fieldNames={{ label: 'name', value: 'id' }}
- options={platformList}
- filterOption={(input, option) => {
- if (option?.name) {
- return option.name.includes(input);
- }
- return false;
- }}
- />
- </Form.Item>
- <Form.Item label={'运营商'} name={'operatorName'}>
- <Select
- showSearch
- placeholder={'请选择运营商'}
- options={OperatorList}
- filterOption={(input, option) => {
- if (option?.label) {
- return option.label.includes(input);
- }
- return false;
- }}
- />
- </Form.Item>
- <Form.Item
- label={'类型'}
- name={'cardType'}
- required
- rules={[{ required: true, message: '请选择类型' }]}
- >
- <Select
- showSearch
- placeholder={'请选择类型'}
- disabled={props.type === 'edit'}
- options={TypeList}
- filterOption={(input, option) => {
- if (option?.label) {
- return option.label.includes(input);
- }
- return false;
- }}
- />
- </Form.Item>
- <Form.Item label={'说明'} name={'describe'}>
- <Input.TextArea showCount maxLength={200} placeholder="请输入说明" />
- </Form.Item>
- </Form>
- </Modal>
- );
- };
- export default Save;
|