authority.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <div>
  3. <div class="button-box clearflex">
  4. <el-button @click="addAuthority" type="primary">新增角色</el-button>
  5. </div>
  6. <el-table :data="tableData" border stripe>
  7. <el-table-column label="id" min-width="180" prop="ID"></el-table-column>
  8. <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
  9. <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
  10. <el-table-column fixed="right" label="操作" width="500">
  11. <template slot-scope="scope">
  12. <el-button @click="addAuthMenu(scope.row)" size="small" type="text">变更角色菜单</el-button>
  13. <el-button @click="addAuthApi(scope.row)" size="small" type="text">变更角色Api</el-button>
  14. <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
  15. </template>
  16. </el-table-column>
  17. </el-table>
  18. <el-pagination
  19. :current-page="page"
  20. :page-size="pageSize"
  21. :page-sizes="[10, 30, 50, 100]"
  22. :style="{float:'right',padding:'20px'}"
  23. :total="total"
  24. @current-change="handleCurrentChange"
  25. @size-change="handleSizeChange"
  26. layout="total, sizes, prev, pager, next, jumper"
  27. ></el-pagination>
  28. <!-- 新增角色弹窗 -->
  29. <el-dialog :visible.sync="dialogFormVisible" title="新增角色">
  30. <el-form :model="form">
  31. <el-form-item label="角色ID">
  32. <el-input autocomplete="off" v-model="form.authorityId"></el-input>
  33. </el-form-item>
  34. <el-form-item label="角色姓名">
  35. <el-input autocomplete="off" v-model="form.authorityName"></el-input>
  36. </el-form-item>
  37. </el-form>
  38. <div class="dialog-footer" slot="footer">
  39. <el-button @click="closeDialog">取 消</el-button>
  40. <el-button @click="enterDialog" type="primary">确 定</el-button>
  41. </div>
  42. </el-dialog>
  43. <!-- 关联menu弹窗 -->
  44. <el-dialog :visible.sync="menuDialogFlag" title="关联菜单">
  45. <el-tree
  46. :data="menuTreeData"
  47. :default-checked-keys="menuTreeIds"
  48. :props="menuDefaultProps"
  49. default-expand-all
  50. highlight-current
  51. node-key="ID"
  52. ref="menuTree"
  53. show-checkbox
  54. v-if="menuDialogFlag"
  55. ></el-tree>
  56. <div class="dialog-footer" slot="footer">
  57. <el-button @click="closeDialog">取 消</el-button>
  58. <el-button @click="relation" type="primary">确 定</el-button>
  59. </div>
  60. </el-dialog>
  61. <!-- 关联api弹窗 -->
  62. <el-dialog :visible.sync="apiDialogFlag" title="关联api">
  63. <el-tree
  64. :data="apiTreeData"
  65. :default-checked-keys="apiTreeIds"
  66. :props="apiDefaultProps"
  67. default-expand-all
  68. highlight-current
  69. node-key="ID"
  70. ref="apiTree"
  71. show-checkbox
  72. v-if="apiDialogFlag"
  73. ></el-tree>
  74. <div class="dialog-footer" slot="footer">
  75. <el-button @click="closeDialog">取 消</el-button>
  76. <el-button @click="authApiEnter" type="primary">确 定</el-button>
  77. </div>
  78. </el-dialog>
  79. </div>
  80. </template>
  81. <script>
  82. // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
  83. import {
  84. getAuthorityList,
  85. deleteAuthority,
  86. createAuthority
  87. } from '@/api/authority'
  88. import { getBaseMenuTree, addMenuAuthority, getMenuAuthority } from '@/api/menu'
  89. import { getAllApis, getAuthAndApi, setAuthAndApi } from '@/api/api'
  90. import infoList from '@/view/superAdmin/mixins/infoList'
  91. export default {
  92. name: 'Authority',
  93. mixins: [infoList],
  94. data() {
  95. return {
  96. listApi: getAuthorityList,
  97. listKey: 'list',
  98. activeUserId: 0,
  99. menuTreeData: [],
  100. menuTreeIds: [],
  101. menuDefaultProps: {
  102. children: 'children',
  103. label: 'nickName'
  104. },
  105. apiTreeData: [],
  106. apiTreeIds: [],
  107. apiDefaultProps: {
  108. children: 'children',
  109. label: 'description'
  110. },
  111. dialogFormVisible: false,
  112. menuDialogFlag: false,
  113. apiDialogFlag: false,
  114. form: {
  115. authorityId: '',
  116. authorityName: ''
  117. }
  118. }
  119. },
  120. methods: {
  121. // 删除角色
  122. deleteAuth(row) {
  123. this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
  124. confirmButtonText: '确定',
  125. cancelButtonText: '取消',
  126. type: 'warning'
  127. })
  128. .then(async () => {
  129. const res = await deleteAuthority({ authorityId: row.authorityId })
  130. if (res.success) {
  131. this.$message({
  132. type: 'success',
  133. message: '删除成功!'
  134. })
  135. this.getTableData()
  136. }
  137. })
  138. .catch(() => {
  139. this.$message({
  140. type: 'info',
  141. message: '已取消删除'
  142. })
  143. })
  144. },
  145. // 初始化表单
  146. initForm() {
  147. for (const key in this.form) {
  148. this.form[key] = ''
  149. }
  150. },
  151. // 关闭窗口
  152. closeDialog() {
  153. this.initForm()
  154. this.dialogFormVisible = false
  155. this.menuDialogFlag = false
  156. this.apiDialogFlag = false
  157. },
  158. // 确定弹窗
  159. async enterDialog() {
  160. const res = await createAuthority(this.form)
  161. if (res.success) {
  162. this.$message({
  163. type: 'success',
  164. message: '添加成功!'
  165. })
  166. this.getTableData()
  167. this.closeDialog()
  168. }
  169. this.initForm()
  170. this.dialogFormVisible = false
  171. },
  172. // 增加角色
  173. addAuthority() {
  174. this.dialogFormVisible = true
  175. },
  176. // 关联用户列表关系
  177. async addAuthMenu(row) {
  178. const res1 = await getMenuAuthority({ authorityId: row.authorityId })
  179. const menus = res1.data.menus
  180. const arr = []
  181. menus.map(item => {
  182. // 防止直接选中父级造成全选
  183. if (!menus.some(same => same.parentId === item.menuId)) {
  184. arr.push(Number(item.menuId))
  185. }
  186. })
  187. this.menuTreeIds = arr
  188. this.activeUserId = row.authorityId
  189. this.menuDialogFlag = true
  190. },
  191. // 关联树 确认方法
  192. async relation() {
  193. const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
  194. const res = await addMenuAuthority({
  195. menus: checkArr,
  196. authorityId: this.activeUserId
  197. })
  198. if (res.success) {
  199. this.$message({
  200. type: 'success',
  201. message: '添加成功!'
  202. })
  203. }
  204. this.closeDialog()
  205. },
  206. // 创建api树方法
  207. buildApiTree(apis) {
  208. const apiObj = new Object()
  209. apis &&
  210. apis.map(item => {
  211. if (apiObj.hasOwnProperty(item.group)) {
  212. apiObj[item.group].push(item)
  213. } else {
  214. Object.assign(apiObj, { [item.group]: [item] })
  215. }
  216. })
  217. const apiTree = []
  218. for (const key in apiObj) {
  219. const treeNode = {
  220. ID: key,
  221. description: key + '组',
  222. children: apiObj[key]
  223. }
  224. apiTree.push(treeNode)
  225. }
  226. return apiTree
  227. },
  228. // 关联用户api关系
  229. async addAuthApi(row) {
  230. const res = await getAuthAndApi({ authorityId: row.authorityId })
  231. this.activeUserId = row.authorityId
  232. this.apiTreeIds = res.data.apis || []
  233. this.apiDialogFlag = true
  234. },
  235. async authApiEnter() {
  236. const checkArr = this.$refs.apiTree.getCheckedKeys(true)
  237. const res = await setAuthAndApi({
  238. authorityId: this.activeUserId,
  239. apiIds: checkArr
  240. })
  241. if (res.success) {
  242. this.$message({
  243. type: 'success',
  244. message: '添加成功!'
  245. })
  246. }
  247. this.closeDialog()
  248. }
  249. },
  250. async created() {
  251. // 获取所有菜单树
  252. const res = await getBaseMenuTree()
  253. this.menuTreeData = res.data.menus
  254. // 获取api并整理成树结构
  255. const res2 = await getAllApis()
  256. const apis = res2.data.apis
  257. this.apiTreeData = this.buildApiTree(apis)
  258. }
  259. }
  260. </script>
  261. <style scoped lang="scss">
  262. .button-box {
  263. padding: 10px 20px;
  264. .el-button {
  265. float: right;
  266. }
  267. }
  268. </style>