DatabasePrivileges.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div>
  3. <a-transfer
  4. :dataSource="dbList"
  5. :titles="[$t('db.text_229'), $t('db.text_230')]"
  6. :listStyle="{
  7. width: '332px',
  8. height: '332px',
  9. }"
  10. :render="renderItem"
  11. :targetKeys="targetKeys"
  12. @change="handleChange" />
  13. </div>
  14. </template>
  15. <script>
  16. import { RDS_ACCOUNT_PRIVILEGES } from '@DB/constants'
  17. export default {
  18. inject: ['form'],
  19. props: {
  20. rdsItem: {
  21. type: Object,
  22. default: () => {
  23. return {}
  24. },
  25. },
  26. },
  27. data () {
  28. return {
  29. mockData: [],
  30. targetKeys: [],
  31. dbList: [],
  32. dbIdItemObj: {},
  33. }
  34. },
  35. created () {
  36. this.fetchQueryAccountList()
  37. this.form.fc.getFieldDecorator('accounts', { preserve: true })
  38. },
  39. methods: {
  40. async fetchQueryAccountList () {
  41. try {
  42. const params = {
  43. scope: this.$store.getters.scope,
  44. limit: 0,
  45. dbinstance: this.rdsItem.id,
  46. }
  47. const manager = new this.$Manager('dbinstanceaccounts', 'v2')
  48. const { status, data } = await manager.list({ params })
  49. if (status === 200 && data.total > 0) {
  50. const retList = data.data
  51. this.dbList = retList
  52. .filter(({ status, name }) => status === 'available' && name !== 'root')
  53. .map(item => {
  54. item.title = item.name
  55. item.key = item.id
  56. this.dbIdItemObj[item.id] = item
  57. return item
  58. })
  59. }
  60. } catch (err) {
  61. throw err
  62. }
  63. },
  64. renderItemRadions (item) {
  65. const { id } = item
  66. const { getFieldDecorator } = this.form.fc
  67. const renderRadios = ['rw', 'r'].map(v => {
  68. return <a-radio value={v}>{RDS_ACCOUNT_PRIVILEGES[v]}</a-radio>
  69. })
  70. const _handleChange = () => {
  71. this.$nextTick(() => {
  72. this.setPrivileges()
  73. })
  74. }
  75. return (
  76. <a-form-item class="radios">
  77. {
  78. getFieldDecorator(id, {
  79. initialValue: item.privileges || 'rw',
  80. })(
  81. <a-radio-group onChange={_handleChange}>
  82. {renderRadios}
  83. </a-radio-group>,
  84. )
  85. }
  86. </a-form-item>
  87. )
  88. },
  89. renderItem (item) {
  90. const customLabel = (
  91. <span class="custom-item">
  92. <b>{item.title}</b> {this.renderItemRadions(item)}
  93. </span>
  94. )
  95. return {
  96. label: customLabel, // for displayed item
  97. value: item.title, // for title and filter matching
  98. }
  99. },
  100. setPrivileges () {
  101. const values = this.form.fc.getFieldsValue()
  102. this.form.fc.setFieldsValue({
  103. accounts: this.targetKeys.map(id => {
  104. return {
  105. account: this.dbIdItemObj[id].name,
  106. privilege: values[id],
  107. }
  108. }),
  109. })
  110. },
  111. handleChange (targetKeys) {
  112. this.targetKeys = targetKeys
  113. this.setPrivileges()
  114. },
  115. },
  116. }
  117. </script>
  118. <style lang="less">
  119. .ant-transfer {
  120. .radios {
  121. display: none;
  122. }
  123. .ant-transfer-list:last-child {
  124. .radios {
  125. display: block;
  126. margin-bottom: 0
  127. }
  128. }
  129. }
  130. .ant-transfer-list-content-item {
  131. display: flex;
  132. > span {
  133. flex: 1;
  134. }
  135. .ant-form-item-control{
  136. line-height: 28px
  137. }
  138. }
  139. .custom-item {
  140. width: 100%;
  141. display: flex;
  142. > b {
  143. flex: 1;
  144. }
  145. }
  146. </style>