<!-- 菜单树 -->
|
<template>
|
<div class="apartment-management">
|
<p class="title"> 部门管理 </p>
|
|
<el-card>
|
<el-row>
|
<el-button class="create-button"
|
type="primary" @click='createModule'>新增</el-button>
|
<el-button class="delete-button"
|
type="primary" @click='deleteModule' v-if='isDelete'>删除</el-button>
|
</el-row>
|
<el-table
|
ref="menusTable"
|
border
|
max-height="600px"
|
:row-style="showRow"
|
:data="menusTable"
|
@selection-change="handleSelectionChange"
|
v-bind="$attrs">
|
<el-table-column
|
show-overflow-tooltip
|
type="selection"
|
align="center"
|
width="55"/>
|
<el-table-column
|
prop="Name"
|
label="部门名称" width="400">
|
<template slot-scope="scope">
|
<span class='level'>
|
<div v-show="scope.row.Children&&scope.row.Children.length>0" @click="openToggle(scope.row)"
|
:class="[scope.row.open?'el-table__expand-icon el-table__expand-icon--expanded':'el-table__expand-icon']">
|
<i class='el-icon-arrow-right'></i>
|
</div>
|
</span>
|
{{scope.row.Name}}
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop="Comment"
|
label="备注" width="200">
|
<template slot-scope="scope">
|
<div>{{scope.row.Comment}}</div>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop="IsEnabled"
|
label="状态">
|
<template slot-scope="scope">
|
<div>{{scope.row.IsEnabled ?'启用':'禁用'}}</div>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop="Sort"
|
label="排序">
|
<template slot-scope="scope">
|
<div>{{scope.row.Sort}}</div>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop="IsSectorArea"
|
label="是否是区域部门">
|
<template slot-scope="scope">
|
<div>{{scope.row.IsSectorArea ? '是' : '否'}}</div>
|
</template>
|
</el-table-column>
|
<el-table-column
|
prop="operation"
|
label="操作">
|
<template slot-scope="scope">
|
<el-button type="text" size="small" @click='addApart(scope.row)'>添加子部门</el-button>
|
<!-- 判断下面是否有子菜单,有子菜单不能是有删除按钮 -->
|
<el-button v-if="!scope.row.children" type="text" size="small" @click='deleteApart(scope.row.Id)'>删除</el-button>
|
<el-button type="text" size="small" @click='editApart(scope.row)'>编辑</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
</el-card>
|
<!-- 导航新增编辑弹出框 -->
|
<el-dialog
|
:visible.sync="depCreateDialog"
|
:title="navBtnTitle"
|
:before-close="cancelDialog"
|
width="40%">
|
<el-form
|
ref="apartForm"
|
:model="apartForm"
|
:rules="apartRules"
|
label-width="120px"
|
class="login-form"
|
auto-complete="on"
|
label-position="left">
|
<el-form-item label="上级部门" prop='fatherIntro'>
|
<el-cascader
|
ref="refApart"
|
placeholder="请选择上级部门"
|
:options="isCreate? selectOpts: rightsList"
|
:show-all-levels="false"
|
:props="{
|
children: 'Children',
|
label: 'Name',
|
value: 'Id'
|
}"
|
v-model="apartForm.fatherIntro"
|
style="width: 100%;"
|
@change='getFatherOpt'
|
change-on-select/>
|
</el-form-item>
|
<el-form-item label="部门名称" prop='introName'>
|
<el-input
|
v-model="apartForm.introName"
|
placeholder="请输入部门名称"/>
|
</el-form-item>
|
<el-form-item label="部门状态" prop='apartStatus'>
|
<el-switch v-model="apartForm.apartStatus"></el-switch>
|
</el-form-item>
|
<el-form-item label="是否是区域部门">
|
<el-switch v-model="apartForm.apartChosen"></el-switch>
|
</el-form-item>
|
<el-form-item label="部门类型" prop='apartInt'>
|
<el-select v-model="apartForm.apartInt" placeholder="请选择" style="width: 100%;">
|
<el-option
|
v-for="item in apartLists"
|
:key="item.id"
|
:label="item.value"
|
:value="item.id">
|
</el-option>
|
</el-select>
|
</el-form-item>
|
<el-form-item label="排序数字">
|
<el-input
|
class='numInp'
|
type='number'
|
v-model="apartForm.sortNum"
|
placeholder="请输入排序数字"/>
|
</el-form-item>
|
<el-form-item label="备注说明">
|
<el-input
|
type='textarea'
|
v-model="apartForm.info"
|
placeholder="请输入备注说明"/>
|
</el-form-item>
|
<el-form-item>
|
<el-button @click="cancelDialog">取 消</el-button>
|
<el-button
|
type="primary"
|
@click="submitDialog('apartForm')">确 定</el-button>
|
</el-form-item>
|
</el-form>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script>
|
import Vue from 'vue'
|
import { GetDepartmentList, AddDepartment, DeleteDepartment, UpdateDepartment } from '@/api/systemManagement/departmentManage'
|
export default {
|
name: 'staffManage',
|
data () {
|
return {
|
defaultProps: {
|
children: 'children',
|
label: 'title'
|
},
|
// 菜单表格结构数据
|
menusTable: [],
|
depCreateDialog: false,
|
apartForm: {
|
fatherIntro: '', // 上级部门
|
introName: '', // 部门名称
|
apartStatus: true, // 部门状态
|
apartChosen: false, // 是否是区域部门
|
apartInt: '', // 是否是区域部门
|
sortNum: 1, // 排序数字
|
info: ''
|
},
|
apartRules: {
|
fatherIntro: [
|
{ required: true, trigger: 'blur', message: '请选择上级部门' }
|
],
|
introName: [{ required: true, trigger: 'blur', message: '请输入部门名称' }],
|
apartInt: [{ required: true, trigger: 'blur', message: '请选择部门类型' }]
|
},
|
navBtnTitle: '新建',
|
deleteIds: [],
|
isDelete: false,
|
parentId: '',
|
isCreate: false,
|
isSon: false,
|
apartLists: [{id: 1, value: '总经办'}, {id: 2, value: '人事部'}, {id: 3, value: '财务部'}, {id: 4, value: '商务部'}, {id: 5, value: '销售部'}, {id: 6, value: '技术部'}],
|
rightsList: [],
|
selectOpts: []
|
}
|
},
|
// 初始化函数,赋值,menusTree =>menusTable
|
created () {
|
this.getList()
|
},
|
methods: {
|
getList () {
|
GetDepartmentList().then(res => {
|
console.log(res)
|
if (res.data.ErrorCode === 200) {
|
this.menusTable = res.data.Result
|
this.rightsList = res.data.Result
|
this.selectOpts = JSON.parse(JSON.stringify(this.rightsList))
|
this.selectOpts.unshift({ Name: '无上级部门', Id: 0 })
|
}
|
})
|
},
|
createModule () {
|
this.navBtnTitle = '新增部门'
|
this.depCreateDialog = true
|
this.isCreate = true
|
this.isSon = false
|
},
|
addApart (row) {
|
this.isCreate = true
|
this.isSon = true
|
this.navBtnTitle = '添加子部门'
|
this.depCreateDialog = true
|
this.apartForm = {
|
fatherIntro: row.Id, // 上级部门
|
introName: '', // 部门名称
|
apartStatus: true, // 部门状态
|
apartChosen: false, // 是否是区域部门
|
apartInt: '', // 是否是区域部门
|
sortNum: Number(row.Sort) + 1, // 排序数字
|
info: ''
|
}
|
},
|
editApart (row) {
|
console.log(row)
|
this.isCreate = false
|
this.isSon = false
|
this.navBtnTitle = '编辑部门'
|
this.depCreateDialog = true
|
this.isCreate = false
|
this.isSon = false
|
this.apartForm = {
|
fatherIntro: row.Id, // 上级部门
|
introName: row.Name, // 部门名称
|
apartStatus: row.IsEnabled, // 部门状态
|
apartChosen: row.IsSectorArea, // 是否是区域部门
|
apartInt: row.DepartmentType, // 是否是区域部门
|
sortNum: row.Sort, // 排序数字
|
info: row.Comment
|
}
|
this.parentId = row.ParentModuleId
|
},
|
getFatherOpt () {
|
console.log(this.apartForm.fatherIntro)
|
const flag = Array.isArray(this.apartForm.fatherIntro)
|
if (flag) {
|
this.parentId = this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[0] : ''
|
}
|
},
|
handleSelectionChange (val) {
|
console.log(val)
|
if (val.length > 0) {
|
let deleteId = []
|
val.forEach(item => {
|
deleteId.push(item.Id)
|
})
|
this.isDelete = true
|
this.deleteIds = deleteId
|
} else {
|
this.isDelete = false
|
}
|
},
|
deleteApart (id) {
|
this.$confirm('确定删除?', '提示', {
|
confirmButtonText: '确定',
|
cancelButtonText: '取消',
|
type: 'warning'
|
})
|
.then(() => {
|
DeleteDepartment({'Id': id}).then(res => {
|
console.log(res)
|
if (res.data.ErrorCode === 200) {
|
this.$message.success(res.data.Message)
|
this.getList()
|
} else {
|
this.$message.error(res.data.Message)
|
}
|
})
|
})
|
.catch(() => {
|
this.$message({
|
type: 'info',
|
message: '已取消删除'
|
})
|
})
|
},
|
submitDialog (formName) {
|
this.$refs[formName].validate(valid => {
|
if (!valid) return
|
const isIntro = Array.isArray(this.apartForm.fatherIntro)
|
let params = {
|
'Name': this.apartForm.introName,
|
'ParentId': isIntro ? (this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[this.apartForm.fatherIntro.length - 1] : (this.apartForm.fatherIntro[0] === 0 ? '' : this.apartForm.fatherIntro[0])) : this.apartForm.fatherIntro,
|
'DepartmentType': this.apartForm.apartInt,
|
'IsEnabled': this.apartForm.apartStatus,
|
'Sort': this.apartForm.sortNum,
|
'Comment': this.apartForm.info,
|
// 'LeaderUserId': '',
|
'IsSectorArea': this.apartForm.apartChosen
|
}
|
if (this.isCreate) {
|
if (this.isSon) {
|
params.ParentModuleId = this.parentId
|
}
|
AddDepartment(params).then(res => {
|
console.log(res)
|
if (res.data.ErrorCode === 200) {
|
this.$message.success(res.data.Message)
|
this.getList()
|
this.depCreateDialog = false
|
this.$refs.apartForm.resetFields()
|
this.apartForm = {
|
fatherIntro: '', // 上级部门
|
introName: '', // 部门名称
|
apartStatus: true, // 部门状态
|
apartChosen: false, // 是否是区域部门
|
apartInt: '', // 是否是区域部门
|
sortNum: 1, // 排序数字
|
info: ''
|
}
|
} else {
|
this.$message.error(res.data.Message)
|
}
|
})
|
} else {
|
params.Id = isIntro ? (this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[this.apartForm.fatherIntro.length - 1] : this.apartForm.fatherIntro[0]) : this.apartForm.fatherIntro
|
|
params.ParentId = this.parentId
|
|
UpdateDepartment(params).then(res => {
|
console.log(res)
|
if (res.data.ErrorCode === 200) {
|
this.$message.success(res.data.Message)
|
this.getList()
|
this.depCreateDialog = false
|
this.$refs.apartForm.resetFields()
|
this.apartForm = {
|
fatherIntro: '', // 上级部门
|
introName: '', // 部门名称
|
apartStatus: true, // 部门状态
|
apartChosen: false, // 是否是区域部门
|
apartInt: '', // 是否是区域部门
|
sortNum: 1, // 排序数字
|
info: ''
|
}
|
} else {
|
this.$message.error(res.data.Message)
|
}
|
})
|
}
|
})
|
},
|
// 关闭新增或编辑
|
cancelDialog () {
|
this.$refs.apartForm.resetFields()
|
this.apartForm = {
|
fatherIntro: '', // 上级部门
|
introName: '', // 部门名称
|
apartStatus: true, // 部门状态
|
apartChosen: false, // 是否是区域部门
|
apartInt: '', // 是否是区域部门
|
sortNum: 1, // 排序数字
|
info: ''
|
}
|
this.depCreateDialog = false
|
},
|
showRow (row) {
|
const show = row.row.parent
|
? row.row.parent._expanded && row.row.parent._show
|
: true
|
row.row._show = show
|
return show
|
? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;'
|
: 'display:none;'
|
},
|
|
// 树节点开关操作
|
openToggle (item) {
|
console.log(item)
|
// 这里只是展开和关闭样式的变换方法
|
Vue.set(item, 'open', !item.open)
|
// 展开的时候,显示子节点,关闭的时候隐藏子节点
|
// 遍历所有的子节点,加入到menuTable中
|
for (let j = 0; j < this.rightsList.length; j++) {
|
// 找到父节点的id,然后依次把子节点放到数组里面父节点后面
|
if (this.rightsList[j].Id !== item.Id) {
|
continue
|
}
|
if (item.open) { // open => close
|
console.log(item.Children)
|
let rightsList = this.rightsList
|
item.Children.forEach(function (child, index) {
|
rightsList.splice(j + index + 1, 0, child) // 添加子节点
|
})
|
} else {
|
this.rightsList.splice(j + 1, item.Children.length) // 删除子节点
|
}
|
break
|
}
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss" scoped>
|
.apartment-management{
|
height: 100%;
|
box-sizing: border-box;
|
display: flex;
|
flex-direction: column;
|
}
|
.nav-dialog-div {
|
margin-bottom: 20px;
|
}
|
.nav-dialog-div /deep/ .el-input {
|
width: auto;
|
}
|
.title {
|
font-size: 18px;
|
height: 40px;
|
line-height: 40px;
|
margin: 10px 0;
|
color: #333;
|
padding: 0 20px;
|
}
|
.level {
|
display: inline-block;
|
width: 20px;
|
}
|
.level1, .level2, .level3 {
|
display: inline-block;
|
width: 20px;
|
}
|
|
.level1 {
|
margin-left: 5px;
|
}
|
|
.level2 {
|
margin-left: 20px;
|
}
|
|
.level3 {
|
margin-left: 35px;
|
}
|
.create-button{
|
margin-bottom: 20px;
|
float: right;
|
}
|
.delete-button{
|
margin-bottom: 20px;
|
float: right;
|
margin-right: 20px;
|
}
|
.numInp{
|
width:40%;
|
}
|
</style>
|