AccountPrivileges.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div>
  3. <a-transfer
  4. :dataSource="dbList"
  5. :titles="[$t('db.text_193'), $t('db.text_194')]"
  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. privileges: {
  21. type: Array,
  22. },
  23. rdsItem: {
  24. type: Object,
  25. default: () => {
  26. return {}
  27. },
  28. },
  29. },
  30. data () {
  31. return {
  32. mockData: [],
  33. targetKeys: [],
  34. dbList: [],
  35. dbIdItemObj: {},
  36. propsPrivileges: {},
  37. }
  38. },
  39. created () {
  40. this.fetchQueryDBList()
  41. this.form.fc.getFieldDecorator('privileges', { preserve: true })
  42. if (this.privileges && this.privileges.length > 0) {
  43. this.privileges.forEach(item => {
  44. this.propsPrivileges[item.dbinstancedatabase_id] = item
  45. })
  46. }
  47. },
  48. methods: {
  49. async fetchQueryDBList () {
  50. try {
  51. const params = {
  52. scope: this.$store.getters.scope,
  53. limit: 0,
  54. dbinstance: this.rdsItem.id,
  55. }
  56. const manager = new this.$Manager('dbinstancedatabases', 'v2')
  57. const { status, data } = await manager.list({ params })
  58. if (status === 200 && data.total > 0) {
  59. const retList = data.data
  60. this.dbList = retList
  61. .filter(({ status, name }) => status === 'running')
  62. .map(item => {
  63. item.title = item.name
  64. this.dbIdItemObj[item.id] = item
  65. if (this.propsPrivileges[item.id]) {
  66. item = {
  67. ...item,
  68. ...this.propsPrivileges[item.id],
  69. }
  70. this.targetKeys.push(item.id)
  71. }
  72. item.key = item.id
  73. return item
  74. })
  75. this.setPrivileges()
  76. }
  77. } catch (err) {
  78. throw err
  79. }
  80. },
  81. renderItemRadions (item) {
  82. const { id } = item
  83. const { getFieldDecorator } = this.form.fc
  84. const renderRadios = ['rw', 'r'].map(v => {
  85. return <a-radio value={v}>{RDS_ACCOUNT_PRIVILEGES[v]}</a-radio>
  86. })
  87. const _handleChange = () => {
  88. this.$nextTick(() => {
  89. this.setPrivileges()
  90. })
  91. }
  92. const initialValue = (item.privileges === 'rw' || item.privileges === 'r') ? item.privileges : 'rw'
  93. return (
  94. <a-form-item class="radios">
  95. {
  96. getFieldDecorator(id, {
  97. initialValue,
  98. })(
  99. <a-radio-group onChange={_handleChange}>
  100. {renderRadios}
  101. </a-radio-group>,
  102. )
  103. }
  104. </a-form-item>
  105. )
  106. },
  107. renderItem (item) {
  108. const customLabel = (
  109. <span class="custom-item">
  110. <b>{item.title}</b> {this.renderItemRadions(item)}
  111. </span>
  112. )
  113. return {
  114. label: customLabel,
  115. value: item.title,
  116. }
  117. },
  118. setPrivileges () {
  119. const values = this.form.fc.getFieldsValue()
  120. this.form.fc.setFieldsValue({
  121. privileges: this.targetKeys.map(id => {
  122. return {
  123. database: this.dbIdItemObj[id].name,
  124. privilege: values[id] || this.dbIdItemObj[id].name,
  125. }
  126. }),
  127. })
  128. },
  129. handleChange (targetKeys) {
  130. this.targetKeys = targetKeys
  131. this.setPrivileges()
  132. },
  133. },
  134. }
  135. </script>
  136. <style lang="less">
  137. .ant-transfer {
  138. .radios {
  139. display: none;
  140. }
  141. .ant-transfer-list:last-child {
  142. .radios {
  143. display: block;
  144. margin-bottom: 0
  145. }
  146. }
  147. }
  148. .ant-transfer-list-content-item {
  149. display: flex;
  150. line-height: 30px;
  151. > span {
  152. flex: 1;
  153. }
  154. .ant-form-item-control{
  155. line-height: 28px
  156. }
  157. }
  158. .custom-item {
  159. width: 100%;
  160. display: flex;
  161. > b {
  162. flex: 1;
  163. }
  164. }
  165. </style>