index.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { PageContainer } from '@ant-design/pro-components';
  2. import React, { useEffect, useState } from 'react';
  3. import { Button, Card, Form, Input, Select, Space, Table } from 'antd';
  4. import { ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  5. import type { ColumnsType } from 'antd/es/table';
  6. import { history } from 'umi';
  7. import { queryRoomList } from '@/services/home';
  8. interface DataType {
  9. control_number: number;
  10. home_name: string;
  11. name: string;
  12. type: string;
  13. temperature: number;
  14. humidity: number;
  15. is_master: number;
  16. power: number;
  17. status: number;
  18. record_id: string;
  19. }
  20. /**
  21. * 房间列表
  22. * @constructor
  23. */
  24. const Room: React.FC = () => {
  25. const [form] = Form.useForm();
  26. const [loading, setLoading] = useState(false);
  27. const [searchData, setSearchData] = useState<object | null>({});
  28. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  29. const [dataList, setDataList] = useState([]);
  30. // 获取列表数据秀
  31. const getRoomList = () => {
  32. const { state } = history.location;
  33. const params = {
  34. q: 'page',
  35. home_id: state,
  36. current: pagination.current,
  37. pageSize: pagination.pageSize,
  38. ...searchData,
  39. };
  40. queryRoomList(params).then((res) => {
  41. if (res.code === 0) {
  42. setDataList(res.data.list);
  43. setPagination(res.data.pagination);
  44. setLoading(false);
  45. }
  46. });
  47. };
  48. useEffect(() => {
  49. getRoomList();
  50. }, []);
  51. useEffect(() => {
  52. getRoomList();
  53. }, [searchData]);
  54. const onFinish = () => {
  55. form.validateFields().then((data) => {
  56. setLoading(true);
  57. setSearchData(data);
  58. });
  59. };
  60. // 重置
  61. const onReset = () => {
  62. form.resetFields();
  63. setLoading(true);
  64. setSearchData(null);
  65. };
  66. // table切换
  67. const tableChange = (page: any) => {
  68. setLoading(true);
  69. const { state } = history.location;
  70. const param = {
  71. q: 'page',
  72. current: page.current,
  73. pageSize: page.pageSize,
  74. home_id: state,
  75. ...searchData,
  76. };
  77. queryRoomList(param).then((res) => {
  78. if (res.code === 0) {
  79. setDataList(res.data.list);
  80. setPagination(res.data.pagination);
  81. setLoading(false);
  82. }
  83. });
  84. };
  85. const columns: ColumnsType<DataType> = [
  86. {
  87. title: '序号',
  88. align: 'center',
  89. key: 'index',
  90. render: (_: any, row: any, index: number) => index + 1,
  91. },
  92. {
  93. title: '分控编号',
  94. dataIndex: 'control_number',
  95. key: 'control_number',
  96. },
  97. {
  98. title: '家名称',
  99. dataIndex: 'home_name',
  100. key: 'home_name',
  101. },
  102. {
  103. title: '房间名称',
  104. dataIndex: 'name',
  105. key: 'name',
  106. },
  107. {
  108. title: '温度',
  109. dataIndex: 'temperature',
  110. key: 'temperature',
  111. },
  112. {
  113. title: '设定温度',
  114. dataIndex: 'set_temp',
  115. key: 'set_temp',
  116. },
  117. {
  118. title: '湿度',
  119. dataIndex: 'humidity',
  120. key: 'humidity',
  121. },
  122. {
  123. title: '是否为主控',
  124. dataIndex: 'is_master',
  125. key: 'is_master',
  126. render: (v) => <span>{v ? '是' : '否'}</span>,
  127. },
  128. {
  129. title: '模式',
  130. dataIndex: 'mode',
  131. key: 'mode',
  132. render: (v) => <span>{{ 0: '制冷', 1: '制热', 2: '除湿', 3: '送风', 4: '加湿' }[v]}</span>,
  133. },
  134. {
  135. title: '风速',
  136. dataIndex: 'fan_speed',
  137. key: 'fan_speed',
  138. },
  139. {
  140. title: '状态',
  141. dataIndex: 'power',
  142. key: 'power',
  143. render: (v) => <span>{v ? '开' : '关'}</span>,
  144. },
  145. ];
  146. const paginationProps = {
  147. showSizeChanger: true,
  148. showQuickJumper: true,
  149. showTotal: (total: number) => {
  150. return <span> 共 {total}条 </span>;
  151. },
  152. ...pagination,
  153. };
  154. return (
  155. <PageContainer>
  156. <Card>
  157. <Form form={form} layout="inline" onFinish={onFinish}>
  158. <Form.Item name="name" label="名称">
  159. <Input placeholder="请输入姓名" />
  160. </Form.Item>
  161. <Form.Item name="status" label="状态">
  162. <Select style={{ width: '175px' }} placeholder="请选择状态">
  163. <Select.Option key={1} value={1}>
  164. 在线
  165. </Select.Option>
  166. <Select.Option key={2} value={2}>
  167. 离线
  168. </Select.Option>
  169. </Select>
  170. </Form.Item>
  171. <Form.Item style={{ marginBottom: '10px' }}>
  172. <Space>
  173. <Button type="primary" htmlType="submit">
  174. <SearchOutlined />
  175. 查询
  176. </Button>
  177. <Button htmlType="button" onClick={onReset}>
  178. <ReloadOutlined />
  179. 重置
  180. </Button>
  181. <Button
  182. onClick={() => {
  183. history.goBack();
  184. }}
  185. >
  186. 返回
  187. </Button>
  188. </Space>
  189. </Form.Item>
  190. </Form>
  191. <Table
  192. columns={columns}
  193. dataSource={dataList}
  194. rowKey={(record: any) => record.record_id}
  195. pagination={paginationProps}
  196. loading={loading}
  197. onChange={tableChange}
  198. style={{ marginTop: '20px' }}
  199. />
  200. </Card>
  201. </PageContainer>
  202. );
  203. };
  204. export default Room;