index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { PageContainer } from '@ant-design/pro-components';
  2. import { Button, Card, Form, Input, Space, Table } from 'antd';
  3. import React, { useEffect, useState } from 'react';
  4. import { ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  5. import { history } from 'umi';
  6. import type { ColumnsType } from 'antd/es/table';
  7. import { queryHomeList } from '@/services/home';
  8. import Overwrite from '@/pages/home/overwrite';
  9. interface DataType {
  10. name: string;
  11. member: any[];
  12. power: number;
  13. record_id: string;
  14. }
  15. /**
  16. * 家列表
  17. * @constructor
  18. */
  19. const Home: React.FC = () => {
  20. const [form] = Form.useForm();
  21. const [loading, setLoading] = useState(false);
  22. const [searchData, setSearchData] = useState<object | null>({});
  23. const [dataList, setDataList] = useState([]);
  24. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  25. const [visible, setVisible] = useState(false);
  26. const [editData, setEditData] = useState(null);
  27. const getData = () => {
  28. const params = {
  29. q: 'page',
  30. current: pagination.current,
  31. pageSize: pagination.pageSize,
  32. ...searchData,
  33. };
  34. queryHomeList(params).then((res) => {
  35. if (res.code === 0) {
  36. setDataList(res.data.list);
  37. setPagination(res.data.pagination);
  38. setLoading(false);
  39. }
  40. });
  41. };
  42. useEffect(() => {
  43. setLoading(true);
  44. getData();
  45. }, []);
  46. useEffect(() => {
  47. getData();
  48. }, [searchData]);
  49. // 查询
  50. const onFinish = () => {
  51. form.validateFields().then((data) => {
  52. setLoading(true);
  53. setSearchData(data);
  54. });
  55. };
  56. // 重置
  57. const onReset = () => {
  58. form.resetFields();
  59. setLoading(true);
  60. setSearchData(null);
  61. };
  62. // table切换
  63. const tableChange = (page: any) => {
  64. setLoading(true);
  65. const param = {
  66. q: 'page',
  67. current: page.current,
  68. pageSize: page.pageSize,
  69. ...searchData,
  70. };
  71. queryHomeList(param).then((res) => {
  72. if (res.code === 0) {
  73. setDataList(res.data.list);
  74. setPagination(res.data.pagination);
  75. setLoading(false);
  76. }
  77. });
  78. };
  79. // 下发
  80. const onOverwrite = (data: any) => {
  81. setVisible(true);
  82. setEditData(data);
  83. };
  84. // 下发弹框回调
  85. const overwriteCallback = () => {
  86. setVisible(false);
  87. getData();
  88. };
  89. // 跳转到房间
  90. const toRoom = (record: DataType) => {
  91. history.push({ pathname: '/roomList', state: record.record_id });
  92. };
  93. const columns: ColumnsType<DataType> = [
  94. {
  95. title: '序号',
  96. align: 'center',
  97. key: 'index',
  98. render: (_: any, row: any, index: number) => index + 1,
  99. },
  100. {
  101. title: '名称',
  102. width: 200,
  103. dataIndex: 'name',
  104. key: 'name',
  105. },
  106. {
  107. title: '设备编号',
  108. width: 100,
  109. dataIndex: 'gateway',
  110. key: 'gateway',
  111. },
  112. {
  113. title: '设备是否在线',
  114. dataIndex: 'is_online',
  115. key: 'is_online',
  116. render: (v) => <span>{v ? '在线' : '离线'}</span>,
  117. },
  118. {
  119. title: '成员人数',
  120. dataIndex: 'member',
  121. key: 'member',
  122. render: (v) => v && <span>{v.length}</span>,
  123. },
  124. {
  125. title: '地区',
  126. dataIndex: 'district',
  127. key: 'district',
  128. },
  129. {
  130. title: '操作',
  131. key: 'action',
  132. render: (_, record) => (
  133. <Space size="middle">
  134. <a
  135. onClick={() => {
  136. toRoom(record);
  137. }}
  138. >
  139. 查看房间
  140. </a>
  141. <a
  142. onClick={() => {
  143. onOverwrite(record);
  144. }}
  145. >
  146. 远程配置
  147. </a>
  148. </Space>
  149. ),
  150. },
  151. ];
  152. const paginationProps = {
  153. showSizeChanger: true,
  154. showQuickJumper: true,
  155. showTotal: (total: number) => {
  156. return <span> 共 {total}条 </span>;
  157. },
  158. ...pagination,
  159. };
  160. return (
  161. <PageContainer>
  162. <Card>
  163. <Form form={form} layout="inline" onFinish={onFinish}>
  164. <Form.Item name="like_name" label="名称">
  165. <Input placeholder="请输入名称" />
  166. </Form.Item>
  167. <Form.Item name="like_gateway" label="设备编号">
  168. <Input placeholder="请输入设备编号" />
  169. </Form.Item>
  170. <Form.Item style={{ marginBottom: '10px' }}>
  171. <Space>
  172. <Button type="primary" htmlType="submit">
  173. <SearchOutlined />
  174. 查询
  175. </Button>
  176. <Button htmlType="button" onClick={onReset}>
  177. <ReloadOutlined />
  178. 重置
  179. </Button>
  180. </Space>
  181. </Form.Item>
  182. </Form>
  183. <Table
  184. columns={columns}
  185. dataSource={dataList}
  186. rowKey={(record: any) => record.record_id}
  187. pagination={paginationProps}
  188. loading={loading}
  189. onChange={tableChange}
  190. style={{ marginTop: '20px' }}
  191. />
  192. {visible && (
  193. <Overwrite visible={visible} editCallback={overwriteCallback} params={editData} />
  194. )}
  195. </Card>
  196. </PageContainer>
  197. );
  198. };
  199. export default Home;