index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div>
  3. <a-form-item>
  4. <a-radio-group v-decorator="decorators.billType" @change="change">
  5. <a-radio-button
  6. v-for="(item, key) in billTypesMap"
  7. :value="key"
  8. :disabled="disabledBillType === item.key"
  9. :key="key">{{ item.label }}</a-radio-button>
  10. </a-radio-group>
  11. </a-form-item>
  12. <template v-if="showDuration">
  13. <a-form-item :extra="duration === '1W' ? $t('compute.text_118') : ''">
  14. <a-radio-group v-decorator="decorators.duration" @change="durationChange">
  15. <a-radio-button
  16. v-for="item in buyDurationOptions"
  17. :disabled="item.disabled"
  18. :value="item.key"
  19. :key="item.key">{{ item.label }}</a-radio-button>
  20. </a-radio-group>
  21. <a-checkbox class="ml-4" v-decorator="decorators.autoRenew">{{$t('compute.text_119')}}</a-checkbox>
  22. </a-form-item>
  23. </template>
  24. </div>
  25. </template>
  26. <script>
  27. import * as R from 'ramda'
  28. import _ from 'lodash'
  29. import { BILL_TYPES_MAP, BUY_DURATION_OPTIONS } from '@Compute/constants'
  30. export default {
  31. name: 'VmPublicCreateBill',
  32. props: {
  33. decorators: {
  34. type: Object,
  35. required: true,
  36. validator: val => R.is(Array, val.billType) && R.is(Array, val.duration),
  37. },
  38. form: {
  39. type: Object,
  40. },
  41. providerList: {
  42. type: Array,
  43. },
  44. disabledBillType: {
  45. type: String,
  46. },
  47. billTypesMaps: {
  48. type: Object,
  49. default: () => BILL_TYPES_MAP,
  50. },
  51. },
  52. data () {
  53. return {
  54. duration: _.get(this.decorators.duration, '[1].initialValue') || '1M',
  55. billTypesMap: this.billTypesMaps,
  56. buyDurationOptions: BUY_DURATION_OPTIONS,
  57. showDuration: _.get(this.decorators.billType, '[1].initialValue') === BILL_TYPES_MAP.package.key,
  58. }
  59. },
  60. watch: {
  61. providerList (providerList, oldV) {
  62. if (!R.equals(providerList, oldV)) {
  63. const list = providerList.map(val => val.name.toLowerCase())
  64. this.buyDurationOptions = this.buyDurationOptions.map(item => {
  65. let disabled = false
  66. if (R.is(Array, item.includes)) {
  67. if (item.includes.every(provider => list.includes(provider))) { // 如果有provider的限制,必须每项都满足
  68. disabled = false
  69. } else {
  70. disabled = true
  71. }
  72. }
  73. return {
  74. ...item,
  75. disabled,
  76. }
  77. })
  78. }
  79. },
  80. },
  81. mounted () {
  82. if (this.form && this.form.fd) {
  83. this.$set(this.form.fd, 'billType', _.get(this.decorators.billType, '[1].initialValue'))
  84. this.$set(this.form.fd, 'duration', this.duration)
  85. }
  86. },
  87. methods: {
  88. durationDisabled (item) {
  89. // 比如一周的包年包月仅阿里云支持
  90. if (this.providerList && this.providerList.length) {
  91. const list = this.providerList.map(val => val.name.toLowerCase())
  92. if (R.is(Array, item.includes)) {
  93. return item.includes.some(provider => list.includes(provider))
  94. }
  95. }
  96. return false
  97. },
  98. change (val) {
  99. this.showDuration = val.target.value === BILL_TYPES_MAP.package.key
  100. if (this.showDuration && this.form && this.form.fc) {
  101. let duration = '1M'
  102. if (this.decorators.duration[1] && this.decorators.duration[1].initialValue) {
  103. duration = this.decorators.duration[1].initialValue
  104. }
  105. this.form.fc.setFieldsValue({
  106. [this.decorators.duration[0]]: duration,
  107. })
  108. }
  109. },
  110. durationChange (val) {
  111. this.duration = val.target.value
  112. },
  113. },
  114. }
  115. </script>