index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div>
  3. <a-form-item class="mb-0">
  4. <a-radio-group @change="change" v-decorator="decorators.durationStandard">
  5. <a-radio-button v-for="item in opts" :key="item.key" :value="item.key">{{ item.label }}</a-radio-button>
  6. </a-radio-group>
  7. </a-form-item>
  8. <a-form-item v-if="showDuration">
  9. <duration-input v-decorator="decorators.duration" />
  10. </a-form-item>
  11. </div>
  12. </template>
  13. <script>
  14. import * as R from 'ramda'
  15. import { getDurationLabel } from '@/utils/utils'
  16. const defaultOpts = ['none', '1h', '6h', '1d', '3d', '1w', '1m', 'custom']
  17. export default {
  18. name: 'Duration',
  19. props: {
  20. decorators: {
  21. type: Object,
  22. required: true,
  23. validator: val => !R.isNil(val.durationStandard) && !R.isNil(val.duration),
  24. },
  25. form: {
  26. type: Object,
  27. },
  28. useServerDuration: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. },
  33. data () {
  34. return {
  35. opts: [],
  36. loading: false,
  37. showDuration: this.decorators.durationStandard[1].initialValue === 'custom',
  38. }
  39. },
  40. created () {
  41. this.getOpts()
  42. },
  43. methods: {
  44. sortOptions (options) {
  45. const opts = [...options]
  46. opts.sort((a, b) => {
  47. // 定义排序优先级
  48. const getPriority = (key) => {
  49. if (key === 'none') return 0
  50. if (key === 'custom') return 999
  51. if (key.endsWith('h')) return 1
  52. if (key.endsWith('d')) return 2
  53. if (key.endsWith('w')) return 3
  54. if (key.endsWith('m')) return 4
  55. if (key.endsWith('y')) return 5
  56. return 6
  57. }
  58. const priorityA = getPriority(a)
  59. const priorityB = getPriority(b)
  60. // 如果优先级不同,按优先级排序
  61. if (priorityA !== priorityB) {
  62. return priorityA - priorityB
  63. }
  64. // 如果优先级相同,按数字大小排序
  65. const numA = parseInt(a) || 0
  66. const numB = parseInt(b) || 0
  67. return numA - numB
  68. })
  69. return opts
  70. },
  71. async getOpts () {
  72. const opts = [...defaultOpts]
  73. if (this.useServerDuration) {
  74. try {
  75. this.loading = true
  76. const res = await new this.$Manager('scopedpolicybindings', 'v1').list({
  77. params: {
  78. category: 'server_duration',
  79. scope: this.$store.getters.scope,
  80. effective: true,
  81. ignoreErrorStatusCode: [403],
  82. },
  83. })
  84. if (res.data && res.data.data && res.data.data.length > 0) {
  85. const { policy = {} } = res.data.data[0]
  86. const { server_duration = [] } = policy
  87. if (server_duration.length > 0) {
  88. this.opts = this.sortOptions(server_duration).map(item => {
  89. return {
  90. key: item,
  91. label: this.getLabel(item),
  92. }
  93. })
  94. if (!this.opts.some(item => item.key === this.decorators.durationStandard[1].initialValue)) {
  95. if (this.opts.some(item => item.key === this.decorators.duration[1].initialValue)) {
  96. this.form.fc.setFieldsValue({
  97. [this.decorators.durationStandard[0]]: this.decorators.duration[1].initialValue,
  98. })
  99. if (this.decorators.duration[1].initialValue !== 'custom') {
  100. this.showDuration = false
  101. } else {
  102. this.showDuration = true
  103. }
  104. } else {
  105. this.form.fc.setFieldsValue({
  106. [this.decorators.durationStandard[0]]: this.opts[0].key,
  107. })
  108. }
  109. }
  110. } else {
  111. this.opts = opts.map(item => {
  112. return {
  113. key: item,
  114. label: this.getLabel(item),
  115. }
  116. })
  117. }
  118. } else {
  119. this.opts = opts.map(item => {
  120. return {
  121. key: item,
  122. label: this.getLabel(item),
  123. }
  124. })
  125. }
  126. } catch (error) {
  127. console.log('error', error)
  128. this.loading = false
  129. this.opts = opts.map(item => {
  130. return {
  131. key: item,
  132. label: this.getLabel(item),
  133. }
  134. })
  135. }
  136. } else {
  137. this.opts = opts.map(item => {
  138. return {
  139. key: item,
  140. label: this.getLabel(item),
  141. }
  142. })
  143. }
  144. },
  145. getLabel (item) {
  146. return getDurationLabel(item)
  147. },
  148. change (e) {
  149. if (e.target.value === 'custom') {
  150. this.showDuration = true
  151. } else {
  152. this.showDuration = false
  153. }
  154. },
  155. },
  156. }
  157. </script>