Start.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="params.name || $t('dictionary.server')" :count="dataList.length" :action="action" />
  6. <dialog-table :data="dataList" :columns="columns" />
  7. <a-form
  8. :form="form.fc">
  9. <a-form-item :label="$t('compute.text_1041')" v-bind="formItemLayout" v-if="isOpenWorkflow">
  10. <a-input v-decorator="decorators.reason" :placeholder="$t('compute.text_1105')" />
  11. </a-form-item>
  12. <a-form-item class="mb-0" v-show="canAutoPrepaid">
  13. <a-checkbox
  14. v-model="form.fd.auto_prepaid"
  15. @change="autoPrepaidChange">
  16. {{$t('compute.change_to_prepaid')}}
  17. </a-checkbox>
  18. </a-form-item>
  19. </a-form>
  20. </div>
  21. <div slot="footer">
  22. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  23. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  24. </div>
  25. </base-dialog>
  26. </template>
  27. <script>
  28. import { mapGetters } from 'vuex'
  29. import DialogMixin from '@/mixins/dialog'
  30. import WindowsMixin from '@/mixins/windows'
  31. import WorkflowMixin from '@/mixins/workflow'
  32. import { BATCH_OPERATE_SERVERS_MAX } from '@/constants/workflow'
  33. import { getNameDescriptionTableColumn, getStatusTableColumn } from '@/utils/common/tableColumn'
  34. export default {
  35. name: 'VmStartDialog',
  36. mixins: [DialogMixin, WindowsMixin, WorkflowMixin],
  37. data () {
  38. const { type, formData = {} } = this.params
  39. return {
  40. loading: false,
  41. type,
  42. action: type === 'modifyWorkflow' ? this.$t('common.modify_workflow') + `(${this.$t('compute.text_272')})` : this.$t('compute.text_272'),
  43. form: {
  44. fc: this.$form.createForm(this),
  45. fd: {
  46. auto_prepaid: formData.auto_prepaid || false,
  47. },
  48. },
  49. decorators: {
  50. reason: [
  51. 'reason',
  52. {
  53. initialValue: formData.reason || '',
  54. },
  55. ],
  56. auto_prepaid: [
  57. 'stopPaying',
  58. {
  59. valuePropName: 'checked',
  60. initialValue: formData.auto_prepaid || false,
  61. },
  62. ],
  63. },
  64. formItemLayout: {
  65. wrapperCol: {
  66. span: 21,
  67. },
  68. labelCol: {
  69. span: 3,
  70. },
  71. },
  72. dataList: [],
  73. }
  74. },
  75. computed: {
  76. ...mapGetters(['userInfo']),
  77. columns () {
  78. if (this.type === 'modifyWorkflow') {
  79. return [getNameDescriptionTableColumn(), getStatusTableColumn({ statusModule: 'server' })]
  80. }
  81. const showFields = ['name', 'ip', 'instance_type']
  82. return this.params.columns.filter((item) => { return showFields.includes(item.field) })
  83. },
  84. // 腾讯云、阿里云、火山云、金山云的按量付费机器,关机可停止付费
  85. // 腾讯云、阿里云包年包月机器,关机停止付费会转为按量付费机器
  86. canAutoPrepaid () {
  87. return this.dataList.every(item => {
  88. return ['qcloud', 'aliyun', 'ksyun'].includes(item.brand.toLocaleLowerCase())
  89. }) && this.dataList.some(item => {
  90. return item.billing_type === 'postpaid'
  91. })
  92. },
  93. isOpenWorkflow () {
  94. return this.checkWorkflowEnabled(this.WORKFLOW_TYPES.APPLY_SERVER_START)
  95. },
  96. },
  97. created () {
  98. this.initData()
  99. },
  100. methods: {
  101. async initData () {
  102. if (this.type === 'modifyWorkflow' && this.params.ids) {
  103. const list = await new this.$Manager('servers', 'v2').batchGet({
  104. id: this.params.ids,
  105. })
  106. this.dataList = list.data?.data || []
  107. } else {
  108. this.dataList = this.params.data || []
  109. }
  110. },
  111. async doShutDownSubmit () {
  112. const data = {}
  113. if (this.form.fd.auto_prepaid) {
  114. data.auto_prepaid = true
  115. }
  116. const ids = this.dataList.map(item => item.id)
  117. return this.params.onManager('batchPerformAction', {
  118. id: ids,
  119. steadyStatus: 'running',
  120. managerArgs: {
  121. action: 'start',
  122. data,
  123. },
  124. })
  125. },
  126. async handleConfirm () {
  127. this.loading = true
  128. try {
  129. if (this.isOpenWorkflow) {
  130. if (this.dataList.length > BATCH_OPERATE_SERVERS_MAX) {
  131. this.$message.error(this.$t('compute.workflow.operate_servers_check.message', [this.$t('compute.text_273'), BATCH_OPERATE_SERVERS_MAX]))
  132. this.loading = false
  133. return
  134. }
  135. const projects = new Set(this.dataList.map(item => item.tenant_id))
  136. if (projects.size > 1) {
  137. this.$message.error(this.$t('compute.text_1348'))
  138. this.loading = false
  139. return
  140. }
  141. await this.handleShutDownByWorkflowSubmit()
  142. } else {
  143. await this.doShutDownSubmit()
  144. }
  145. this.loading = false
  146. this.cancelDialog()
  147. } catch (error) {
  148. this.loading = false
  149. throw error
  150. }
  151. },
  152. autoPrepaidChange (val) {
  153. const { checked } = val.target
  154. this.form.fd.auto_prepaid = checked
  155. },
  156. async handleShutDownByWorkflowSubmit () {
  157. const ids = this.dataList.map(item => item.id)
  158. const values = await this.form.fc.validateFields()
  159. const params = {}
  160. if (this.form.fd.auto_prepaid) params.auto_prepaid = true
  161. const variables = {
  162. project: this.dataList[0].tenant_id,
  163. project_domain: this.dataList[0].domain_id,
  164. process_definition_key: this.WORKFLOW_TYPES.APPLY_SERVER_START,
  165. initiator: this.userInfo.id,
  166. ids: ids.join(','),
  167. description: values.reason,
  168. paramter: JSON.stringify(params),
  169. }
  170. if (this.type === 'modifyWorkflow') {
  171. await this.updateWorkflow(variables, this.params.workflow)
  172. this.params.success && this.params.success()
  173. } else {
  174. await this.createWorkflow(variables)
  175. }
  176. this.$message.success(this.$t('common.worflow_tip', [this.$t('compute.text_272')]))
  177. this.$router.push('/workflow')
  178. },
  179. },
  180. }
  181. </script>