authority.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div class="authority">
  3. <div class="button-box clearflex">
  4. <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
  5. </div>
  6. <el-table
  7. :data="tableData"
  8. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  9. border
  10. row-key="authorityId"
  11. stripe
  12. style="width: 100%"
  13. >
  14. <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
  15. <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
  16. <el-table-column fixed="right" label="操作" min-width="300">
  17. <template slot-scope="scope">
  18. <el-button @click="opdendrawer(scope.row)" size="small" type="text">设置权限</el-button>
  19. <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
  20. <el-button @click="addAuthority(scope.row.authorityId)" size="small" type="text">新增子角色</el-button>
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. <!-- 新增角色弹窗 -->
  25. <el-dialog :visible.sync="dialogFormVisible" title="新增角色">
  26. <el-form :model="form" :rules="rules" ref="authorityForm">
  27. <el-form-item label="父级角色ID" prop="parentId">
  28. <el-input autocomplete="off" disabled v-model="form.parentId"></el-input>
  29. </el-form-item>
  30. <el-form-item label="角色ID" prop="authorityId">
  31. <el-input autocomplete="off" v-model="form.authorityId"></el-input>
  32. </el-form-item>
  33. <el-form-item label="角色姓名" prop="authorityName">
  34. <el-input autocomplete="off" v-model="form.authorityName"></el-input>
  35. </el-form-item>
  36. </el-form>
  37. <div class="dialog-footer" slot="footer">
  38. <el-button @click="closeDialog">取 消</el-button>
  39. <el-button @click="enterDialog" type="primary">确 定</el-button>
  40. </div>
  41. </el-dialog>
  42. <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
  43. <el-tabs class="role-box" type="border-card">
  44. <el-tab-pane label="角色菜单">
  45. <Menus :row="activeRow" />
  46. </el-tab-pane>
  47. <el-tab-pane label="角色api">
  48. <apis :row="activeRow" />
  49. </el-tab-pane>
  50. <el-tab-pane label="资源权限">
  51. <Datas :authority="tableData" :row="activeRow" />
  52. </el-tab-pane>
  53. </el-tabs>
  54. </el-drawer>
  55. </div>
  56. </template>
  57. <script>
  58. // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
  59. import {
  60. getAuthorityList,
  61. deleteAuthority,
  62. createAuthority
  63. } from '@/api/authority'
  64. import Menus from '@/view/superAdmin/authority/components/menus'
  65. import Apis from '@/view/superAdmin/authority/components/apis'
  66. import Datas from '@/view/superAdmin/authority/components/datas'
  67. import infoList from '@/components/mixins/infoList'
  68. export default {
  69. name: 'Authority',
  70. mixins: [infoList],
  71. data() {
  72. return {
  73. listApi: getAuthorityList,
  74. listKey: 'list',
  75. drawer: false,
  76. activeRow: {},
  77. activeUserId: 0,
  78. dialogFormVisible: false,
  79. apiDialogFlag: false,
  80. form: {
  81. authorityId: '',
  82. authorityName: '',
  83. parentId: '0'
  84. },
  85. rules: {
  86. authorityId: [
  87. { required: true, message: '请输入角色ID', trigger: 'blur' }
  88. ],
  89. authorityName: [
  90. { required: true, message: '请输入角色名', trigger: 'blur' }
  91. ],
  92. parentId: [
  93. { required: true, message: '请选择请求方式', trigger: 'blur' }
  94. ]
  95. }
  96. }
  97. },
  98. components: {
  99. Menus,
  100. Apis,
  101. Datas
  102. },
  103. methods: {
  104. opdendrawer(row) {
  105. this.drawer = true
  106. this.activeRow = row
  107. },
  108. // 删除角色
  109. deleteAuth(row) {
  110. this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
  111. confirmButtonText: '确定',
  112. cancelButtonText: '取消',
  113. type: 'warning'
  114. })
  115. .then(async () => {
  116. const res = await deleteAuthority({ authorityId: row.authorityId })
  117. if (res.code == 0) {
  118. this.$message({
  119. type: 'success',
  120. message: '删除成功!'
  121. })
  122. this.getTableData()
  123. }
  124. })
  125. .catch(() => {
  126. this.$message({
  127. type: 'info',
  128. message: '已取消删除'
  129. })
  130. })
  131. },
  132. // 初始化表单
  133. initForm() {
  134. for (const key in this.form) {
  135. this.form[key] = ''
  136. }
  137. },
  138. // 关闭窗口
  139. closeDialog() {
  140. this.initForm()
  141. this.dialogFormVisible = false
  142. this.apiDialogFlag = false
  143. },
  144. // 确定弹窗
  145. async enterDialog() {
  146. if (this.form.authorityId == '0') {
  147. this.$message({
  148. type: 'error',
  149. message: '角色id不能为0'
  150. })
  151. return false
  152. }
  153. this.$refs.authorityForm.validate(async valid => {
  154. if (valid) {
  155. const res = await createAuthority(this.form)
  156. if (res.code == 0) {
  157. this.$message({
  158. type: 'success',
  159. message: '添加成功!'
  160. })
  161. this.getTableData()
  162. this.closeDialog()
  163. }
  164. this.initForm()
  165. this.dialogFormVisible = false
  166. }
  167. })
  168. },
  169. // 增加角色
  170. addAuthority(parentId) {
  171. this.form.parentId = parentId
  172. this.dialogFormVisible = true
  173. }
  174. },
  175. created() {
  176. this.pageSize = 999
  177. }
  178. }
  179. </script>
  180. <style lang="scss">
  181. .authority {
  182. .button-box {
  183. padding: 10px 20px;
  184. .el-button {
  185. float: right;
  186. }
  187. }
  188. }
  189. .role-box {
  190. .el-tabs__content {
  191. height: calc(100vh - 150px);
  192. overflow: auto;
  193. }
  194. }
  195. </style>