index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import React, { useEffect, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-components';
  3. import { Button, Card, Form, Input, message, Space, Table, Tag } from 'antd';
  4. import type { ColumnsType } from 'antd/es/table';
  5. import { queryDeviceOperations, queryDeviceOperationsData } from '@/services/afterSales/afterSales';
  6. import DeviceStatusData from './deviceStatusData';
  7. import DeviceHistory from '@/pages/AfterSales/AfterSalesManagement/deviceHistory';
  8. import { DownloadOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  9. import Overwrite from '@/pages/home/overwrite';
  10. import { queryAfterSalesExport, queryOtaCheck, queryOtaUpdate } from '@/services/reportHome';
  11. interface DataType {
  12. home_name: string;
  13. user_name: string;
  14. device_code: string;
  15. is_online: boolean;
  16. power: number;
  17. mode: number;
  18. speed: number;
  19. set_temp: number;
  20. temperature: number;
  21. humidity: number;
  22. fan_voltage: number;
  23. record_id: string;
  24. }
  25. const BannerManagement: React.FC = () => {
  26. const [dataList, setDataList] = useState([]);
  27. const [loading, setLoading] = useState(false);
  28. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  29. const [historyVisible, setHistoryVisible] = useState(false);
  30. const [historyData, setHistoryData] = useState(null);
  31. const [statusVisible, setStatusVisible] = useState(false);
  32. const [statusData, setStatusData] = useState(null);
  33. const [form] = Form.useForm();
  34. const [searchData, setSearchData] = useState<object | null>({});
  35. const [visible, setVisible] = useState(false);
  36. const [editData, setEditData] = useState(null);
  37. const getList = () => {
  38. const params = {
  39. q: 'page',
  40. current: pagination.current,
  41. pageSize: pagination.pageSize,
  42. ...searchData,
  43. };
  44. queryDeviceOperations(params).then((res) => {
  45. if (res && res.code === 0) {
  46. setDataList(res.data.list || []);
  47. setPagination(res.data.pagination);
  48. setLoading(false);
  49. }
  50. });
  51. };
  52. // 切换分页
  53. const tableChange = (page: any) => {
  54. setLoading(true);
  55. const params = {
  56. q: 'page',
  57. current: page.current,
  58. pageSize: page.pageSize,
  59. ...searchData,
  60. };
  61. queryDeviceOperations(params).then((res) => {
  62. if (res && res.code === 0) {
  63. setDataList(res.data.list || []);
  64. setPagination(res.data.pagination);
  65. setLoading(false);
  66. }
  67. });
  68. };
  69. useEffect(() => {
  70. getList();
  71. }, []);
  72. useEffect(() => {
  73. getList();
  74. }, [searchData]);
  75. // 设备运行历史
  76. const onHistory = (record: any) => {
  77. setHistoryVisible(true);
  78. setHistoryData(record);
  79. };
  80. // 设备运行历史回调
  81. const onHistoryCallback = () => {
  82. setHistoryVisible(false);
  83. };
  84. // 24小时设备状态数据
  85. const onDeviceOperations = (record: any) => {
  86. queryDeviceOperationsData({ device_id: record.device_code }).then((res) => {
  87. if (res && res.code === 0) {
  88. const list = res.data.list || [];
  89. const result = list.reduce(
  90. (acc: Record<string, Record<string, any>>, { label, value, time }: any) => {
  91. if (!acc[time]) {
  92. // 如果不存在,则初始化一个新对象
  93. acc[time] = {};
  94. }
  95. // 将当前的 label 和 value 添加到对应的时间对象中
  96. acc[time][label] = value;
  97. return acc;
  98. },
  99. {},
  100. );
  101. const finalResult: any = Object.entries(result).map(([time, values]: any) => ({
  102. time,
  103. ...values,
  104. }));
  105. setStatusData(finalResult);
  106. setStatusVisible(true);
  107. }
  108. });
  109. };
  110. // 24小时设备状态数据回调
  111. const onStatusCallback = () => {
  112. setStatusVisible(false);
  113. };
  114. // 搜索
  115. const onFinish = () => {
  116. form.validateFields().then((data) => {
  117. setLoading(true);
  118. setSearchData(data);
  119. });
  120. };
  121. // 重置
  122. const onReset = () => {
  123. form.resetFields();
  124. setLoading(true);
  125. setSearchData(null);
  126. };
  127. // 下发
  128. const onOverwrite = (data: any) => {
  129. setVisible(true);
  130. setEditData(data);
  131. };
  132. // 下发弹框回调
  133. const overwriteCallback = () => {
  134. setVisible(false);
  135. getList();
  136. };
  137. // 导出
  138. const onExport = () => {
  139. form.validateFields().then((data) => {
  140. queryAfterSalesExport(data)
  141. .then((res: any) => {
  142. const link = document.createElement('a');
  143. link.style.display = 'none';
  144. link.href = window.URL.createObjectURL(new Blob([res]));
  145. link.setAttribute('download', '售后管理列表.xlsx');
  146. document.body.appendChild(link);
  147. link.click();
  148. document.body.removeChild(link);
  149. })
  150. .catch(() => {
  151. message.error('导出失败');
  152. });
  153. });
  154. };
  155. // 升级
  156. const onUpgrade = (v: any) => {
  157. const params = {
  158. device_code: v.device_code,
  159. device_type_id: v.device_type,
  160. };
  161. queryOtaCheck(params)
  162. .then((res) => {
  163. if (res && res.code === 0) {
  164. if (res.data.need_update) {
  165. // 需要升级
  166. queryOtaUpdate(params)
  167. .then((item) => {
  168. if (item && item.code === 0) {
  169. message.success('升级成功');
  170. } else {
  171. message.error(res.message || '报错了');
  172. }
  173. })
  174. .catch((e) => {
  175. if (e && e.message) {
  176. message.error(e.message || '报错了');
  177. }
  178. });
  179. } else {
  180. message.success('已是最新版本!');
  181. }
  182. } else {
  183. message.error(res.message || '报错了');
  184. }
  185. })
  186. .catch((e) => {
  187. if (e && e.message) {
  188. message.error(e.message || '报错了');
  189. }
  190. });
  191. };
  192. const columns: ColumnsType<DataType> = [
  193. {
  194. title: '序号',
  195. align: 'center',
  196. key: 'index',
  197. render: (_: any, _row: any, index: number) => index + 1,
  198. },
  199. {
  200. title: '家名称',
  201. dataIndex: 'home_name',
  202. key: 'home_name',
  203. },
  204. {
  205. title: '用户名',
  206. dataIndex: 'user_name',
  207. key: 'user_name',
  208. },
  209. {
  210. title: '手机号',
  211. dataIndex: 'phone',
  212. key: 'phone',
  213. },
  214. {
  215. title: '地区',
  216. dataIndex: 'address',
  217. key: 'address',
  218. },
  219. {
  220. title: '设备编号',
  221. dataIndex: 'device_code',
  222. key: 'device_code',
  223. },
  224. {
  225. title: '设备类型',
  226. dataIndex: 'device_type_name',
  227. key: 'device_type_name',
  228. },
  229. {
  230. title: '是否在线',
  231. dataIndex: 'is_online',
  232. key: 'is_online',
  233. render: (_, record: any) => {
  234. return (
  235. <Tag color={record.is_online ? 'green' : 'red'}>{record.is_online ? '在线' : '离线'}</Tag>
  236. );
  237. },
  238. },
  239. {
  240. title: '当前开关状态',
  241. dataIndex: 'power',
  242. key: 'power',
  243. render: (_, record: any) => {
  244. return (
  245. <Tag color={{ 0: 'red', 1: 'green' }[record.power]}>
  246. {{ 0: '关', 1: '开' }[record.power]}
  247. </Tag>
  248. );
  249. },
  250. },
  251. {
  252. title: '当前模式',
  253. dataIndex: 'mode',
  254. key: 'mode',
  255. render: (_, record: any) => {
  256. return (
  257. <span>{{ 0: '制冷', 1: '制热', 2: '除湿', 3: '送风', 4: '加湿' }[record.mode]}</span>
  258. );
  259. },
  260. },
  261. {
  262. title: '风速',
  263. dataIndex: 'speed',
  264. key: 'speed',
  265. render: (_, record: any) => {
  266. return (
  267. <span>
  268. {{ 1: '一档', 2: '二档', 3: '三档', 4: '四档', 5: '五档', 6: '六档' }[record.speed]}
  269. </span>
  270. );
  271. },
  272. },
  273. {
  274. title: '设定温度',
  275. dataIndex: 'set_temp',
  276. key: 'set_temp',
  277. render: (_, record: any) => {
  278. return `${record.set_temp}℃`;
  279. },
  280. },
  281. {
  282. title: '温度',
  283. dataIndex: 'temperature',
  284. key: 'temperature',
  285. render: (_, record: any) => {
  286. return `${record.temperature}℃`;
  287. },
  288. },
  289. {
  290. title: '湿度',
  291. dataIndex: 'humidity',
  292. key: 'humidity',
  293. },
  294. {
  295. title: '电压值',
  296. dataIndex: 'fan_voltage',
  297. key: 'fan_voltage',
  298. },
  299. {
  300. title: '操作',
  301. key: 'action',
  302. render: (_, record) => (
  303. <Space size="middle">
  304. <a
  305. onClick={() => {
  306. onHistory(record);
  307. }}
  308. >
  309. 设备运行历史
  310. </a>
  311. <a
  312. onClick={() => {
  313. onDeviceOperations(record);
  314. }}
  315. >
  316. 24小时设备状态数据
  317. </a>
  318. <a
  319. onClick={() => {
  320. onOverwrite(record);
  321. }}
  322. >
  323. 远程配置
  324. </a>
  325. <a
  326. onClick={() => {
  327. onUpgrade(record);
  328. }}
  329. >
  330. 升级
  331. </a>
  332. </Space>
  333. ),
  334. },
  335. ];
  336. return (
  337. <PageContainer>
  338. <Card>
  339. <Form form={form} layout="inline" onFinish={onFinish} style={{ padding: '20px' }}>
  340. <Form.Item name="home_name" label="家名称">
  341. <Input placeholder="请输入家名称" />
  342. </Form.Item>
  343. <Form.Item name="user_name" label="用户名">
  344. <Input placeholder="请输入用户名" />
  345. </Form.Item>
  346. <Form.Item name="device_id" label="设备编号 ">
  347. <Input placeholder="请输入设备编号" />
  348. </Form.Item>
  349. <Form.Item style={{ marginBottom: '10px' }}>
  350. <Space>
  351. <Button type="primary" htmlType="submit">
  352. <SearchOutlined />
  353. 查询
  354. </Button>
  355. <Button htmlType="button" onClick={onReset}>
  356. <ReloadOutlined />
  357. 重置
  358. </Button>
  359. <Button type="dashed" onClick={onExport}>
  360. <DownloadOutlined />
  361. 导出
  362. </Button>
  363. </Space>
  364. </Form.Item>
  365. </Form>
  366. <Table
  367. columns={columns}
  368. dataSource={dataList}
  369. rowKey={(record) => record.record_id}
  370. pagination={pagination}
  371. loading={loading}
  372. onChange={tableChange}
  373. />
  374. {historyVisible && (
  375. <DeviceHistory
  376. visible={historyVisible}
  377. params={historyData}
  378. editCallback={onHistoryCallback}
  379. />
  380. )}
  381. {statusVisible && (
  382. <DeviceStatusData
  383. params={statusData}
  384. visible={statusVisible}
  385. editCallback={onStatusCallback}
  386. />
  387. )}
  388. {visible && (
  389. <Overwrite visible={visible} editCallback={overwriteCallback} params={editData} />
  390. )}
  391. </Card>
  392. </PageContainer>
  393. );
  394. };
  395. export default BannerManagement;