role.vue 8.4 KB

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