Create.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc">
  7. <a-form-item class="mb-0" :label="$t('network.text_205', [$t('dictionary.project')])" v-bind="formItemLayout" v-if="params.type === 'create'">
  8. <domain-project :fc="form.fc" :decorators="decorators" :labelInValue="false" />
  9. </a-form-item>
  10. <a-form-item :label="$t('network.text_291')" v-bind="formItemLayout" v-if="params.type === 'create'">
  11. <a-input v-decorator="decorators.name" :placeholder="$t('network.text_44')" />
  12. </a-form-item>
  13. <a-form-item :label="$t('common.description')" v-bind="formItemLayout" v-if="params.type === 'create'">
  14. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  15. </a-form-item>
  16. <a-form-item v-bind="formItemLayout">
  17. <span slot="label">{{$t('network.text_292')}}<a-tooltip>
  18. <div slot="title">{{$t('network.text_293')}}<br />{{$t('network.text_294')}}<br />{{$t('network.text_295')}}<br />{{$t('network.text_296')}}<br />{{$t('network.text_297')}}</div>
  19. <a-icon type="info-circle" />
  20. </a-tooltip>
  21. </span>
  22. <a-textarea v-decorator="decorators.acl_entries"
  23. :placeholder="$t('network.text_298')"
  24. rows="8" />
  25. </a-form-item>
  26. </a-form>
  27. </div>
  28. <div slot="footer">
  29. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  30. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  31. </div>
  32. </base-dialog>
  33. </template>
  34. <script>
  35. import { mapGetters } from 'vuex'
  36. import { REGEXP } from '@/utils/validate'
  37. import DomainProject from '@/sections/DomainProject'
  38. import DialogMixin from '@/mixins/dialog'
  39. import WindowsMixin from '@/mixins/windows'
  40. import i18n from '@/locales'
  41. const ipsValidate = (rule, value, cb) => {
  42. const pureIps = value.split(/[\s\n]/).filter(v => !!v).map(str => str.trim())
  43. const noRepeatIps = Array.from(new Set(pureIps))
  44. const isRepeat = noRepeatIps.length !== pureIps.length
  45. let descValid = true
  46. const valid = pureIps.every(ipComment => {
  47. const [ip, comment = ''] = ipComment.split('|').filter(v => !!v && v !== '|')
  48. if (comment.length > 16) descValid = false
  49. if (ip.includes('/')) {
  50. const [ipStr] = ip.split('/')
  51. if (REGEXP.IPv4.regexp.test(ipStr) && REGEXP.cidr.regexp.test(ip)) {
  52. return true
  53. } else {
  54. return false
  55. }
  56. } else {
  57. if (REGEXP.IPv4.regexp.test(ip)) {
  58. return true
  59. }
  60. return false
  61. }
  62. })
  63. if (valid) {
  64. if (isRepeat) {
  65. cb(new Error(i18n.t('network.text_299')))
  66. } else {
  67. if (!descValid) return cb(new Error(i18n.t('network.text_300')))
  68. cb()
  69. }
  70. } else {
  71. cb(new Error(i18n.t('network.text_301')))
  72. }
  73. }
  74. export default {
  75. name: 'LbaclsCreateDialog',
  76. components: {
  77. DomainProject,
  78. },
  79. mixins: [DialogMixin, WindowsMixin],
  80. data () {
  81. return {
  82. loading: false,
  83. form: {
  84. fc: this.$form.createForm(this, {
  85. onValuesChange: (props, values) => {
  86. if (values.acl_entries) {
  87. const str = values.acl_entries
  88. const cidrs = str.split(/[\s\n]/).filter(v => !!v)
  89. this.aclEntries = cidrs.map(str => {
  90. const [cidr, comment = ''] = str.split('|')
  91. return {
  92. cidr,
  93. comment,
  94. }
  95. })
  96. }
  97. },
  98. }),
  99. },
  100. decorators: {
  101. domain: [
  102. 'domain',
  103. {
  104. initialValue: this.$store.getters.userInfo.projectDomainId,
  105. rules: [
  106. { required: true, message: this.$t('rules.domain'), trigger: 'change' },
  107. ],
  108. },
  109. ],
  110. project: [
  111. 'project',
  112. {
  113. initialValue: this.$store.getters.userInfo.projectId,
  114. rules: [
  115. { required: true, message: this.$t('rules.project'), trigger: 'change' },
  116. ],
  117. },
  118. ],
  119. name: [
  120. 'name',
  121. {
  122. validateFirst: true,
  123. validateTrigger: ['blur'],
  124. rules: [
  125. { required: true, message: this.$t('network.text_116') },
  126. { validator: this.$validate('serverName') },
  127. ],
  128. },
  129. ],
  130. description: ['description'],
  131. acl_entries: [
  132. 'acl_entries',
  133. {
  134. validateFirst: true,
  135. validateTrigger: ['blur'],
  136. rules: [
  137. { required: true, message: this.$t('network.text_302') },
  138. { validator: ipsValidate },
  139. ],
  140. },
  141. ],
  142. },
  143. formItemLayout: {
  144. wrapperCol: {
  145. span: 18,
  146. },
  147. labelCol: {
  148. span: 6,
  149. },
  150. },
  151. aclEntries: [],
  152. }
  153. },
  154. computed: {
  155. ...mapGetters(['isAdminMode', 'scope', 'isDomainMode', 'userInfo', 'l3PermissionEnable']),
  156. },
  157. created () {
  158. if (this.params.type === 'update') {
  159. const initialValue = this.params.data[0].acl_entries.map(v => {
  160. if (v.comment) {
  161. return `${v.cidr}|${v.comment}`
  162. }
  163. return v.cidr
  164. }).join(`
  165. `)
  166. this.form.fc.getFieldDecorator('acl_entries', { preserve: true, initialValue })
  167. }
  168. },
  169. methods: {
  170. doCreate (data) {
  171. if (this.isAdminMode) {
  172. data.admin = true
  173. } else {
  174. delete data.domain
  175. }
  176. return this.params.onManager('create', {
  177. managerArgs: {
  178. data: {
  179. ...data,
  180. name: data.name,
  181. acl_entries: this.aclEntries,
  182. },
  183. },
  184. })
  185. },
  186. doUpdate (data) {
  187. return this.params.onManager('update', {
  188. id: this.params.data[0].id,
  189. managerArgs: {
  190. data: {
  191. ...data,
  192. name: data.name,
  193. acl_entries: this.aclEntries,
  194. },
  195. },
  196. })
  197. },
  198. async handleConfirm () {
  199. this.loading = true
  200. try {
  201. const values = await this.form.fc.validateFields()
  202. if (this.params.type === 'create') {
  203. await this.doCreate(values)
  204. } else {
  205. await this.doUpdate(values)
  206. }
  207. this.loading = false
  208. this.cancelDialog()
  209. } catch (error) {
  210. this.loading = false
  211. }
  212. },
  213. },
  214. }
  215. </script>