ReversedIP.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.network')" :count="params.data.length" :action="params.title" />
  6. <dialog-table class="mb-2" :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form :form="form.fc">
  8. <a-form-item
  9. v-for="(item, i) in ipList"
  10. :key="item.key"
  11. :label="i === 0 ? $t('network.text_213') : ''"
  12. v-bind="i === 0 ? formItemLayout : formItemLayoutWithOutLabel">
  13. <a-input
  14. v-decorator="ipDecorator(item, i)"
  15. :placeholder="$t('network.text_217')"
  16. style="width: 60%; margin-right: 8px" />
  17. <a-button
  18. shape="circle"
  19. icon="minus"
  20. size="small"
  21. v-if="ipList.length > 1"
  22. @click="decrease(i)"
  23. class="mt-2 ml-2" />
  24. </a-form-item>
  25. <a-form-item v-bind="formItemLayoutWithOutLabel" style="position:relative; top: -14px">
  26. <div class="d-flex align-items-center">
  27. <a-button type="primary" shape="circle" icon="plus" size="small" @click="addIP" />
  28. <a-button type="link" @click="addIP">{{$t('network.text_640')}}</a-button>
  29. </div>
  30. </a-form-item>
  31. <a-form-item :label="$t('network.text_641')" v-bind="formItemLayout">
  32. <a-textarea v-decorator="['notes', {
  33. rules: [
  34. { required: true, message: this.$t('network.text_643') },
  35. { max: 200, message: $t('network.text_642')}
  36. ],
  37. }]" :placeholder="$t('network.text_643')" :rows="2" />
  38. </a-form-item>
  39. </a-form>
  40. </div>
  41. <div slot="footer">
  42. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  43. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  44. </div>
  45. </base-dialog>
  46. </template>
  47. <script>
  48. // import * as R from 'ramda'
  49. import DialogMixin from '@/mixins/dialog'
  50. import WindowsMixin from '@/mixins/windows'
  51. import { uuid } from '@/utils/utils'
  52. import validateForm, { validate } from '@/utils/validate'
  53. export default {
  54. name: 'NetworkReversedIPDialog',
  55. mixins: [DialogMixin, WindowsMixin],
  56. data () {
  57. return {
  58. ipList: [{ key: uuid() }],
  59. loading: false,
  60. scope: this.$store.getters.scope,
  61. form: {
  62. fc: this.$form.createForm(this),
  63. },
  64. formItemLayout: {
  65. wrapperCol: {
  66. span: 19,
  67. },
  68. labelCol: {
  69. span: 5,
  70. },
  71. },
  72. formItemLayoutWithOutLabel: {
  73. wrapperCol: {
  74. span: 19, offset: 5,
  75. },
  76. },
  77. }
  78. },
  79. methods: {
  80. async ipBlur (rule, value, _callback) {
  81. const ipData = this.form.fc.getFieldValue('ipData') || {}
  82. const ips = Object.values(ipData)
  83. if (ips.filter(ip => ip === value).length >= 2) {
  84. return _callback(this.$t('network.text_644'))
  85. }
  86. const manager = new this.$Manager('reservedips', 'v2')
  87. try {
  88. const { data } = await manager.list({
  89. params: {
  90. search: value,
  91. network: this.params.data[0].id,
  92. },
  93. })
  94. if (data && data.length > 0) return _callback(this.$t('network.text_645'))
  95. } catch {
  96. _callback()
  97. }
  98. },
  99. ipDecorator (item, i) {
  100. const { key } = item
  101. return [`ipData.${key}`, {
  102. validateFirst: true,
  103. rules: [
  104. {
  105. required: true, message: this.$t('network.text_646'),
  106. },
  107. { validator: validateForm(['IPv4', 'IPv6'], true, 'some'), message: this.$t('network.text_647') },
  108. { validator: this.ipBlur, trigger: ['change'] },
  109. ],
  110. }]
  111. },
  112. ipToInt (ip) {
  113. 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])$/
  114. const result = REG.exec(ip)
  115. if (!result) return -1
  116. const ret = (parseInt(result[1]) << 24 |
  117. parseInt(result[2]) << 16 |
  118. parseInt(result[3]) << 8 |
  119. parseInt(result[4])) >>> 0
  120. return ret
  121. },
  122. intToIp (ipInt) {
  123. return [
  124. (ipInt >>> 24) & 0xFF,
  125. (ipInt >>> 16) & 0xFF,
  126. (ipInt >>> 8) & 0xFF,
  127. ipInt & 0xFF,
  128. ].join('.')
  129. },
  130. getNextIp (ip) {
  131. if (!ip) return ''
  132. const ipResult = validate(ip, 'IPv4')
  133. if (ipResult === false || ipResult.result === false) {
  134. return ''
  135. }
  136. const ipInt = this.ipToInt(ip)
  137. if (ipInt === -1) return ''
  138. const nextInt = ipInt + 1
  139. if (nextInt > 0xFFFFFFFF) return ''
  140. return this.intToIp(nextInt)
  141. },
  142. addIP () {
  143. const key = uuid()
  144. let nextIp = ''
  145. if (this.ipList.length > 0) {
  146. const lastIndex = this.ipList.length - 1
  147. const lastItem = this.ipList[lastIndex]
  148. const lastFieldName = `ipData.${lastItem.key}`
  149. const lastFieldValue = this.form.fc.getFieldValue(lastFieldName)
  150. if (lastFieldValue) {
  151. nextIp = this.getNextIp(lastFieldValue)
  152. }
  153. }
  154. this.ipList.push({
  155. key,
  156. })
  157. this.$nextTick(() => {
  158. if (nextIp) {
  159. this.form.fc.setFieldsValue({
  160. [`ipData.${key}`]: nextIp,
  161. })
  162. }
  163. })
  164. },
  165. decrease (index) {
  166. this.ipList.splice(index, 1)
  167. },
  168. async handleConfirm () {
  169. this.loading = true
  170. try {
  171. const values = await this.form.fc.validateFields()
  172. await this.params.onManager('performAction', {
  173. id: this.params.data[0].id,
  174. managerArgs: {
  175. action: 'reserve-ip',
  176. data: {
  177. ips: Object.values(values.ipData),
  178. notes: values.notes,
  179. },
  180. },
  181. })
  182. this.cancelDialog()
  183. } finally {
  184. this.loading = false
  185. }
  186. },
  187. },
  188. }
  189. </script>