ScheduledtaskEdit.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ title }}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('cloudenv.text_431')" class="mt-3" :count="params.data.length" :action="title" />
  6. <dialog-table v-if="params.columns && params.columns.length" :data="params.data" :columns="columns" />
  7. <a-form
  8. :form="form.fc"
  9. v-bind="formItemLayout"
  10. hideRequiredMark>
  11. <a-form-item :label="$t('cloudenv.text_440')" v-if="isServer">
  12. <list-select
  13. v-decorator="decorators.servers"
  14. :list-props="resourceProps"
  15. :multiple="true"
  16. :formatter="formatter" />
  17. </a-form-item>
  18. <a-form-item :label="$t('cloudenv.text_16')" class="mb-0" v-if="isTag">
  19. <tag v-decorator="decorators.tag" :extra="$t('cloudenv.text_441')" />
  20. </a-form-item>
  21. </a-form>
  22. </div>
  23. <div slot="footer">
  24. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t("dialog.ok") }}</a-button>
  25. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  26. </div>
  27. </base-dialog>
  28. </template>
  29. <script>
  30. import ResourcePropsMixin from '../mixins/resourceProps'
  31. import DialogMixin from '@/mixins/dialog'
  32. import WindowsMixin from '@/mixins/windows'
  33. import Tag from '@/sections/Tag'
  34. import ListSelect from '@/sections/ListSelect'
  35. import validateForm from '@/utils/validate'
  36. export default {
  37. name: 'ScheduledtaskEditDialog',
  38. components: {
  39. Tag,
  40. ListSelect,
  41. },
  42. mixins: [DialogMixin, WindowsMixin, ResourcePropsMixin],
  43. data () {
  44. const getInitTags = (labels) => {
  45. const tag = {}
  46. labels.forEach((v) => {
  47. const arr = v.replace('user:', '').split(':')
  48. tag[`user:${arr[0]}`] = arr[1]
  49. })
  50. return tag
  51. }
  52. const labels = this.params.data[0].labels
  53. return {
  54. loading: false,
  55. form: {
  56. fc: this.$form.createForm(this),
  57. fd: {},
  58. },
  59. decorators: {
  60. servers: [
  61. 'servers',
  62. {
  63. initialValue: labels,
  64. },
  65. ],
  66. tag: [
  67. 'tag',
  68. {
  69. initialValue: getInitTags(labels),
  70. rules: [
  71. { required: true, message: this.$t('cloudenv.text_451') },
  72. { validator: validateForm('tagName') },
  73. ],
  74. },
  75. ],
  76. },
  77. formItemLayout: {
  78. wrapperCol: {
  79. span: 21,
  80. },
  81. labelCol: {
  82. span: 3,
  83. },
  84. },
  85. }
  86. },
  87. computed: {
  88. isServer () {
  89. return this.params.data.every((item) => item.label_type === 'id')
  90. },
  91. isTag () {
  92. return this.params.data.every((item) => item.label_type === 'tag')
  93. },
  94. title () {
  95. return this.params.title || this.$t('cloudenv.text_454')
  96. },
  97. columns () {
  98. const showColumns = ['name', 'status', 'resource_type']
  99. return this.params.columns.filter(v => showColumns.includes(v.field))
  100. },
  101. },
  102. methods: {
  103. async handleConfirm () {
  104. this.loading = true
  105. const manager = new this.$Manager('scheduledtasks', 'v1')
  106. try {
  107. const values = await this.form.fc.validateFields()
  108. const ids = this.params.data.map(item => item.id)
  109. const params = {}
  110. if (this.isServer) {
  111. params.labels = values.servers
  112. } else if (this.isTag) {
  113. const tags = Object.keys(values.tag).map((k) => {
  114. return `${k}:${values.tag[k]}`
  115. })
  116. params.labels = tags
  117. }
  118. const res = await manager.performAction({
  119. id: ids[0],
  120. action: 'set-labels',
  121. data: {
  122. labels: params.labels,
  123. },
  124. })
  125. this.$bus.$emit('ScheduledtasksListSingleRefresh', ids)
  126. this.isServer && this.$bus.$emit('RelatedResourceServerListSidePageRefresh', res.data.labels)
  127. this.isTag && this.$bus.$emit('RelatedResourceTagListSidePageRefresh', res.data.labels)
  128. this.params.success && this.params.success(res.data.labels)
  129. this.cancelDialog()
  130. } catch (error) {
  131. throw error
  132. } finally {
  133. this.loading = false
  134. }
  135. },
  136. formatter (val) {
  137. return `${val.name}`
  138. },
  139. },
  140. }
  141. </script>