edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div class="app-container roles-edit">
  3. <el-form ref="ruleRef" label-width="100px" :model="role" :rules="rules">
  4. <el-form-item label="角色名称:" prop="roleName">
  5. <el-input v-model="role.roleName" placeholder="角色名称" maxlength="20" />
  6. </el-form-item>
  7. <el-form-item label="角色描述:" prop="roleDesc">
  8. <el-input v-model="role.roleDesc" placeholder="角色描述" maxlength="20" />
  9. </el-form-item>
  10. <el-form-item label="角色授权:" prop="menuIds">
  11. <el-input v-show="false" v-model="role.menuIds" />
  12. <el-tree
  13. ref="tree"
  14. :data="menuTree"
  15. show-checkbox
  16. default-expand-all
  17. node-key="id"
  18. highlight-current
  19. :props="defaultProps"
  20. @check-change="getCheckedKeys"
  21. />
  22. </el-form-item>
  23. <el-form-item>
  24. <!-- 确认 取消 -->
  25. <div class="control-box">
  26. <el-button type="warning" @click="$_back">返回</el-button>
  27. <el-button type="primary" @click="handleSubmit">保存</el-button>
  28. </div>
  29. </el-form-item>
  30. </el-form>
  31. </div>
  32. </template>
  33. <script>
  34. import { sysMenuTree, createRole, getRole, updateRole } from '@/api/system'
  35. export default {
  36. data() {
  37. return {
  38. editType: '',
  39. role: {
  40. id: '',
  41. roleName: '',
  42. roleDesc: '',
  43. menuIds: ''
  44. },
  45. rules: {
  46. roleName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
  47. menuIds: [{ required: true, message: '角色授权不能为空', trigger: 'change' }]
  48. },
  49. menuTree: [],
  50. defaultProps: {
  51. children: 'subMenus',
  52. label: 'title'
  53. }
  54. }
  55. },
  56. created() {
  57. this.editType = this.$route.query.type
  58. if (this.editType === 'edit') {
  59. this.role.id = parseInt(this.$route.query.id)
  60. this.fetchRoleInfo()
  61. }
  62. this.getMenuTree()
  63. },
  64. methods: {
  65. getCheckedNodes() {
  66. console.log(this.$refs.tree.getCheckedNodes())
  67. },
  68. getCheckedKeys() {
  69. // console.log(this.$refs.tree.getCheckedNodes(false, true).map(item => item.id))
  70. this.role.menuIds = this.$refs.tree.getCheckedNodes(false, true).map(item => item.id).join(',')
  71. },
  72. // 获取菜单权限列表
  73. getMenuTree() {
  74. sysMenuTree({ menuType: 1 }).then(res => {
  75. this.menuTree = res.data
  76. })
  77. },
  78. // 设置角色信息
  79. setRoleInfo(data) {
  80. for (const key in this.role) {
  81. if (Object.hasOwnProperty.call(this.role, key)) {
  82. this.role[key] = data[key]
  83. }
  84. }
  85. console.log(this.role.menuIds.split(','))
  86. this.$refs.tree.setCheckedKeys([...this.role.menuIds.split(',')])
  87. },
  88. // 获取角色信息
  89. fetchRoleInfo() {
  90. getRole(this.role.id).then(res => {
  91. console.log(res.data)
  92. this.setRoleInfo(res.data)
  93. })
  94. },
  95. // 修改角色
  96. updateRole() {
  97. updateRole(this.role.id, this.role).then(res => {
  98. console.log(res)
  99. this.$message.success('修改角色成功')
  100. this.$store.dispatch('tagsView/delView', this.$route)
  101. this.$router.back()
  102. })
  103. },
  104. // 创建角色
  105. createRole() {
  106. createRole(this.role).then(res => {
  107. console.log(res)
  108. this.$message.success('添加角色成功')
  109. this.$store.dispatch('tagsView/delView', this.$route)
  110. this.$router.back()
  111. })
  112. },
  113. // 提交
  114. handleSubmit() {
  115. this.$refs.ruleRef.validate(valide => {
  116. if (!valide) return
  117. if (this.editType === 'add') {
  118. this.createRole()
  119. } else {
  120. this.updateRole()
  121. }
  122. })
  123. }
  124. }
  125. }
  126. </script>
  127. <style scoped lang="scss">
  128. .roles-edit {
  129. width: 600px;
  130. margin: 0 auto;
  131. margin-top: 40px;
  132. }
  133. .control-box {
  134. margin: 20px 0;
  135. text-align: center;
  136. .el-button {
  137. width: 120px;
  138. margin: 0 8px;
  139. }
  140. }
  141. </style>