index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { TableCard } from '@/components';
  2. import SearchComponent from '@/components/SearchComponent';
  3. import { getMenuPathByCode, MENUS_CODE } from '@/utils/menu';
  4. import { StatusColorEnum } from '@/components/BadgeStatus';
  5. import { PageContainer } from '@ant-design/pro-layout';
  6. import type { ProColumns } from '@jetlinks/pro-table';
  7. import { Badge, Button, Card, Col, Empty, message, Pagination, Popconfirm, Row } from 'antd';
  8. import { useEffect, useState } from 'react';
  9. import { useHistory } from 'umi';
  10. import styles from './index.less';
  11. import Service from './service';
  12. import { CheckCircleOutlined, DeleteOutlined, EditOutlined, StopOutlined } from '@ant-design/icons';
  13. const defaultImage = require('/public/images/device-access.png');
  14. export const service = new Service('gateway/device');
  15. const AccessConfig = () => {
  16. const history = useHistory();
  17. const [param, setParam] = useState<any>({ pageSize: 10 });
  18. const columns: ProColumns<any>[] = [
  19. {
  20. title: '名称',
  21. dataIndex: 'name',
  22. },
  23. {
  24. title: '状态',
  25. dataIndex: 'state',
  26. valueType: 'select',
  27. valueEnum: {
  28. disabled: {
  29. text: '已停止',
  30. status: 'disabled',
  31. },
  32. enabled: {
  33. text: '已启动',
  34. status: 'enabled',
  35. },
  36. },
  37. },
  38. {
  39. title: '说明',
  40. dataIndex: 'description',
  41. },
  42. ];
  43. const [dataSource, setDataSource] = useState<any>({
  44. data: [],
  45. pageSize: 10,
  46. pageIndex: 0,
  47. total: 0,
  48. });
  49. const handleSearch = (params: any) => {
  50. setParam(params);
  51. service
  52. .queryList({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  53. .then((resp) => {
  54. if (resp.status === 200) {
  55. setDataSource(resp.result);
  56. }
  57. });
  58. };
  59. useEffect(() => {
  60. handleSearch(param);
  61. }, []);
  62. return (
  63. <PageContainer>
  64. <Card>
  65. <SearchComponent
  66. field={columns}
  67. // pattern={'simple'}
  68. enableSave={false}
  69. onSearch={(data: any) => {
  70. const dt = {
  71. pageSize: 10,
  72. terms: [...data?.terms],
  73. };
  74. handleSearch(dt);
  75. }}
  76. />
  77. <div style={{ width: '100%', display: 'flex', justifyContent: 'flex-end' }}>
  78. <Button
  79. type="primary"
  80. onClick={() => {
  81. history.push(`${getMenuPathByCode(MENUS_CODE['link/AccessConfig/Detail'])}`);
  82. }}
  83. >
  84. 新增
  85. </Button>
  86. </div>
  87. {dataSource?.data.length > 0 ? (
  88. <Row gutter={[16, 16]} style={{ marginTop: 10 }}>
  89. {(dataSource?.data || []).map((item: any) => (
  90. <Col key={item.name} span={12}>
  91. <TableCard
  92. showMask={false}
  93. actions={[
  94. <Button
  95. key="edit"
  96. type="link"
  97. onClick={() => {
  98. history.push(
  99. `${getMenuPathByCode(MENUS_CODE['link/AccessConfig/Detail'])}?id=${
  100. item.id
  101. }`,
  102. );
  103. }}
  104. >
  105. <EditOutlined />
  106. 编辑
  107. </Button>,
  108. <Button key="warning" type="link">
  109. <Popconfirm
  110. title={`确认${item.state.value !== 'disabled' ? '禁用' : '启用'}`}
  111. onConfirm={() => {
  112. if (item.state.value !== 'disabled') {
  113. service.shutDown(item.id).then((resp) => {
  114. if (resp.status === 200) {
  115. message.success('操作成功!');
  116. handleSearch(param);
  117. }
  118. });
  119. } else {
  120. service.startUp(item.id).then((resp) => {
  121. if (resp.status === 200) {
  122. message.success('操作成功!');
  123. handleSearch(param);
  124. }
  125. });
  126. }
  127. }}
  128. >
  129. {item.state.value !== 'disabled' ? (
  130. <span>
  131. <StopOutlined />
  132. 禁用
  133. </span>
  134. ) : (
  135. <span>
  136. <CheckCircleOutlined />
  137. 启用
  138. </span>
  139. )}
  140. </Popconfirm>
  141. </Button>,
  142. <Button key="delete" type="link">
  143. <Popconfirm
  144. title={'确认删除?'}
  145. onConfirm={() => {
  146. service.remove(item.id).then((resp: any) => {
  147. if (resp.status === 200) {
  148. message.success('操作成功!');
  149. handleSearch(param);
  150. }
  151. });
  152. }}
  153. >
  154. <DeleteOutlined />
  155. 删除
  156. </Popconfirm>
  157. </Button>,
  158. ]}
  159. status={item.state.value}
  160. statusText={item.state.text}
  161. statusNames={{
  162. enabled: StatusColorEnum.processing,
  163. disabled: StatusColorEnum.error,
  164. }}
  165. >
  166. <div className={styles.context}>
  167. <div>
  168. <img width={88} height={88} src={defaultImage} alt={''} />
  169. </div>
  170. <div className={styles.card}>
  171. <div className={styles.header}>
  172. <div className={styles.title}>{item.name || '--'}</div>
  173. <div className={styles.desc}>{item.description || '--'}</div>
  174. </div>
  175. <div className={styles.container}>
  176. <div className={styles.server}>
  177. <div className={styles.subTitle}>{item?.channelInfo?.name || '--'}</div>
  178. <p>
  179. {item.channelInfo?.addresses.map((i: any) => (
  180. <div key={i.address}>
  181. <Badge color={i.health === -1 ? 'red' : 'green'} text={i.address} />
  182. </div>
  183. ))}
  184. </p>
  185. </div>
  186. <div className={styles.procotol}>
  187. <div className={styles.subTitle}>
  188. {item?.protocolDetail?.name || '--'}
  189. </div>
  190. <p>{item.protocolDetail?.description || '--'}</p>
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. </TableCard>
  196. </Col>
  197. ))}
  198. </Row>
  199. ) : (
  200. <Empty />
  201. )}
  202. {dataSource.data.length > 0 && (
  203. <div style={{ display: 'flex', marginTop: 20, justifyContent: 'flex-end' }}>
  204. <Pagination
  205. showSizeChanger
  206. size="small"
  207. className={'pro-table-card-pagination'}
  208. total={dataSource?.total || 0}
  209. current={dataSource?.pageIndex + 1}
  210. onChange={(page, size) => {
  211. handleSearch({
  212. ...param,
  213. pageIndex: page - 1,
  214. pageSize: size,
  215. });
  216. }}
  217. pageSizeOptions={[10, 20, 50, 100]}
  218. pageSize={dataSource?.pageSize}
  219. showTotal={(num) => {
  220. const minSize = dataSource?.pageIndex * dataSource?.pageSize + 1;
  221. const MaxSize = (dataSource?.pageIndex + 1) * dataSource?.pageSize;
  222. return `第 ${minSize} - ${MaxSize > num ? num : MaxSize} 条/总共 ${num} 条`;
  223. }}
  224. />
  225. </div>
  226. )}
  227. </Card>
  228. </PageContainer>
  229. );
  230. };
  231. export default AccessConfig;