赫艾前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.9 KiB

5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
1 week ago
5 years ago
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.id ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible">
  6. <el-form v-loading="dialogLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
  7. <el-form-item label="角色名称" prop="roleName">
  8. <el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
  9. </el-form-item>
  10. <el-form-item label="备注" prop="remark">
  11. <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
  12. </el-form-item>
  13. <el-form-item size="mini" label="授权">
  14. <el-tree
  15. :data="menuList"
  16. :props="menuListTreeProps"
  17. node-key="menuId"
  18. ref="menuListTree"
  19. :default-expand-all="false"
  20. :expand-on-click-node="false"
  21. show-checkbox>
  22. </el-tree>
  23. </el-form-item>
  24. </el-form>
  25. <span slot="footer" class="dialog-footer">
  26. <el-button @click="visible = false">取消</el-button>
  27. <el-button type="primary" :disabled="dialogLoading" @click="dataFormSubmit()">确定</el-button>
  28. </span>
  29. </el-dialog>
  30. </template>
  31. <script>
  32. import { treeDataTranslate } from '@/utils'
  33. export default {
  34. data () {
  35. return {
  36. visible: false,
  37. dialogLoading: false,
  38. menuList: [],
  39. menuListTreeProps: {
  40. label: 'name',
  41. children: 'children'
  42. },
  43. dataForm: {
  44. id: 0,
  45. roleName: '',
  46. remark: ''
  47. },
  48. dataRule: {
  49. roleName: [
  50. { required: true, message: '角色名称不能为空', trigger: 'blur' }
  51. ]
  52. },
  53. tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
  54. }
  55. },
  56. methods: {
  57. init (id) {
  58. this.dataForm.id = id || 0
  59. this.dataForm.roleName = ''
  60. this.dataForm.remark = ''
  61. this.menuList = []
  62. this.visible = true
  63. this.dialogLoading = true
  64. this.$nextTick(() => {
  65. if (this.$refs['dataForm']) {
  66. this.$refs['dataForm'].clearValidate()
  67. }
  68. if (this.$refs.menuListTree) {
  69. this.$refs.menuListTree.setCheckedKeys([])
  70. }
  71. })
  72. const menuPromise = this.$http({
  73. url: this.$http.adornUrl('/sys/menu/list'),
  74. method: 'get',
  75. params: this.$http.adornParams()
  76. })
  77. const rolePromise = this.dataForm.id
  78. ? this.$http({
  79. url: this.$http.adornUrl(`/sys/role/info/${this.dataForm.id}`),
  80. method: 'get',
  81. params: this.$http.adornParams()
  82. })
  83. : Promise.resolve({data: null})
  84. // 并行加载菜单与角色详情,减少弹窗等待时长 - rqrq
  85. Promise.all([menuPromise, rolePromise]).then(([menuRes, roleRes]) => {
  86. this.menuList = treeDataTranslate(menuRes.data || [], 'menuId')
  87. let checkedMenuIds = []
  88. if (this.dataForm.id && roleRes.data && roleRes.data.code === 0) {
  89. this.dataForm.roleName = roleRes.data.role.roleName
  90. this.dataForm.remark = roleRes.data.role.remark
  91. checkedMenuIds = (roleRes.data.role.menuIdList || []).slice()
  92. const idx = checkedMenuIds.indexOf(this.tempKey)
  93. if (idx !== -1) {
  94. checkedMenuIds = checkedMenuIds.slice(0, idx)
  95. }
  96. }
  97. this.$nextTick(() => {
  98. if (this.$refs.menuListTree) {
  99. this.$refs.menuListTree.setCheckedKeys(checkedMenuIds)
  100. }
  101. })
  102. }).catch(() => {
  103. this.$message.error('权限数据加载失败')
  104. }).finally(() => {
  105. this.dialogLoading = false
  106. })
  107. },
  108. // 表单提交
  109. dataFormSubmit () {
  110. this.$refs['dataForm'].validate((valid) => {
  111. if (valid) {
  112. this.$http({
  113. url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`),
  114. method: 'post',
  115. data: this.$http.adornData({
  116. 'roleId': this.dataForm.id || undefined,
  117. 'roleName': this.dataForm.roleName,
  118. 'remark': this.dataForm.remark,
  119. 'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())
  120. })
  121. }).then(({data}) => {
  122. if (data && data.code === 0) {
  123. this.$message({
  124. message: '操作成功',
  125. type: 'success',
  126. duration: 1500,
  127. onClose: () => {
  128. this.visible = false
  129. this.$emit('refreshDataList')
  130. }
  131. })
  132. } else {
  133. this.$message.error(data.msg)
  134. }
  135. })
  136. }
  137. })
  138. }
  139. }
  140. }
  141. </script>