AddInterface.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <script setup lang="ts">
  2. import { onMounted, ref, useTemplateRef, watch } from 'vue';
  3. import { message } from 'ant-design-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. gatewayLinkAdd,
  10. gatewayLinkDelete,
  11. gatewayLinkGetList,
  12. obtainListInterfaces,
  13. obtainListPhysicalInterfaces,
  14. } from '@/api';
  15. import deviceInterface from '@/assets/img/device-interface.png';
  16. import type {
  17. InterfaceData,
  18. ListInterfaces,
  19. ListPhysicalInterfaces,
  20. RegisterGatewayForm,
  21. UseGuideStepItemProps,
  22. } from '@/types';
  23. const props = defineProps<UseGuideStepItemProps<RegisterGatewayForm>>();
  24. const modalComponentRef = useTemplateRef('modalComponent');
  25. const currentStep = props.steps[props.stepIndex];
  26. const interfaceId = ref<number>();
  27. const interfaceList = ref<ListInterfaces[]>([]);
  28. const protocolName = ref<string>('');
  29. const linkName = ref<string>('');
  30. const listPhysicalInterfaces = ref<ListPhysicalInterfaces[]>([]);
  31. const { handleRequest } = useRequest();
  32. interface InterfaceStyle {
  33. background: string;
  34. backgroundSize: string;
  35. }
  36. interface LeftStyle {
  37. left: string;
  38. }
  39. // 定义一个响应式变量来存储样式对象
  40. const interfaceStyle = ref<InterfaceStyle>({
  41. background: `url(${deviceInterface})`,
  42. backgroundSize: '410px 120px',
  43. });
  44. const leftStyle = ref<LeftStyle>({
  45. left: '229px',
  46. });
  47. const interfaceData = ref<InterfaceData[]>([]);
  48. const postGetList = () => {
  49. handleRequest(async () => {
  50. interfaceData.value = await gatewayLinkGetList(props.form.id);
  51. if (interfaceData.value.length) {
  52. currentStep.nextStepButtonDisabled = false;
  53. } else {
  54. currentStep.nextStepButtonDisabled = true;
  55. }
  56. });
  57. };
  58. // 根据条件显示背景
  59. const interfaceShow = (value: string) => {
  60. return value === 'LAN' ? 'background-color: rgba(54, 207, 201, 1);' : 'background-color: rgba(89, 126, 247, 1);';
  61. };
  62. // 添加接口列表
  63. const addInterface = () => {
  64. if (!props.form.interfaceId) {
  65. return message.warning(t('registerGateway.pleaseSelectInterface'));
  66. }
  67. if (!props.form.protocolId) {
  68. return message.warning(t('setupProtocol.plzSelectProtocolType'));
  69. }
  70. interfaceList.value.forEach((item) => {
  71. if (item.id === props.form.interfaceId) {
  72. linkName.value = item.name;
  73. }
  74. });
  75. listPhysicalInterfaces.value.forEach((item) => {
  76. if (item.id === props.form.protocolId) {
  77. protocolName.value = item.protocolName;
  78. }
  79. });
  80. handleRequest(async () => {
  81. await gatewayLinkAdd({
  82. interfaceId: props.form.interfaceId,
  83. linkName: linkName.value,
  84. gatewayId: props.form.id,
  85. protocolType: protocolName.value,
  86. });
  87. postGetList();
  88. });
  89. };
  90. const confirm = () => {
  91. handleRequest(async () => {
  92. if (interfaceId.value) {
  93. await gatewayLinkDelete(interfaceId.value);
  94. modalComponentRef.value?.hideView();
  95. postGetList();
  96. }
  97. });
  98. };
  99. // 删除接口
  100. const addDelete = (value: number) => {
  101. interfaceId.value = value;
  102. modalComponentRef.value?.showView();
  103. };
  104. const getListPhysicalInterfaces = (value: number) => {
  105. handleRequest(async () => {
  106. listPhysicalInterfaces.value = await obtainListPhysicalInterfaces(value);
  107. if (listPhysicalInterfaces.value.length) {
  108. props.form.protocolId = listPhysicalInterfaces.value[0].id;
  109. } else {
  110. props.form.protocolId = undefined;
  111. }
  112. });
  113. };
  114. watch(
  115. () => props.form.interfaceId,
  116. (count) => {
  117. if (count) {
  118. if (count === 3) {
  119. leftStyle.value.left = '85px';
  120. } else if (count === 4) {
  121. leftStyle.value.left = '154px';
  122. } else if (count === 1) {
  123. leftStyle.value.left = '186px';
  124. } else if (count === 2) {
  125. leftStyle.value.left = '218px';
  126. }
  127. getListPhysicalInterfaces(count);
  128. }
  129. },
  130. );
  131. onMounted(() => {
  132. handleRequest(async () => {
  133. interfaceList.value = await obtainListInterfaces(props.form.modelId);
  134. if (interfaceList.value.length) {
  135. props.form.interfaceId = interfaceList.value[0].id;
  136. leftStyle.value.left = '85px';
  137. getListPhysicalInterfaces(interfaceList.value[0].id);
  138. }
  139. postGetList();
  140. });
  141. });
  142. </script>
  143. <template>
  144. <div>
  145. <AFlex>
  146. <div class="interface-width">
  147. <AFormItem :label="$t('registerGateway.selectInterface')" name="interfaceId">
  148. <ASelect
  149. ref="select"
  150. v-model:value="form.interfaceId"
  151. class="interface-select"
  152. :options="interfaceList"
  153. :field-names="{ label: 'name', value: 'id' }"
  154. />
  155. </AFormItem>
  156. <AFormItem :label="$t('setupProtocol.selectProtocolType')" name="protocolId">
  157. <ASelect
  158. ref="select"
  159. v-model:value="form.protocolId"
  160. class="interface-select"
  161. :field-names="{ label: 'protocolName', value: 'id' }"
  162. :options="listPhysicalInterfaces"
  163. />
  164. </AFormItem>
  165. </div>
  166. <div :style="interfaceStyle" class="interface-img">
  167. <div v-if="form.interfaceId" class="interface-bgc" :style="leftStyle"></div>
  168. </div>
  169. </AFlex>
  170. <AButton class="dev-but" type="primary" @click="addInterface">{{ $t('common.add') }}</AButton>
  171. <div class="use-guide-title">{{ $t('registerGateway.interfaceList') }}</div>
  172. <AFlex v-if="interfaceData.length === 0" justify="center" align="center" class="empty-data">
  173. <div>
  174. <img src="@/assets/img/no-data.png" alt="" />
  175. <div class="empty-text">{{ $t('common.noData') }}</div>
  176. </div>
  177. </AFlex>
  178. <AFlex v-else wrap="wrap">
  179. <AFlex v-for="item in interfaceData" :key="item.id" class="interface-list" align="center">
  180. <AFlex class="interface-div" justify="space-between" align="center">
  181. <AFlex align="center">
  182. <AFlex class="interface-div1" :style="interfaceShow(item.interfaceType)" justify="center" align="center">
  183. <span class="interface-div1-span">{{ item.interfaceType }}</span>
  184. </AFlex>
  185. <div class="interface-div2">{{ item.linkName }}</div>
  186. </AFlex>
  187. <AFlex align="center">
  188. <div class="interface-agreement">{{ item.protocolType }}</div>
  189. </AFlex>
  190. </AFlex>
  191. <div @click="addDelete(item.id)">
  192. <AFlex class="interface-but" justify="center" align="center">
  193. <SvgIcon class="interface-but-icon" name="delete" />
  194. </AFlex>
  195. </div>
  196. </AFlex>
  197. </AFlex>
  198. <ConfirmModal
  199. ref="modalComponent"
  200. :title="$t('common.deleteConfirmation')"
  201. :description-text="$t('common.confirmDeletion')"
  202. :icon="{ name: 'delete' }"
  203. :icon-bg-color="'#F56C6C'"
  204. @confirm="confirm"
  205. />
  206. </div>
  207. </template>
  208. <style lang="scss" scoped>
  209. .empty-text {
  210. margin-top: 16px;
  211. font-size: 14px;
  212. font-style: normal;
  213. font-weight: 400;
  214. line-height: 22px;
  215. color: #ccc;
  216. }
  217. .empty-data {
  218. width: 814px;
  219. height: 40vh;
  220. text-align: center;
  221. }
  222. .interface-width {
  223. width: 364px;
  224. }
  225. .interface-but-icon {
  226. font-size: 14px;
  227. color: #f56e6e;
  228. }
  229. .use-guide-title {
  230. margin-top: 40px;
  231. margin-bottom: 16px;
  232. font-size: 16px;
  233. font-style: normal;
  234. font-weight: 500;
  235. line-height: 24px;
  236. color: rgb(0 0 0 / 85%);
  237. text-align: left;
  238. }
  239. .dev-but {
  240. width: 96px;
  241. height: 40px;
  242. margin-top: 10px;
  243. border-radius: 3px;
  244. }
  245. .interface-select {
  246. width: 256px;
  247. }
  248. .interface-bgc {
  249. position: relative;
  250. top: 46px;
  251. width: 36px;
  252. height: 36px;
  253. background-color: rgb(82 196 26 / 35%);
  254. border-radius: 8px;
  255. }
  256. .interface-div {
  257. width: 458px;
  258. height: 72px;
  259. background: #f5f7fa;
  260. border-radius: 8px;
  261. }
  262. .interface-img {
  263. position: 'static';
  264. width: 410px;
  265. height: 120px;
  266. margin-left: 32px;
  267. }
  268. .interface-div1 {
  269. width: 48px;
  270. height: 48px;
  271. margin-left: 20px;
  272. border-radius: 4px;
  273. }
  274. .interface-div1-span {
  275. font-size: 16px;
  276. font-style: normal;
  277. font-weight: 400;
  278. line-height: 18px;
  279. color: rgb(255 255 255 / 97%);
  280. }
  281. .interface-div2 {
  282. margin-left: 20px;
  283. font-size: 16px;
  284. font-style: normal;
  285. font-weight: 500;
  286. line-height: 17px;
  287. color: var(--antd-color-text-secondary);
  288. text-align: left;
  289. }
  290. .interface-agreement {
  291. margin-top: 6px;
  292. margin-right: 24px;
  293. font-size: 14px;
  294. font-style: normal;
  295. font-weight: 400;
  296. line-height: 22px;
  297. color: #666;
  298. text-align: right;
  299. }
  300. .interface-but {
  301. width: 32px;
  302. height: 32px;
  303. margin-left: 16px;
  304. cursor: pointer;
  305. background: #fff;
  306. border: 1px solid #d9d9d9;
  307. border-radius: 4px;
  308. }
  309. .interface-container {
  310. width: 565px;
  311. height: 410px;
  312. }
  313. .interface-list {
  314. margin-right: 80px;
  315. margin-bottom: 16px;
  316. }
  317. </style>