Bill.vue 3.4 KB

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