GatewayProtocol.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <script setup lang="ts">
  2. import { computed, onMounted, reactive, ref, useTemplateRef } from 'vue';
  3. import { message, Modal } from 'ant-design-vue';
  4. import ModalGuidance from '@/layout/ModalGuidance.vue';
  5. import SvgIcon from '@/components/SvgIcon.vue';
  6. import { useRequest } from '@/hooks/request';
  7. import { useViewVisible } from '@/hooks/view-visible';
  8. import { t } from '@/i18n';
  9. import { deleteProtocolBaseInfo, getProtocolList, updateProtocolBaseInfo } from '@/api';
  10. import ProtocolContent from '../setup-protocol/ProtocolContent.vue';
  11. import SetupProtocol from '../setup-protocol/SetupProtocol.vue';
  12. import type { ColumnType } from 'ant-design-vue/es/table';
  13. import type { PageParams, ProtocolBaseInfo } from '@/types';
  14. const protocolInitialInfo: Partial<ProtocolBaseInfo> = {
  15. id: undefined,
  16. protocolName: '',
  17. protocolType: undefined,
  18. deviceType: undefined,
  19. dataBit: 5,
  20. parityBit: 'N',
  21. stopBit: '1',
  22. baudRate: undefined,
  23. dataSendInterval: undefined,
  24. highFreqSendInterval: undefined,
  25. readTimeout: undefined,
  26. nextDataReadDelay: undefined,
  27. nextRoundDataReadDelay: undefined,
  28. readContinuousAddr: undefined,
  29. readContinuousAddrCode: undefined,
  30. readContinuousAddrLength: undefined,
  31. byteOrder: undefined,
  32. byteOrderCode: undefined,
  33. addrOrder: undefined,
  34. addrOrderCode: undefined,
  35. };
  36. const protocolInfoForm = reactive<Partial<ProtocolBaseInfo>>({
  37. ...protocolInitialInfo,
  38. });
  39. const protocolList = ref<ProtocolBaseInfo[]>([]);
  40. const protocolTotal = ref(0);
  41. const searchContent = ref('');
  42. const pageParams = ref<PageParams>({
  43. pageIndex: 1,
  44. pageSize: 10,
  45. pageSorts: [
  46. {
  47. column: 'id',
  48. asc: true,
  49. },
  50. ],
  51. });
  52. const columns = computed<ColumnType<ProtocolBaseInfo>[]>(() => {
  53. return [
  54. {
  55. title: t('gatewayProtocol.protocolName'),
  56. dataIndex: 'protocolName',
  57. key: 'protocolName',
  58. },
  59. {
  60. title: t('setupProtocol.protocolType'),
  61. dataIndex: 'protocolType',
  62. key: 'protocolType',
  63. },
  64. {
  65. title: t('setupProtocol.deviceType'),
  66. dataIndex: 'deviceType',
  67. key: 'deviceType',
  68. },
  69. {
  70. title: t('common.updateTime'),
  71. dataIndex: 'updateTime',
  72. key: 'updateTime',
  73. },
  74. {
  75. title: t('common.operation'),
  76. key: 'action',
  77. },
  78. ];
  79. });
  80. onMounted(() => {
  81. getProtocolData();
  82. });
  83. const { isLoading, handleRequest } = useRequest();
  84. const getProtocolData = () => {
  85. handleRequest(async () => {
  86. const { records, total } = await getProtocolList({
  87. ...pageParams.value,
  88. searchContent: searchContent.value,
  89. });
  90. protocolList.value = records;
  91. protocolTotal.value = total;
  92. });
  93. };
  94. const handleSearch = () => {
  95. pageParams.value.pageIndex = 1;
  96. getProtocolData();
  97. };
  98. const handleAdd = () => {
  99. modalGuidanceRef.value?.showView();
  100. };
  101. const handleEdit = (record: ProtocolBaseInfo) => {
  102. Object.assign(protocolInfoForm, record);
  103. showView();
  104. };
  105. const handleDelete = (record: ProtocolBaseInfo) => {
  106. Modal.confirm({
  107. title: t('gatewayProtocol.confirmDeleteProtocol', { name: record.protocolName }),
  108. async onOk() {
  109. try {
  110. await deleteProtocolBaseInfo(record.id);
  111. message.success(t('gatewayProtocol.deleteProtocolSuccessful'));
  112. pageParams.value.pageIndex = 1;
  113. getProtocolData();
  114. } catch (err) {
  115. message.error((err as Error).message);
  116. console.error(err);
  117. }
  118. },
  119. });
  120. };
  121. const handlePageChange = (page: number, pageSize: number) => {
  122. pageParams.value.pageIndex = page;
  123. pageParams.value.pageSize = pageSize;
  124. getProtocolData();
  125. };
  126. const modalGuidanceRef = useTemplateRef('modalGuidance');
  127. const { isLoading: isConfirmLoading, handleRequest: handleModalConfirmRequest } = useRequest();
  128. const { visible, showView, hideView } = useViewVisible();
  129. const protocolContentRef = useTemplateRef('protocolContent');
  130. const handleClose = () => {
  131. Object.assign(protocolInfoForm, protocolInitialInfo);
  132. };
  133. const handleOk = () => {
  134. handleModalConfirmRequest(async () => {
  135. await protocolContentRef.value?.validateProtocolInfo();
  136. await protocolContentRef.value?.isAtLeastOneParam();
  137. await updateProtocolBaseInfo(protocolInfoForm);
  138. message.success(t('gatewayProtocol.editProtocolSuccessful'));
  139. getProtocolData();
  140. hideView();
  141. });
  142. };
  143. </script>
  144. <template>
  145. <div class="protocol-list-container">
  146. <div class="protocol-search">
  147. <div>
  148. <span class="protocol-search-label">{{ $t('common.search') }}:</span>
  149. <AInput
  150. v-model:value="searchContent"
  151. class="protocol-search-input"
  152. :placeholder="$t('gatewayProtocol.searchTip')"
  153. allow-clear
  154. />
  155. </div>
  156. <AButton type="primary" @click="handleSearch">{{ $t('common.query') }}</AButton>
  157. </div>
  158. <div class="protocol-add-container">
  159. <AButton class="icon-button add-button" type="primary" @click="handleAdd">
  160. <SvgIcon name="plus" />
  161. {{ $t('common.addNew') }}
  162. </AButton>
  163. </div>
  164. <ATable
  165. class="protocol-table"
  166. :data-source="protocolList"
  167. :columns="columns"
  168. row-key="id"
  169. :loading="isLoading"
  170. :pagination="{
  171. current: pageParams.pageIndex,
  172. pageSize: pageParams.pageSize,
  173. total: protocolTotal,
  174. showSizeChanger: false,
  175. hideOnSinglePage: false,
  176. onChange: handlePageChange,
  177. }"
  178. >
  179. <template #bodyCell="{ column, record }">
  180. <span v-if="column.key === 'action'">
  181. <a @click="handleEdit(record as ProtocolBaseInfo)">{{ $t('common.revise') }}</a>
  182. <ADivider type="vertical" />
  183. <a @click="handleDelete(record as ProtocolBaseInfo)">{{ $t('common.delete') }}</a>
  184. </span>
  185. </template>
  186. </ATable>
  187. <ModalGuidance ref="modalGuidance" @finish="getProtocolData">
  188. <SetupProtocol />
  189. </ModalGuidance>
  190. <AModal
  191. v-model:open="visible"
  192. wrap-class-name="gateway-protocol-modal"
  193. :title="$t('common.editor')"
  194. :width="1140"
  195. centered
  196. :mask-closable="false"
  197. :after-close="handleClose"
  198. :confirm-loading="isConfirmLoading"
  199. destroy-on-close
  200. @ok="handleOk"
  201. >
  202. <ProtocolContent ref="protocolContent" :info="protocolInfoForm" :is-recognized="false" />
  203. </AModal>
  204. </div>
  205. </template>
  206. <style lang="scss">
  207. .gateway-protocol-modal {
  208. .ant-modal-content {
  209. height: 638px;
  210. }
  211. .ant-modal-body {
  212. height: calc(100% - 84px);
  213. overflow: hidden auto;
  214. }
  215. }
  216. </style>
  217. <style lang="scss" scoped>
  218. .protocol-list-container {
  219. height: 100%;
  220. padding: 32px;
  221. background: var(--antd-color-bg-base);
  222. border-radius: 18px;
  223. }
  224. .protocol-search {
  225. display: flex;
  226. align-items: center;
  227. justify-content: space-between;
  228. margin-bottom: 24px;
  229. }
  230. .protocol-search-label {
  231. margin-right: 10px;
  232. font-size: 14px;
  233. line-height: 22px;
  234. color: var(--antd-color-text);
  235. }
  236. .protocol-search-input {
  237. width: 272px;
  238. }
  239. .protocol-add-container {
  240. display: flex;
  241. align-items: center;
  242. }
  243. .add-button {
  244. margin-right: 8px;
  245. font-size: 14px;
  246. }
  247. .protocol-table {
  248. margin-top: 72px;
  249. a {
  250. color: var(--antd-color-primary);
  251. }
  252. }
  253. </style>