IpAddress.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="network-config">
  3. <div class="d-flex align-items-start mb-2" v-for="(item, i) in networkList" :key="item.key">
  4. <a-form-item class="w-100 mb-0 mr-2">
  5. <a-input
  6. :placeholder="$t('network.text_217')"
  7. v-decorator="decorators.networkConfig.ips(item.key, item.network)"
  8. @change="e => ipChange(e, i)" />
  9. </a-form-item>
  10. <a-button v-if="networkList.length > 1" shape="circle" icon="minus" size="small" @click="decrease(item.key, i)" class="mt-2 ml-1" />
  11. </div>
  12. <div class="d-flex align-items-center">
  13. <a-button type="primary" shape="circle" icon="plus" size="small" @click="add" />
  14. <a-button type="link" @click="add">{{$t('network.text_670')}}</a-button>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. import { uuid } from '@/utils/utils'
  20. import { validate } from '@/utils/validate'
  21. export default {
  22. name: 'ReservedIPAddress',
  23. props: {
  24. decorators: {
  25. type: Object,
  26. required: true,
  27. },
  28. form: {
  29. type: Object,
  30. required: true,
  31. },
  32. },
  33. data () {
  34. return {
  35. networkList: [],
  36. }
  37. },
  38. created () {
  39. this.add()
  40. },
  41. methods: {
  42. getFormInstance () {
  43. return this.form || this.$parent?.form?.fc || this.$parent?.$parent?.form?.fc
  44. },
  45. ipChange (e, i) {
  46. this.networkList[i].ip = e.target.value
  47. },
  48. ipToInt (ip) {
  49. const REG = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  50. const result = REG.exec(ip)
  51. if (!result) return -1
  52. const ret = (parseInt(result[1]) << 24 |
  53. parseInt(result[2]) << 16 |
  54. parseInt(result[3]) << 8 |
  55. parseInt(result[4])) >>> 0
  56. return ret
  57. },
  58. intToIp (ipInt) {
  59. return [
  60. (ipInt >>> 24) & 0xFF,
  61. (ipInt >>> 16) & 0xFF,
  62. (ipInt >>> 8) & 0xFF,
  63. ipInt & 0xFF,
  64. ].join('.')
  65. },
  66. getNextIp (ip) {
  67. if (!ip) return ''
  68. const ipResult = validate(ip, 'IPv4')
  69. if (ipResult === false || ipResult.result === false) {
  70. return ''
  71. }
  72. const ipInt = this.ipToInt(ip)
  73. if (ipInt === -1) return ''
  74. const nextInt = ipInt + 1
  75. if (nextInt > 0xFFFFFFFF) return ''
  76. return this.intToIp(nextInt)
  77. },
  78. add () {
  79. const uid = uuid()
  80. let nextIp = ''
  81. if (this.networkList.length > 0) {
  82. const lastIndex = this.networkList.length - 1
  83. const lastItem = this.networkList[lastIndex]
  84. const form = this.getFormInstance()
  85. if (form) {
  86. const lastFieldName = `networkIps[${lastItem.key}]`
  87. const lastFieldValue = form.getFieldValue(lastFieldName)
  88. if (lastFieldValue) {
  89. nextIp = this.getNextIp(lastFieldValue)
  90. }
  91. }
  92. }
  93. this.networkList.push({
  94. key: uid,
  95. ip: nextIp,
  96. })
  97. this.$nextTick(() => {
  98. if (nextIp) {
  99. const form = this.getFormInstance()
  100. if (form) {
  101. const fieldName = `networkIps[${uid}]`
  102. form.setFieldsValue({
  103. [fieldName]: nextIp,
  104. })
  105. }
  106. }
  107. })
  108. },
  109. decrease (uid, index) {
  110. this.networkList.splice(index, 1)
  111. },
  112. },
  113. }
  114. </script>
  115. <style lang="less" scoped>
  116. @import '../../../../../src/styles/less/theme';
  117. .network-config {
  118. .network-count-tips {
  119. .remain-num {
  120. color: @primary-color;
  121. }
  122. }
  123. }
  124. </style>