NetworkIpMacsCreate.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ title }}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.network')" :count="params.data.length" :action="action" />
  6. <dialog-table :data="params.data" :columns="columns" />
  7. <a-form :form="form.fc" class="mt-3" v-bind="formItemLayout" hideRequiredMark>
  8. <a-form-item
  9. v-for="(k, index) in form.fc.getFieldValue('keys')"
  10. :key="k"
  11. v-bind="index === 0 ? formItemLayout : formItemLayoutWithOutLabel"
  12. :label="index === 0 ? $t('network.mac_ip.mapping_table') : ''"
  13. :required="false">
  14. <a-row :gutter="6">
  15. <a-col :span="11">
  16. <a-form-item
  17. :wrapperCol="{ span: 24 }"
  18. class="mb-0 mr-1">
  19. <a-input v-decorator="decorators.mappingTables.ip(k)" :placeholder="$t('network.text_217')" />
  20. </a-form-item>
  21. </a-col>
  22. <a-col :span="11">
  23. <a-form-item
  24. :wrapperCol="{ span: 24 }"
  25. class="mb-0 mr-1">
  26. <a-input v-decorator="decorators.mappingTables.mac(k)" :placeholder="$t('common.tips.input', [$t('network.text_228')])" />
  27. </a-form-item>
  28. </a-col>
  29. <a-col :span="2" v-show="!isEdit">
  30. <a-button v-if="index === 0" type="primary" shape="circle" icon="plus" size="small" @click="add" />
  31. <a-button v-else shape="circle" icon="minus" size="small" @click="remove(k)" />
  32. </a-col>
  33. </a-row>
  34. </a-form-item>
  35. </a-form>
  36. </div>
  37. <div slot="footer">
  38. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  39. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  40. </div>
  41. </base-dialog>
  42. </template>
  43. <script>
  44. import DialogMixin from '@/mixins/dialog'
  45. import WindowsMixin from '@/mixins/windows'
  46. import { Manager } from '@/utils/manager'
  47. let id = 0
  48. export default {
  49. name: 'NetworkIpMacsCreateDialog',
  50. mixins: [DialogMixin, WindowsMixin],
  51. data () {
  52. return {
  53. loading: false,
  54. decorators: {
  55. mappingTables: {
  56. ip: i => [
  57. `ip[${i}]`,
  58. {
  59. initialValue: this.params.editData?.[i].ip_addr || undefined,
  60. validateTrigger: ['change', 'blur'],
  61. validateFirst: true,
  62. rules: [
  63. { required: true, message: this.$t('network.text_217') },
  64. { validator: this.$validate('IPv4') },
  65. ],
  66. },
  67. ],
  68. mac: i => [
  69. `mac[${i}]`,
  70. {
  71. initialValue: this.params.editData?.[i].mac_addr || undefined,
  72. validateTrigger: ['change', 'blur'],
  73. validateFirst: true,
  74. rules: [
  75. { required: true, message: this.$t('common.tips.input', [this.$t('network.text_228')]) },
  76. { validator: this.$validate('mac') },
  77. ],
  78. },
  79. ],
  80. },
  81. },
  82. columns: [
  83. {
  84. field: 'name',
  85. title: this.$t('common.name'),
  86. },
  87. {
  88. field: 'vpc',
  89. title: 'VPC',
  90. },
  91. {
  92. field: 'ip',
  93. title: this.$t('network.text_213'),
  94. width: 180,
  95. slots: {
  96. default: ({ row }) => {
  97. return [
  98. <div>{ this.$t('network.ip.start', [row.guest_ip_start, row.guest_ip_mask])}</div>,
  99. <div>{ this.$t('network.ip.end', [row.guest_ip_end, row.guest_ip_mask])}</div>,
  100. ]
  101. },
  102. },
  103. },
  104. {
  105. field: 'ip',
  106. title: this.$t('network.ipv6.address'),
  107. width: 180,
  108. slots: {
  109. default: ({ row }) => {
  110. if (!row.guest_ip6_start || !row.guest_ip6_end) {
  111. return '-'
  112. }
  113. return [
  114. <div>{ this.$t('network.ip.start', [row.guest_ip6_start, row.guest_ip6_mask])}</div>,
  115. <div>{ this.$t('network.ip.end', [row.guest_ip6_end, row.guest_ip6_mask])}</div>,
  116. ]
  117. },
  118. },
  119. },
  120. ],
  121. formItemLayout: {
  122. wrapperCol: {
  123. md: { span: 16 },
  124. xl: { span: 18 },
  125. xxl: { span: 20 },
  126. },
  127. labelCol: {
  128. md: { span: 8 },
  129. xl: { span: 6 },
  130. xxl: { span: 4 },
  131. },
  132. },
  133. formItemLayoutWithOutLabel: {
  134. wrapperCol: {
  135. md: { span: 16, offset: 8 },
  136. xl: { span: 18, offset: 6 },
  137. xxl: { span: 20, offset: 4 },
  138. },
  139. },
  140. }
  141. },
  142. computed: {
  143. isEdit () {
  144. return this.params.editData
  145. },
  146. title () {
  147. return this.isEdit ? this.$t('common.edit') : this.$t('common.create')
  148. },
  149. action () {
  150. return this.isEdit ? this.$t('network.mac_ip.edit_mapping_table') : this.$t('network.mac_ip.create_mapping_table')
  151. },
  152. },
  153. beforeCreate () {
  154. this.form = {}
  155. this.form.fc = this.$form.createForm(this, { name: 'ip_mac_create_form' })
  156. this.form.fc.getFieldDecorator('keys', { initialValue: [id], preserve: true })
  157. },
  158. methods: {
  159. generateValues (values) {
  160. const data = {
  161. network_id: this.params.data[0].id,
  162. }
  163. const ip_mac = {}
  164. values.keys.forEach(key => {
  165. const ip = values.ip[key]
  166. const mac = values.mac[key]
  167. ip_mac[`${ip}`] = mac
  168. })
  169. data.ip_mac = ip_mac
  170. return data
  171. },
  172. async doCreate (values) {
  173. const manager = new Manager('network_ip_macs/batch-create')
  174. const data = this.generateValues(values)
  175. await manager.create({ data })
  176. },
  177. async doUpdate (values) {
  178. const manager = new Manager('network_ip_macs')
  179. const data = {
  180. ip_addr: values.ip[0],
  181. mac_addr: values.mac[0],
  182. }
  183. await manager.update({
  184. id: this.params.editData[0].id,
  185. data,
  186. })
  187. },
  188. async handleConfirm () {
  189. this.loading = true
  190. try {
  191. const { validateFields } = this.form.fc
  192. const values = await validateFields()
  193. this.isEdit ? this.doUpdate(values) : this.doCreate(values)
  194. this.$message.success(this.$t('common.success'))
  195. this.params.ok && this.params.ok()
  196. this.cancelDialog()
  197. } catch (error) {
  198. throw error
  199. } finally {
  200. this.loading = false
  201. }
  202. },
  203. add () {
  204. const { form } = this
  205. const keys = form.fc.getFieldValue('keys')
  206. const nextKeys = keys.concat(++id)
  207. form.fc.setFieldsValue({ keys: nextKeys })
  208. },
  209. remove (k) {
  210. const { form } = this
  211. const keys = form.fc.getFieldValue('keys')
  212. if (keys.length === 1) {
  213. return
  214. }
  215. form.fc.setFieldsValue({
  216. keys: keys.filter(key => key !== k),
  217. })
  218. },
  219. },
  220. }
  221. </script>
  222. <style>
  223. </style>