roleAdmin.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div class="role-admin">
  3. <!-- :body-style="{padding: '24px 10px 24px 32px'}" -->
  4. <a-card
  5. :bordered="false"
  6. title="角色面板"
  7. >
  8. <template slot="extra">
  9. <a-button type="primary" @click="openModal({id: '', label: ''}, 0, 'add')">新增</a-button>
  10. </template>
  11. <div class="role-list">
  12. <div v-for="(item, index) in roleList" :key="item.id">
  13. <div class="['role-item']" >
  14. <a-row>
  15. <a-col :span="14" @click="selectRole(item)">
  16. <a-icon v-show="roleId === item.id" type="check-circle" theme="twoTone" two-tone-color="#52c41a" />
  17. <span class="cp"> {{ item.label }}</span>
  18. </a-col>
  19. <a-col :span="10">
  20. <a @click="openModal(item, index, 'edit')">重命名</a>
  21. <a-divider type="vertical" />
  22. <a-popconfirm
  23. title="你确定要删除这个角色吗?"
  24. ok-text="Yes"
  25. cancel-text="No"
  26. @confirm="deleteRole(item)"
  27. >
  28. <a> 删除</a>
  29. </a-popconfirm>
  30. </a-col>
  31. </a-row>
  32. </div>
  33. <a-divider />
  34. </div>
  35. </div>
  36. </a-card>
  37. <!-- 重命名 or 新增 角色弹窗 -->
  38. <a-modal
  39. :title="modalType | filtersModalType"
  40. :visible="visible"
  41. :confirm-loading="confirmLoading"
  42. @ok="postRole"
  43. @cancel="visible= false"
  44. >
  45. <a-input v-model="form.label" :placeholder="modalType | filtersModalType " ></a-input>
  46. </a-modal>
  47. </div>
  48. </template>
  49. <script>
  50. import { postRole, getRole, deleteRole } from '@/api/role'
  51. const roleList = []
  52. for (let i = 0; i < 10; i++) {
  53. roleList.push({
  54. label: `校长${i}`,
  55. id: i
  56. })
  57. }
  58. export default {
  59. name: 'RoleAdmin',
  60. filters: {
  61. filtersModalType (type) {
  62. switch (type) {
  63. case 'add':
  64. return '新增标签名'
  65. case 'edit':
  66. return '编辑角色名'
  67. }
  68. }
  69. },
  70. mounted () {
  71. this.getRole()
  72. },
  73. watch: {
  74. roleId () {
  75. this.$emit('changeRole', this.roleId)
  76. }
  77. },
  78. data () {
  79. return {
  80. modalType: 'add', // 编辑 edit 或者 新增 add
  81. visible: false,
  82. confirmLoading: false,
  83. roleList: [],
  84. form: {
  85. label: '', // 新增或者修改的字段
  86. id: ''
  87. },
  88. roleId: ''
  89. }
  90. },
  91. methods: {
  92. // 选择一个角色
  93. selectRole (records) {
  94. this.roleId = records.id
  95. },
  96. // 删除角色
  97. async deleteRole (records) {
  98. const { code, data } = await deleteRole(records.id)
  99. if (code === 0) {
  100. console.log(data)
  101. data ? this.$message.success('删除成功') : this.$message.error('删除失败')
  102. }
  103. this.getRole()
  104. },
  105. // 添加角色
  106. async postRole () {
  107. const $par = {
  108. label: this.form.label,
  109. id: this.form.id,
  110. type: this.modalType
  111. }
  112. this.confirmLoadin = true
  113. const { code, data } = await postRole($par)
  114. this.confirmLoading = false
  115. if (code === 0) {
  116. console.log(data)
  117. }
  118. this.getRole()
  119. this.visible = false
  120. },
  121. // 查询角色
  122. async getRole () {
  123. const { code, data } = await getRole({ curPage: 1 })
  124. if (code === 0) {
  125. const { records } = data
  126. console.log(data)
  127. this.roleList = records
  128. this.roleId = this.roleList[0].id
  129. }
  130. },
  131. handleOk (e) {
  132. this.confirmLoading = true
  133. setTimeout(() => {
  134. this.visible = false
  135. this.confirmLoading = false
  136. }, 2000)
  137. },
  138. // 打开弹窗
  139. openModal (record, index, type) {
  140. this.modalType = type
  141. this.visible = true
  142. this.form.label = record.label
  143. this.form.id = record.id
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped lang="less">
  149. @import '~ant-design-vue/es/style/themes/default.less';
  150. // .role-admin {
  151. // min-height: 100vh;
  152. // }
  153. .role-item {
  154. }
  155. .active {
  156. // color: @primary-color;
  157. }
  158. </style>