GatewayList.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <script setup lang="ts">
  2. import { onMounted, ref, useTemplateRef } from 'vue';
  3. import ModalGuidance from '@/layout/ModalGuidance.vue';
  4. import ConfirmModal from '@/components/ConfirmModal.vue';
  5. import SvgIcon from '@/components/SvgIcon.vue';
  6. import { useRequest } from '@/hooks/request';
  7. import { t } from '@/i18n';
  8. import {
  9. addGatewayLinkBatchUpdate,
  10. gatewayList,
  11. getGatewayLinks,
  12. obtainListInterfaces,
  13. obtainListPhysicalInterfaces,
  14. orgGatewayUnregister,
  15. } from '@/api';
  16. import RegisterGateway from '../register-gateway/RegisterGateway.vue';
  17. import type { DefaultOptionType, SelectValue } from 'ant-design-vue/es/select';
  18. import type {
  19. BatchUpdate,
  20. GatewayListItem,
  21. GatewayQuery,
  22. InterfaceData,
  23. InterfaceLsit,
  24. ListInterfaces,
  25. ListPhysicalInterfaces,
  26. } from '@/types';
  27. const gatewayColumns = [
  28. {
  29. title: t('common.serialNumber'),
  30. dataIndex: 'index',
  31. key: 'index',
  32. },
  33. {
  34. title: t('common.sequenceNumber'),
  35. dataIndex: 'snCode',
  36. key: 'snCode',
  37. ellipsis: true,
  38. },
  39. {
  40. title: t('createDevice.modelNumber'),
  41. dataIndex: 'modelName',
  42. key: 'modelName',
  43. ellipsis: true,
  44. },
  45. {
  46. title: t('registerGateway.onlineStatus'),
  47. dataIndex: 'state',
  48. key: 'state',
  49. ellipsis: true,
  50. },
  51. {
  52. title: t('common.operation'),
  53. dataIndex: 'action',
  54. key: 'action',
  55. width: 80,
  56. },
  57. ];
  58. const agreementColumns = [
  59. // {
  60. // title: t('registerGateway.stationNumber'),
  61. // dataIndex: 'station',
  62. // key: 'station',
  63. // ellipsis: true,
  64. // },
  65. {
  66. title: t('registerGateway.agreement'),
  67. dataIndex: 'protocolName',
  68. key: 'protocolName',
  69. ellipsis: true,
  70. },
  71. {
  72. title: t('registerGateway.associatedEquipment'),
  73. dataIndex: 'deviceName',
  74. key: 'deviceName',
  75. ellipsis: true,
  76. },
  77. ];
  78. const gatewayData = ref<GatewayListItem[]>([]);
  79. const interfaceList = ref<InterfaceLsit[]>([]);
  80. const interfaceOriginalData = ref<InterfaceLsit[]>([]);
  81. const gatewayId = ref<number>();
  82. const gatewayEditor = ref<boolean>(false);
  83. const interfaceSelectList = ref<ListInterfaces[]>([]);
  84. const listPhysicalInterfaces = ref<ListPhysicalInterfaces[]>([]);
  85. const gatewayLinks = ref<BatchUpdate[]>([]);
  86. // 添加选中状态
  87. const selectedRowId = ref<string | null>(null);
  88. const modalGuidanceRef = useTemplateRef('modalGuidance');
  89. const activeKey = ref();
  90. const gatewayQuery = ref<GatewayQuery>({
  91. pageIndex: 1,
  92. pageSize: 10,
  93. searchContent: '',
  94. state: -1,
  95. total: 0,
  96. });
  97. const modalComponentRef = useTemplateRef('modalComponent');
  98. const { handleRequest } = useRequest();
  99. const getGatewayList = () => {
  100. handleRequest(async () => {
  101. const { records, total } = await gatewayList(gatewayQuery.value);
  102. gatewayData.value = records;
  103. gatewayQuery.value.total = total;
  104. if (records.length) {
  105. getObtainListInterfaces(gatewayData.value[0].modelId);
  106. postLinkGetList(gatewayData.value[0].id);
  107. selectedRowId.value = String(gatewayData.value[0].id);
  108. } else {
  109. interfaceList.value = [];
  110. }
  111. });
  112. };
  113. const addGatewayList = () => {
  114. gatewayQuery.value.pageIndex = 1;
  115. getGatewayList();
  116. };
  117. const addReset = () => {
  118. gatewayQuery.value.searchContent = '';
  119. gatewayQuery.value.state = -1;
  120. addGatewayList();
  121. };
  122. const rowClick = (record: GatewayListItem) => {
  123. return {
  124. onClick: () => {
  125. // 更新选中行ID
  126. selectedRowId.value = String(record.id);
  127. gatewayId.value = record.id;
  128. getObtainListInterfaces(record.modelId);
  129. postLinkGetList(record.id);
  130. },
  131. };
  132. };
  133. // 动态行类名
  134. const rowClassName = (record: GatewayListItem) => {
  135. return String(record.id) === selectedRowId.value ? 'selected-row' : '';
  136. };
  137. const getObtainListInterfaces = (id: number) => {
  138. handleRequest(async () => {
  139. interfaceSelectList.value = await obtainListInterfaces(id);
  140. });
  141. };
  142. const getObtainListPhysicalInterfaces = (id: number, value?: InterfaceData) => {
  143. handleRequest(async () => {
  144. listPhysicalInterfaces.value = await obtainListPhysicalInterfaces(id);
  145. if (value) {
  146. if (listPhysicalInterfaces.value.length) {
  147. value.protocolType = listPhysicalInterfaces.value[0].protocolName;
  148. } else {
  149. value.protocolType = '';
  150. }
  151. }
  152. });
  153. };
  154. const choosePhysicalInterface = (
  155. selectValue: SelectValue,
  156. option: DefaultOptionType,
  157. id: number,
  158. value: InterfaceData,
  159. index: number,
  160. ) => {
  161. interfaceList.value[index].interfaceType = option.interfaceType;
  162. interfaceList.value[index].linkName = option.name;
  163. getObtainListPhysicalInterfaces(id, value);
  164. };
  165. const postLinkGetList = (id: number) => {
  166. handleRequest(async () => {
  167. interfaceOriginalData.value = await getGatewayLinks(id);
  168. if (interfaceOriginalData.value.length) {
  169. interfaceList.value = interfaceOriginalData.value;
  170. activeKey.value = interfaceList.value[0].id;
  171. getObtainListPhysicalInterfaces(interfaceList.value[0].interfaceId);
  172. } else {
  173. interfaceList.value = [];
  174. }
  175. });
  176. };
  177. const switchPages = () => {
  178. getGatewayList();
  179. };
  180. const refreshData = () => {
  181. gatewayQuery.value.pageIndex = 1;
  182. getGatewayList();
  183. };
  184. const confirm = () => {
  185. handleRequest(async () => {
  186. if (gatewayId.value) {
  187. await orgGatewayUnregister(gatewayId.value);
  188. modalComponentRef.value?.hideView();
  189. getGatewayList();
  190. }
  191. });
  192. };
  193. const addGatewayDelete = (id: number) => {
  194. gatewayId.value = id;
  195. modalComponentRef.value?.showView();
  196. };
  197. const cancelSave = () => {
  198. gatewayEditor.value = false;
  199. if (gatewayId.value) {
  200. postLinkGetList(gatewayId.value);
  201. }
  202. };
  203. const addSave = () => {
  204. handleRequest(async () => {
  205. interfaceList.value.forEach((item) => {
  206. const {
  207. id,
  208. linkName,
  209. gatewayId,
  210. interfaceId,
  211. protocolType,
  212. bindState,
  213. dataBit,
  214. parityBit,
  215. stopBit,
  216. baudRate,
  217. readTimeout,
  218. nextDataReadDelay,
  219. nextRoundDataReadDelay,
  220. } = item;
  221. gatewayLinks.value.push({
  222. link: {
  223. id,
  224. linkName,
  225. gatewayId,
  226. interfaceId,
  227. protocolType,
  228. bindState,
  229. dataBit,
  230. parityBit,
  231. stopBit,
  232. baudRate,
  233. readTimeout,
  234. nextDataReadDelay,
  235. nextRoundDataReadDelay,
  236. },
  237. protocols: item.protocols,
  238. });
  239. });
  240. await addGatewayLinkBatchUpdate(gatewayLinks.value);
  241. gatewayEditor.value = false;
  242. });
  243. };
  244. const addGateway = () => {
  245. modalGuidanceRef.value?.showView();
  246. };
  247. const getProtocolData = () => {
  248. getGatewayList();
  249. };
  250. onMounted(() => {
  251. addGatewayList();
  252. });
  253. </script>
  254. <template>
  255. <div>
  256. <AFlex justify="space-between">
  257. <div class="text-top">{{ $t('navigation.gatewayManage') }}</div>
  258. <div>
  259. <AButton type="primary" class="icon-button" @click="addGateway">
  260. <AFlex align="center">
  261. <SvgIcon name="plus" />
  262. <span> {{ $t('common.add') }} </span>
  263. </AFlex>
  264. </AButton>
  265. </div>
  266. </AFlex>
  267. <div class="gateway-content">
  268. <ARow>
  269. <ACol :span="12" class="gateway-left">
  270. <AFlex justify="space-between" align="center" class="gateway-left-top">
  271. <div class="gateway-left-top-text">{{ $t('navigation.gatewayList') }}</div>
  272. <AButton type="text" @click="refreshData" class="icon-button gateway-left-top-icon">
  273. <AFlex align="center">
  274. <SvgIcon name="refresh-o" />
  275. <span> {{ $t('registerGateway.refreshData') }} </span>
  276. </AFlex>
  277. </AButton>
  278. </AFlex>
  279. <AFlex justify="space-between" align="center" class="input-bottom">
  280. <div>
  281. <span class="gateway-left-text">{{ $t('common.search') }}</span>
  282. <AInput v-model:value="gatewayQuery.searchContent" placeholder="请输入序列号、型号" class="input-width" />
  283. </div>
  284. <div>
  285. <span class="gateway-left-text">{{ $t('common.status') }}</span>
  286. <ASelect class="select-width" v-model:value="gatewayQuery.state" placeholder="请选择">
  287. <ASelectOption :value="-1">{{ $t('common.all') }}</ASelectOption>
  288. <ASelectOption :value="1">{{ $t('common.online') }}</ASelectOption>
  289. <ASelectOption :value="0">{{ $t('common.offline') }}</ASelectOption>
  290. </ASelect>
  291. </div>
  292. </AFlex>
  293. <AFlex justify="flex-end">
  294. <AButton type="primary" class="query-button" @click="addGatewayList">{{ $t('common.query') }}</AButton>
  295. <AButton @click="addReset" class="default-button">{{ $t('common.reset') }}</AButton>
  296. </AFlex>
  297. <ATable
  298. :columns="gatewayColumns"
  299. :data-source="gatewayData"
  300. :row-key="(record) => record.id"
  301. :custom-row="rowClick"
  302. :pagination="false"
  303. :row-class-name="rowClassName"
  304. >
  305. <template #bodyCell="{ column, record, index }">
  306. <template v-if="column.key === 'index'">
  307. {{ index + 1 }}
  308. </template>
  309. <template v-else-if="column.key === 'state'">
  310. <div v-if="record.state === 0" class="tag-style default">{{ $t('common.offline') }}</div>
  311. <div v-else class="tag-style success">{{ $t('common.online') }}</div>
  312. </template>
  313. <template v-else-if="column.key === 'action'">
  314. <SvgIcon @click="addGatewayDelete(record.id)" class="icon-delete" name="delete" />
  315. </template>
  316. </template>
  317. </ATable>
  318. <AFlex justify="flex-end" class="gateway-left-footer">
  319. <APagination
  320. v-model:current="gatewayQuery.pageIndex"
  321. v-model:page-size="gatewayQuery.pageSize"
  322. :total="gatewayQuery.total"
  323. :show-size-changer="true"
  324. @change="switchPages"
  325. show-quick-jumper
  326. :show-total="(total) => $t('common.pageTotal', { total })"
  327. />
  328. </AFlex>
  329. </ACol>
  330. <ACol :span="12" class="gateway-right">
  331. <AFlex justify="space-between" align="center" class="gateway-right-top">
  332. <div class="gateway-left-top-text">{{ $t('registerGateway.gatewayConfiguration') }}</div>
  333. <!-- 注释编辑功能 -->
  334. <div v-if="false">
  335. <div v-if="gatewayEditor">
  336. <AButton v-if="gatewayEditor" type="text" class="icon-button gateway-left-top-icon" @click="cancelSave">
  337. <AFlex align="center">
  338. <SvgIcon name="close" />
  339. <span> {{ $t('common.cancel') }} </span>
  340. </AFlex>
  341. </AButton>
  342. <AButton v-if="gatewayEditor" type="text" class="icon-button gateway-left-top-icon" @click="addSave">
  343. <AFlex align="center">
  344. <SvgIcon name="edit-o" />
  345. <span> {{ $t('common.save') }} </span>
  346. </AFlex>
  347. </AButton>
  348. </div>
  349. <div v-else>
  350. <AButton
  351. v-if="!gatewayEditor"
  352. type="text"
  353. class="icon-button gateway-left-top-icon"
  354. @click="gatewayEditor = true"
  355. :disabled="!interfaceList.length"
  356. >
  357. <AFlex align="center">
  358. <SvgIcon name="edit-o" />
  359. <span> {{ $t('common.editor') }} </span>
  360. </AFlex>
  361. </AButton>
  362. </div>
  363. </div>
  364. </AFlex>
  365. <ACollapse v-model:active-key="activeKey" accordion v-if="interfaceList.length" collapsible="icon">
  366. <ACollapsePanel v-for="(item, index) in interfaceList" :key="item.id">
  367. <template #header>
  368. <span class="interface-text-right">{{ $t('createDevice.physicalInterface') }}: </span>
  369. <span v-if="!gatewayEditor">{{ item.linkName }}</span>
  370. <ASelect
  371. v-else
  372. v-model:value="item.interfaceId"
  373. class="interface-select"
  374. :options="interfaceSelectList"
  375. :field-names="{ label: 'name', value: 'id' }"
  376. @change="(value, option) => choosePhysicalInterface(value, option, item.interfaceId, item, index)"
  377. />
  378. </template>
  379. <AFlex align="center" class="gateway-type-bottom">
  380. <div class="gateway-right-text">{{ $t('setupProtocol.protocolType') }}:</div>
  381. <div>
  382. <span v-if="!gatewayEditor">{{ item.protocolType }}</span>
  383. <ASelect
  384. v-else
  385. v-model:value="item.protocolType"
  386. class="interface-select"
  387. :options="listPhysicalInterfaces"
  388. :field-names="{ label: 'protocolName', value: 'protocolName' }"
  389. />
  390. </div>
  391. </AFlex>
  392. <ATable :columns="agreementColumns" :data-source="item.protocols" :pagination="false">
  393. <!-- <template #bodyCell="{ column, record }">
  394. <template v-if="column.key === 'station'">
  395. <AInputNumber v-if="gatewayEditor" v-model:value="record.station" :min="0" />
  396. </template>
  397. </template> -->
  398. </ATable>
  399. </ACollapsePanel>
  400. </ACollapse>
  401. </ACol>
  402. </ARow>
  403. </div>
  404. <ModalGuidance ref="modalGuidance" @finish="getProtocolData">
  405. <RegisterGateway />
  406. </ModalGuidance>
  407. <ConfirmModal
  408. ref="modalComponent"
  409. :title="$t('common.deleteConfirmation')"
  410. :description-text="$t('common.confirmDeletion')"
  411. :icon="{ name: 'delete' }"
  412. :icon-bg-color="'#F56C6C'"
  413. @confirm="confirm"
  414. />
  415. </div>
  416. </template>
  417. <style lang="scss" scoped>
  418. .success {
  419. width: 40px;
  420. color: #52c41a;
  421. background: #f6ffed;
  422. border: 1px solid #b7eb8f;
  423. }
  424. .default {
  425. width: 40px;
  426. color: #666;
  427. background: #f8f8f8;
  428. border: 1px solid #e5e5e5;
  429. }
  430. .tag-style {
  431. display: flex;
  432. align-items: center; /* 垂直居中 */
  433. justify-content: center; /* 水平居中 */
  434. height: 24px;
  435. font-size: 12px;
  436. border-radius: 4px;
  437. }
  438. /* 添加选中行样式 */
  439. :deep(.selected-row) td {
  440. background-color: #e0f5f5 !important;
  441. }
  442. .gateway-type-bottom {
  443. margin-bottom: 12px;
  444. }
  445. .interface-text-right {
  446. margin-right: 10px;
  447. }
  448. .interface-select {
  449. width: 192px;
  450. }
  451. .icon-delete {
  452. font-size: 21px;
  453. color: var(--antd-color-primary-hover);
  454. cursor: pointer;
  455. }
  456. .gateway-left-footer {
  457. margin-top: 24px;
  458. }
  459. .gateway-right-text {
  460. margin-right: 10px;
  461. font-size: 14px;
  462. font-style: normal;
  463. font-weight: 500;
  464. line-height: 24px;
  465. color: rgb(0 0 0 / 85%);
  466. text-align: left;
  467. }
  468. .gateway-right-top {
  469. margin-bottom: 16px;
  470. }
  471. .query-button {
  472. margin-right: 12px;
  473. margin-bottom: 16px;
  474. }
  475. .input-bottom {
  476. margin-bottom: 16px;
  477. }
  478. .select-width {
  479. width: 192px;
  480. }
  481. .input-width {
  482. width: 192px;
  483. }
  484. .gateway-left-text {
  485. margin-right: 12px;
  486. font-size: 14px;
  487. font-style: normal;
  488. font-weight: 400;
  489. line-height: 22px;
  490. color: rgb(0 0 0 / 85%);
  491. text-align: right;
  492. }
  493. .gateway-left-top-icon {
  494. font-size: 14px;
  495. font-style: normal;
  496. font-weight: 400;
  497. line-height: 24px;
  498. color: #666;
  499. text-align: left;
  500. }
  501. .gateway-left-top-text {
  502. font-size: 16px;
  503. font-style: normal;
  504. font-weight: 600;
  505. line-height: 24px;
  506. color: #333;
  507. text-align: left;
  508. }
  509. .gateway-left-top {
  510. margin-bottom: 24px;
  511. }
  512. .gateway-left {
  513. padding: 24px;
  514. }
  515. .gateway-right {
  516. padding: 24px;
  517. border-left: 1px solid #e4e7ed;
  518. }
  519. .gateway-content {
  520. margin-top: 24px;
  521. background: #fff;
  522. border-radius: 16px;
  523. }
  524. .text-top {
  525. font-size: 20px;
  526. font-style: normal;
  527. font-weight: 500;
  528. line-height: 28px;
  529. color: rgb(0 0 0 / 85%);
  530. text-align: left;
  531. }
  532. .deletion-button {
  533. width: 84px;
  534. height: 32px;
  535. margin-right: 16px;
  536. }
  537. </style>