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.
WeChat/wx-manage-master/src/views/modules/sys/role-add-or-update.vue

148 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<!-- 弹窗组件显示角色的新增或修改表单 -->
<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
<!-- 表单组件绑定数据和验证规则 -->
<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!-- 角色名称输入框 -->
<el-form-item label="角色名称" prop="roleName">
<el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
</el-form-item>
<!-- 备注输入框 -->
<el-form-item label="备注" prop="remark">
<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
</el-form-item>
<!-- 授权菜单树 -->
<el-form-item size="mini" label="授权">
<el-tree :data="menuList" :props="menuListTreeProps" node-key="menuId" ref="menuListTree" :default-expand-all="true" show-checkbox>
</el-tree>
</el-form-item>
</el-form>
<!-- 弹窗底部按钮,取消和确认按钮 -->
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">取消</el-button>
<el-button type="primary" @click="dataFormSubmit()"></el-button>
</span>
</el-dialog>
</template>
<script>
// 导入工具函数,用于将树形数据转换
import { treeDataTranslate } from '@/utils'
export default {
data() {
return {
// 弹窗显示状态
visible: false,
// 菜单列表,用于树形展示
menuList: [],
// 用于el-tree组件的树形数据配置
menuListTreeProps: {
label: 'name', // 显示名称字段
children: 'children' // 子节点字段
},
// 表单数据
dataForm: {
id: 0, // 角色ID新增时为0
roleName: '', // 角色名称
remark: '' // 备注
},
// 表单验证规则
dataRule: {
roleName: [
{ required: true, message: '角色名称不能为空', trigger: 'blur' } // 角色名称不能为空
]
}
}
},
methods: {
// 初始化方法传入角色ID来初始化表单
init(id) {
this.dataForm.id = id || 0 // 如果没有传入ID则初始化为0新增
// 获取菜单列表并转换为树形结构
this.$http({
url: this.$http.adornUrl('/sys/menu/list'),
method: 'get',
params: this.$http.adornParams()
}).then(({ data }) => {
this.menuList = treeDataTranslate(data, 'menuId') // 转换为树形数据
}).then(() => {
// 显示弹窗
this.visible = true
// 在下一次DOM更新后执行
this.$nextTick(() => {
// 重置表单字段
this.$refs['dataForm'].resetFields()
// 清除选中的菜单
this.$refs.menuListTree.setCheckedKeys([])
})
}).then(() => {
// 如果是修改操作有ID获取角色详细信息
if (this.dataForm.id) {
this.$http({
url: this.$http.adornUrl(`/sys/role/info/${this.dataForm.id}`),
method: 'get',
params: this.$http.adornParams()
}).then(({ data }) => {
if (data && data.code === 200) {
// 填充表单数据
this.dataForm.roleName = data.role.roleName
this.dataForm.remark = data.role.remark
// 设置菜单树选中的节点
data.role.menuIdList.forEach(item => {
this.$refs.menuListTree.setChecked(item, true)
})
}
})
}
})
},
// 表单提交方法
dataFormSubmit() {
// 校验表单数据
this.$refs['dataForm'].validate((valid) => {
if (valid) {
// 提交数据
this.$http({
url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`), // 根据ID决定是保存还是更新
method: 'post',
data: this.$http.adornData({
'roleId': this.dataForm.id || undefined, // 角色ID
'roleName': this.dataForm.roleName, // 角色名称
'remark': this.dataForm.remark, // 备注
// 获取已选中的菜单ID列表
'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), this.$refs.menuListTree.getHalfCheckedKeys())
})
}).then(({ data }) => {
if (data && data.code === 200) {
// 操作成功,显示提示消息
this.$message({
message: '操作成功',
type: 'success',
duration: 1500,
onClose: () => {
// 关闭弹窗并触发刷新列表事件
this.visible = false
this.$emit('refreshDataList')
}
})
} else {
// 显示错误提示
this.$message.error(data.msg)
}
})
}
})
}
}
}
</script>