role.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <div class="app-container">
  3. <el-button type="primary" @click="handleAddRole">New Role</el-button>
  4. <el-table :data="rolesList" style="width: 100%;margin-top:30px;" border>
  5. <el-table-column align="center" label="Role Key" width="220">
  6. <template slot-scope="scope">
  7. {{ scope.row.key }}
  8. </template>
  9. </el-table-column>
  10. <el-table-column align="center" label="Role Name" width="220">
  11. <template slot-scope="scope">
  12. {{ scope.row.name }}
  13. </template>
  14. </el-table-column>
  15. <el-table-column align="header-center" label="Description">
  16. <template slot-scope="scope">
  17. {{ scope.row.description }}
  18. </template>
  19. </el-table-column>
  20. <el-table-column align="center" label="Operations">
  21. <template slot-scope="scope">
  22. <el-button type="primary" size="small" @click="handleEdit(scope)">Edit</el-button>
  23. <el-button type="danger" size="small" @click="handleDelete(scope)">Delete</el-button>
  24. </template>
  25. </el-table-column>
  26. </el-table>
  27. <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'Edit Role':'New Role'">
  28. <el-form :model="role" label-width="80px" label-position="left">
  29. <el-form-item label="Name">
  30. <el-input v-model="role.name" placeholder="Role Name" />
  31. </el-form-item>
  32. <el-form-item label="Desc">
  33. <el-input
  34. v-model="role.description"
  35. :autosize="{ minRows: 2, maxRows: 4}"
  36. type="textarea"
  37. placeholder="Role Description"
  38. />
  39. </el-form-item>
  40. <el-form-item label="Menus">
  41. <el-tree
  42. ref="tree"
  43. :check-strictly="checkStrictly"
  44. :data="routesData"
  45. :props="defaultProps"
  46. show-checkbox
  47. node-key="path"
  48. class="permission-tree"
  49. />
  50. </el-form-item>
  51. </el-form>
  52. <div style="text-align:right;">
  53. <el-button type="danger" @click="dialogVisible=false">Cancel</el-button>
  54. <el-button type="primary" @click="confirmRole">Confirm</el-button>
  55. </div>
  56. </el-dialog>
  57. </div>
  58. </template>
  59. <script>
  60. import path from 'path'
  61. import { deepClone } from '@/utils'
  62. import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
  63. const defaultRole = {
  64. key: '',
  65. name: '',
  66. description: '',
  67. routes: []
  68. }
  69. export default {
  70. data() {
  71. return {
  72. role: Object.assign({}, defaultRole),
  73. routes: [],
  74. rolesList: [],
  75. dialogVisible: false,
  76. dialogType: 'new',
  77. checkStrictly: false,
  78. defaultProps: {
  79. children: 'children',
  80. label: 'title'
  81. }
  82. }
  83. },
  84. computed: {
  85. routesData() {
  86. return this.routes
  87. }
  88. },
  89. created() {
  90. // Mock: get all routes and roles list from server
  91. this.getRoutes()
  92. this.getRoles()
  93. },
  94. methods: {
  95. async getRoutes() {
  96. const res = await getRoutes()
  97. this.serviceRoutes = res.data
  98. this.routes = this.generateRoutes(res.data)
  99. },
  100. async getRoles() {
  101. const res = await getRoles()
  102. this.rolesList = res.data
  103. },
  104. // Reshape the routes structure so that it looks the same as the sidebar
  105. generateRoutes(routes, basePath = '/') {
  106. const res = []
  107. for (let route of routes) {
  108. // skip some route
  109. if (route.hidden) { continue }
  110. const onlyOneShowingChild = this.onlyOneShowingChild(route.children, route)
  111. if (route.children && onlyOneShowingChild && !route.alwaysShow) {
  112. route = onlyOneShowingChild
  113. }
  114. const data = {
  115. path: path.resolve(basePath, route.path),
  116. title: route.meta && route.meta.title
  117. }
  118. // recursive child routes
  119. if (route.children) {
  120. data.children = this.generateRoutes(route.children, data.path)
  121. }
  122. res.push(data)
  123. }
  124. return res
  125. },
  126. generateArr(routes) {
  127. let data = []
  128. routes.forEach(route => {
  129. data.push(route)
  130. if (route.children) {
  131. const temp = this.generateArr(route.children)
  132. if (temp.length > 0) {
  133. data = [...data, ...temp]
  134. }
  135. }
  136. })
  137. return data
  138. },
  139. handleAddRole() {
  140. this.role = Object.assign({}, defaultRole)
  141. if (this.$refs.tree) {
  142. this.$refs.tree.setCheckedNodes([])
  143. }
  144. this.dialogType = 'new'
  145. this.dialogVisible = true
  146. },
  147. handleEdit(scope) {
  148. this.dialogType = 'edit'
  149. this.dialogVisible = true
  150. this.checkStrictly = true
  151. this.role = deepClone(scope.row)
  152. this.$nextTick(() => {
  153. const routes = this.generateRoutes(this.role.routes)
  154. this.$refs.tree.setCheckedNodes(this.generateArr(routes))
  155. // set checked state of a node not affects its father and child nodes
  156. this.checkStrictly = false
  157. })
  158. },
  159. handleDelete({ $index, row }) {
  160. this.$confirm('Confirm to remove the role?', 'Warning', {
  161. confirmButtonText: 'Confirm',
  162. cancelButtonText: 'Cancel',
  163. type: 'warning'
  164. })
  165. .then(async() => {
  166. await deleteRole(row.key)
  167. this.rolesList.splice($index, 1)
  168. this.$message({
  169. type: 'success',
  170. message: 'Delete succed!'
  171. })
  172. })
  173. .catch(err => { console.error(err) })
  174. },
  175. generateTree(routes, basePath = '/', checkedKeys) {
  176. const res = []
  177. for (const route of routes) {
  178. const routePath = path.resolve(basePath, route.path)
  179. // recursive child routes
  180. if (route.children) {
  181. route.children = this.generateTree(route.children, routePath, checkedKeys)
  182. }
  183. if (checkedKeys.includes(routePath) || (route.children && route.children.length >= 1)) {
  184. res.push(route)
  185. }
  186. }
  187. return res
  188. },
  189. async confirmRole() {
  190. const isEdit = this.dialogType === 'edit'
  191. const checkedKeys = this.$refs.tree.getCheckedKeys()
  192. this.role.routes = this.generateTree(deepClone(this.serviceRoutes), '/', checkedKeys)
  193. if (isEdit) {
  194. await updateRole(this.role.key, this.role)
  195. for (let index = 0; index < this.rolesList.length; index++) {
  196. if (this.rolesList[index].key === this.role.key) {
  197. this.rolesList.splice(index, 1, Object.assign({}, this.role))
  198. break
  199. }
  200. }
  201. } else {
  202. const { data } = await addRole(this.role)
  203. this.role.key = data.key
  204. this.rolesList.push(this.role)
  205. }
  206. const { description, key, name } = this.role
  207. this.dialogVisible = false
  208. this.$notify({
  209. title: 'Success',
  210. dangerouslyUseHTMLString: true,
  211. message: `
  212. <div>Role Key: ${key}</div>
  213. <div>Role Name: ${name}</div>
  214. <div>Description: ${description}</div>
  215. `,
  216. type: 'success'
  217. })
  218. },
  219. // reference: src/view/layout/components/Sidebar/SidebarItem.vue
  220. onlyOneShowingChild(children = [], parent) {
  221. let onlyOneChild = null
  222. const showingChildren = children.filter(item => !item.hidden)
  223. // When there is only one child route, the child route is displayed by default
  224. if (showingChildren.length === 1) {
  225. onlyOneChild = showingChildren[0]
  226. onlyOneChild.path = path.resolve(parent.path, onlyOneChild.path)
  227. return onlyOneChild
  228. }
  229. // Show parent if there are no child route to display
  230. if (showingChildren.length === 0) {
  231. onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  232. return onlyOneChild
  233. }
  234. return false
  235. }
  236. }
  237. }
  238. </script>
  239. <style lang="scss" scoped>
  240. .app-container {
  241. .roles-table {
  242. margin-top: 30px;
  243. }
  244. .permission-tree {
  245. margin-bottom: 30px;
  246. }
  247. }
  248. </style>