WhiteListForm.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <a-form slot="body" :form="form.fc" class="mt-3">
  5. <template v-if="params.data && params.data.length > 0">
  6. <dialog-selected-tips :name="$t('dictionary.elasticcaches')" :count="params.data.length" :action="params.title" />
  7. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  8. </template>
  9. <a-form-item v-bind="formItemLayout" :label="$t('db.text_60')">
  10. <template v-if="params.data && params.data.length > 0">
  11. {{params.data[0].name}}
  12. </template>
  13. <a-input v-else :placeholder="$t('validator.dbName')" v-decorator="decorators.name" />
  14. </a-form-item>
  15. <a-form-item v-bind="formItemLayout" :label="$t('db.text_294')">
  16. <a-textarea :placeholder="$t('db.text_295')"
  17. :autosize="{ minRows: 4, maxRows: 7 }"
  18. v-decorator="decorators['ip_list']" />
  19. <div style="line-height:20px;color:#999">{{$t('db.text_296')}}<b style="color:#666">{{remainIpCount}}</b>{{$t('db.text_297')}}</div>
  20. </a-form-item>
  21. </a-form>
  22. <div slot="footer">
  23. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  24. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  25. </div>
  26. </base-dialog>
  27. </template>
  28. <script>
  29. import validateForm, { REGEXP } from '@/utils/validate'
  30. import DialogMixin from '@/mixins/dialog'
  31. import WindowsMixin from '@/mixins/windows'
  32. export default {
  33. name: 'RedisWhiteListFormDialog',
  34. mixins: [DialogMixin, WindowsMixin],
  35. data () {
  36. return {
  37. loading: false,
  38. title: this.$t('db.text_41'),
  39. form: {
  40. fc: this.$form.createForm(this),
  41. },
  42. formItemLayout: {
  43. wrapperCol: { span: 20 },
  44. labelCol: { span: 4 },
  45. },
  46. ipsLength: 0,
  47. }
  48. },
  49. computed: {
  50. decorators () {
  51. const { initialValues = {} } = this.params
  52. const decorators = {
  53. name: [
  54. 'name',
  55. {
  56. initialValue: initialValues.name,
  57. validateFirst: true,
  58. rules: [
  59. { required: true, message: this.$t('db.text_136') },
  60. { validator: validateForm('dbName') },
  61. ],
  62. },
  63. ],
  64. ip_list: [
  65. 'ip_list',
  66. {
  67. initialValue: initialValues.ip_list,
  68. validateFirst: true,
  69. validateTrigger: 'blur',
  70. rules: [
  71. { required: true, message: this.$t('db.text_298') },
  72. { validator: this.validateIps },
  73. ],
  74. },
  75. ],
  76. }
  77. return decorators
  78. },
  79. remainIpCount () {
  80. return 20 - this.ipsLength
  81. },
  82. },
  83. created () {
  84. const { allIPList } = this.params
  85. this.ipsLength = allIPList.length
  86. },
  87. methods: {
  88. validateIps (rule, value, _callback) {
  89. const REG_IP = REGEXP.IPv4.regexp
  90. const REG_NUM = /\d/
  91. if (value) {
  92. const ips = value.split(',')
  93. const alreadyipList = [...this.params.allIPList]
  94. const itselfIpList = this.params.data ? this.params.data[0].ip_list.split(',') : []
  95. for (let i = 0; i < alreadyipList.length; i++) {
  96. for (let j = 0; j < itselfIpList.length; j++) {
  97. const sameIndex = alreadyipList.indexOf(itselfIpList[j])
  98. if (sameIndex > -1) {
  99. alreadyipList.splice(sameIndex, 1)
  100. }
  101. }
  102. }
  103. const ipsLength = ips.length
  104. if (ipsLength >= 20) {
  105. return _callback(this.$t('db.text_299'))
  106. }
  107. for (let i = 0; i < ipsLength; i++) {
  108. const _item = ips[i]
  109. const [_ip, _u] = _item.split('/')
  110. if (_ip && !REG_IP.test(_ip)) {
  111. return _callback(this.$t('db.text_300', [_item]))
  112. }
  113. if (_u && !REG_NUM.test(_u)) {
  114. return _callback(this.$t('db.text_301', [_u]))
  115. }
  116. if (ips.indexOf(_item) !== i) {
  117. return _callback(this.$t('db.text_302', [_item]))
  118. }
  119. }
  120. for (let i = 0; i < alreadyipList.length; i++) {
  121. for (let j = 0; j < ipsLength; j++) {
  122. if (alreadyipList.indexOf(ips[j]) > -1) {
  123. return _callback(this.$t('db.text_302', [ips[j]]))
  124. }
  125. }
  126. }
  127. this.ipsLength = ipsLength
  128. }
  129. _callback()
  130. },
  131. validateForm () {
  132. return new Promise((resolve, reject) => {
  133. this.form.fc.validateFields((err, values) => {
  134. if (!err) {
  135. resolve(values)
  136. } else {
  137. reject(err)
  138. }
  139. })
  140. })
  141. },
  142. async handleConfirm () {
  143. this.loading = true
  144. try {
  145. const values = await this.validateForm()
  146. if (this.params.data && this.params.data.length > 0) {
  147. const params = {
  148. ...values,
  149. elasticcache: this.params.redisItem.id,
  150. }
  151. if (params.name === this.params.data[0].name) {
  152. delete params.name
  153. }
  154. await this.params.list.onManager('update', {
  155. steadyStatus: this.params.steadyStatus,
  156. id: this.params.data[0].id,
  157. managerArgs: {
  158. data: params,
  159. },
  160. })
  161. } else {
  162. await this.params.list.onManager('create', {
  163. steadyStatus: this.params.steadyStatus,
  164. managerArgs: {
  165. data: {
  166. ...values,
  167. elasticcache: this.params.redisItem.id,
  168. },
  169. },
  170. })
  171. }
  172. this.loading = false
  173. this.cancelDialog()
  174. } catch (error) {
  175. this.loading = false
  176. }
  177. },
  178. },
  179. }
  180. </script>