index.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { useEffect, useState } from 'react';
  2. import { Button, Col, Form, Input, message, Modal, Radio, Row, Select } from 'antd';
  3. import { useIntl } from 'umi';
  4. import { RadioCard, UploadImage } from '@/components';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { service } from '../index';
  7. import SaveProductModal from './SaveProduct';
  8. import type { DeviceItem } from '../typings';
  9. interface SaveProps {
  10. visible: boolean;
  11. close: () => void;
  12. reload: () => void;
  13. data?: DeviceItem;
  14. model: 'add' | 'edit';
  15. }
  16. const DefaultAccessType = 'gb28181-2016';
  17. export default (props: SaveProps) => {
  18. const { visible, close, data } = props;
  19. const intl = useIntl();
  20. const [form] = Form.useForm();
  21. const [loading, setLoading] = useState(false);
  22. const [productVisible, setProductVisible] = useState(false);
  23. const [accessType, setAccessType] = useState(DefaultAccessType);
  24. const [productList, setProductList] = useState<any[]>([]);
  25. const getProductList = async (productParams: any) => {
  26. const resp = await service.queryProductList(productParams);
  27. if (resp.status === 200) {
  28. setProductList(resp.result);
  29. }
  30. };
  31. const queryProduct = async (value: string) => {
  32. getProductList({
  33. terms: [
  34. { column: 'accessProvider', value: value },
  35. { column: 'state', value: 1 },
  36. ],
  37. });
  38. };
  39. useEffect(() => {
  40. if (visible) {
  41. if (props.model === 'edit') {
  42. form.setFieldsValue(data);
  43. const _accessType = data?.provider || DefaultAccessType;
  44. setAccessType(_accessType);
  45. queryProduct(_accessType);
  46. } else {
  47. form.setFieldsValue({
  48. provider: DefaultAccessType,
  49. });
  50. queryProduct(DefaultAccessType);
  51. setAccessType(DefaultAccessType);
  52. }
  53. }
  54. }, [visible]);
  55. const handleSave = async () => {
  56. const formData = await form.validateFields();
  57. if (formData) {
  58. const { provider, ...extraFormData } = formData;
  59. setLoading(true);
  60. const resp =
  61. provider === DefaultAccessType
  62. ? await service.saveGB(extraFormData)
  63. : await service.saveFixed(extraFormData);
  64. setLoading(false);
  65. if (resp.status === 200) {
  66. if (props.reload) {
  67. props.reload();
  68. }
  69. form.resetFields();
  70. close();
  71. message.success('操作成功');
  72. } else {
  73. message.error('操作失败');
  74. }
  75. }
  76. };
  77. const intlFormat = (
  78. id: string,
  79. defaultMessage: string,
  80. paramsID?: string,
  81. paramsMessage?: string,
  82. ) => {
  83. const paramsObj: Record<string, string> = {};
  84. if (paramsID) {
  85. const paramsMsg = intl.formatMessage({
  86. id: paramsID,
  87. defaultMessage: paramsMessage,
  88. });
  89. paramsObj.name = paramsMsg;
  90. }
  91. return intl.formatMessage(
  92. {
  93. id,
  94. defaultMessage,
  95. },
  96. paramsObj,
  97. );
  98. };
  99. console.log(productList);
  100. return (
  101. <>
  102. <Modal
  103. maskClosable={false}
  104. visible={visible}
  105. onCancel={() => {
  106. form.resetFields();
  107. close();
  108. }}
  109. width={610}
  110. title={intl.formatMessage({
  111. id: `pages.data.option.${props.model || 'add'}`,
  112. defaultMessage: '新增',
  113. })}
  114. confirmLoading={loading}
  115. onOk={handleSave}
  116. >
  117. <Form
  118. form={form}
  119. layout={'vertical'}
  120. labelCol={{
  121. style: { width: 100 },
  122. }}
  123. >
  124. <Row>
  125. <Col span={24}>
  126. <Form.Item
  127. name={'provider'}
  128. label={'接入方式'}
  129. required
  130. rules={[{ required: true, message: '请选择接入方式' }]}
  131. >
  132. <RadioCard
  133. model={'singular'}
  134. itemStyle={{ width: '50%' }}
  135. onSelect={(key) => {
  136. setAccessType(key);
  137. queryProduct(key);
  138. }}
  139. disabled={props.model === 'edit'}
  140. options={[
  141. {
  142. label: 'GB/T28181',
  143. value: DefaultAccessType,
  144. },
  145. {
  146. label: '固定地址',
  147. value: 'fixed-media',
  148. },
  149. ]}
  150. />
  151. </Form.Item>
  152. </Col>
  153. </Row>
  154. <Row>
  155. <Col span={8}>
  156. <Form.Item name={'photoUrl'}>
  157. <UploadImage />
  158. </Form.Item>
  159. </Col>
  160. <Col span={16}>
  161. <Form.Item
  162. label={'ID'}
  163. name={'id'}
  164. required
  165. rules={[{ required: true, message: '请输入ID' }, {}]}
  166. >
  167. <Input placeholder={'请输入ID'} disabled={props.model === 'edit'} />
  168. </Form.Item>
  169. <Form.Item
  170. label={'设备名称'}
  171. name={'name'}
  172. required
  173. rules={[
  174. { required: true, message: '请输入名称' },
  175. { max: 64, message: '最多可输入64个字符' },
  176. ]}
  177. >
  178. <Input placeholder={'请输入设备名称'} />
  179. </Form.Item>
  180. </Col>
  181. </Row>
  182. <Row>
  183. <Col span={24}>
  184. <Form.Item
  185. label={'所属产品'}
  186. required
  187. rules={[{ required: true, message: '请选择所属产品' }]}
  188. >
  189. <Form.Item name={'productId'} noStyle>
  190. <Select
  191. fieldNames={{
  192. label: 'name',
  193. value: 'id',
  194. }}
  195. disabled={props.model === 'edit'}
  196. options={productList}
  197. placeholder={'请选择所属产品'}
  198. style={{ width: props.model === 'edit' ? '100%' : 'calc(100% - 36px)' }}
  199. />
  200. </Form.Item>
  201. {props.model !== 'edit' && (
  202. <Form.Item noStyle>
  203. <Button
  204. type={'link'}
  205. style={{ padding: '4px 10px' }}
  206. onClick={() => {
  207. setProductVisible(true);
  208. }}
  209. >
  210. <PlusOutlined />
  211. </Button>
  212. </Form.Item>
  213. )}
  214. </Form.Item>
  215. </Col>
  216. {accessType === DefaultAccessType && (
  217. <Col span={24}>
  218. <Form.Item
  219. label={'接入密码'}
  220. name={'password'}
  221. required
  222. rules={[
  223. { required: true, message: '请输入接入密码' },
  224. { max: 64, message: '最大可输入64位' },
  225. ]}
  226. >
  227. <Input.Password placeholder={'请输入接入密码'} />
  228. </Form.Item>
  229. </Col>
  230. )}
  231. {props.model === 'edit' && (
  232. <>
  233. <Col span={24}>
  234. <Form.Item
  235. label={'流传输模式'}
  236. name={'streamMode'}
  237. required
  238. rules={[{ required: true, message: '请选择流传输模式' }]}
  239. >
  240. <Radio.Group
  241. optionType="button"
  242. buttonStyle="solid"
  243. options={[
  244. { label: 'UDP', value: 'UDP' },
  245. { label: 'TCP', value: 'TCP' },
  246. ]}
  247. />
  248. </Form.Item>
  249. </Col>
  250. <Col span={24}>
  251. <Form.Item
  252. label={'设备厂商'}
  253. name={'manufacturer'}
  254. rules={[{ max: 64, message: '最多可输入64个字符' }]}
  255. >
  256. <Input placeholder={'请输入设备厂商'} />
  257. </Form.Item>
  258. </Col>
  259. <Col span={24}>
  260. <Form.Item
  261. label={'设备型号'}
  262. name={'model'}
  263. rules={[{ max: 64, message: '最多可输入64个字符' }]}
  264. >
  265. <Input placeholder={'请输入设备型号'} />
  266. </Form.Item>
  267. </Col>
  268. <Col span={24}>
  269. <Form.Item
  270. label={'固件版本'}
  271. name={'firmware'}
  272. rules={[{ max: 64, message: '最多可输入64个字符' }]}
  273. >
  274. <Input placeholder={'请输入固件版本'} />
  275. </Form.Item>
  276. </Col>
  277. </>
  278. )}
  279. <Col span={24}>
  280. <Form.Item label={'说明'} name={'description'}>
  281. <Input.TextArea
  282. placeholder={intlFormat('pages.form.tip.input', '请输入')}
  283. rows={4}
  284. style={{ width: '100%' }}
  285. maxLength={200}
  286. showCount={true}
  287. />
  288. </Form.Item>
  289. </Col>
  290. </Row>
  291. <Form.Item name={'id'} hidden>
  292. <Input />
  293. </Form.Item>
  294. </Form>
  295. </Modal>
  296. <SaveProductModal
  297. visible={productVisible}
  298. type={accessType}
  299. close={() => {
  300. setProductVisible(false);
  301. }}
  302. reload={(productId: string, name: string) => {
  303. form.setFieldsValue({ productId });
  304. productList.push({
  305. id: productId,
  306. name,
  307. });
  308. setProductList([...productList]);
  309. }}
  310. />
  311. </>
  312. );
  313. };