后端

role-add-or-update.vue 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.id ? '新增' : '修改'"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible">
  6. <el-form :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="true"
  20. show-checkbox>
  21. </el-tree>
  22. </el-form-item>
  23. </el-form>
  24. <span slot="footer" class="dialog-footer">
  25. <el-button @click="visible = false">取消</el-button>
  26. <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  27. </span>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import { treeDataTranslate } from '@/utils'
  32. export default {
  33. data () {
  34. return {
  35. visible: false,
  36. menuList: [],
  37. menuListTreeProps: {
  38. label: 'name',
  39. children: 'children'
  40. },
  41. dataForm: {
  42. id: 0,
  43. roleName: '',
  44. remark: ''
  45. },
  46. dataRule: {
  47. roleName: [
  48. { required: true, message: '角色名称不能为空', trigger: 'blur' }
  49. ]
  50. },
  51. tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
  52. }
  53. },
  54. methods: {
  55. init (id) {
  56. this.dataForm.id = id || 0
  57. this.$http({
  58. url: this.$http.adornUrl('/sys/menu/list'),
  59. method: 'get',
  60. params: this.$http.adornParams()
  61. }).then(({data}) => {
  62. this.menuList = treeDataTranslate(data, 'menuId')
  63. }).then(() => {
  64. this.visible = true
  65. this.$nextTick(() => {
  66. this.$refs['dataForm'].resetFields()
  67. this.$refs.menuListTree.setCheckedKeys([])
  68. })
  69. }).then(() => {
  70. if (this.dataForm.id) {
  71. this.$http({
  72. url: this.$http.adornUrl(`/sys/role/info/${this.dataForm.id}`),
  73. method: 'get',
  74. params: this.$http.adornParams()
  75. }).then(({data}) => {
  76. if (data && data.code === 0) {
  77. this.dataForm.roleName = data.role.roleName
  78. this.dataForm.remark = data.role.remark
  79. var idx = data.role.menuIdList.indexOf(this.tempKey)
  80. if (idx !== -1) {
  81. data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx)
  82. }
  83. this.$refs.menuListTree.setCheckedKeys(data.role.menuIdList)
  84. }
  85. })
  86. }
  87. })
  88. },
  89. // 表单提交
  90. dataFormSubmit () {
  91. this.$refs['dataForm'].validate((valid) => {
  92. if (valid) {
  93. this.$http({
  94. url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`),
  95. method: 'post',
  96. data: this.$http.adornData({
  97. 'roleId': this.dataForm.id || undefined,
  98. 'roleName': this.dataForm.roleName,
  99. 'remark': this.dataForm.remark,
  100. 'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())
  101. })
  102. }).then(({data}) => {
  103. if (data && data.code === 0) {
  104. this.$message({
  105. message: '操作成功',
  106. type: 'success',
  107. duration: 1500,
  108. onClose: () => {
  109. this.visible = false
  110. this.$emit('refreshDataList')
  111. }
  112. })
  113. } else {
  114. this.$message.error(data.msg)
  115. }
  116. })
  117. }
  118. })
  119. }
  120. }
  121. }
  122. </script>