在车辆管理功能中,车辆归属部门应该只能选择分公司,因此需要过滤部门列表,只显示分公司(parent_id = 100的部门)。
100 (根部门)
├── 101 (分公司1) ← 只显示这一层
├── 102 (分公司2) ← 只显示这一层
│ ├── 103 (子部门) ← 不显示
│ └── 104 (子部门) ← 不显示
└── 105 (分公司3) ← 只显示这一层
parent_id = 100 的部门(分公司)文件路径:ruoyi-ui/src/views/system/vehicle/index.vue
/** 获取部门列表 */
getDeptList() {
listDept().then(response => {
this.deptList = response.data;
});
}
/** 获取部门列表(只显示分公司:parent_id=100) */
getDeptList() {
listDept({ parentId: 100 }).then(response => {
// 过滤出分公司(parent_id=100的部门)
if (response.data) {
this.deptList = response.data.filter(dept => dept.parentId === 100);
} else {
this.deptList = [];
}
});
}
采用了**双重过滤**的安全策略:
javascript listDept({ parentId: 100 }) parentId: 100 参数javascript this.deptList = response.data.filter(dept => dept.parentId === 100); javascript if (response.data) { // 处理数据 } else { this.deptList = []; } <el-form-item label="归属部门" prop="deptId">
<el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable size="small">
<el-option
v-for="dept in deptList"
:key="dept.deptId"
:label="dept.deptName"
:value="dept.deptId"
/>
</el-select>
</el-form-item>
影响:下拉选项只显示分公司
<el-form-item label="归属部门" prop="deptId">
<el-select v-model="form.deptId" placeholder="请选择归属部门" clearable style="width: 100%">
<el-option
v-for="dept in deptList"
:key="dept.deptId"
:label="dept.deptName"
:value="dept.deptId"
/>
</el-select>
</el-form-item>
影响:新增或修改车辆时,只能选择分公司作为归属部门
CREATE TABLE sys_dept (
dept_id BIGINT(20) NOT NULL COMMENT '部门id',
parent_id BIGINT(20) DEFAULT '0' COMMENT '父部门id',
dept_name VARCHAR(30) DEFAULT '' COMMENT '部门名称',
order_num INT(4) DEFAULT '0' COMMENT '显示顺序',
leader VARCHAR(20) DEFAULT NULL COMMENT '负责人',
phone VARCHAR(11) DEFAULT NULL COMMENT '联系电话',
email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
status CHAR(1) DEFAULT '0' COMMENT '部门状态(0正常 1停用)',
del_flag CHAR(1) DEFAULT '0' COMMENT '删除标志(0代表存在 2代表删除)',
create_by VARCHAR(64) DEFAULT '' COMMENT '创建者',
create_time DATETIME COMMENT '创建时间',
update_by VARCHAR(64) DEFAULT '' COMMENT '更新者',
update_time DATETIME COMMENT '更新时间',
PRIMARY KEY (dept_id)
) COMMENT='部门表';
-- 根部门
INSERT INTO sys_dept VALUES (100, 0, '总公司', 0, '管理员', '15888888888', 'admin@qq.com', '0', '0', 'admin', NOW(), '', NULL);
-- 分公司(parent_id = 100)
INSERT INTO sys_dept VALUES (101, 100, '深圳分公司', 1, '张三', '15888888881', 'sz@qq.com', '0', '0', 'admin', NOW(), '', NULL);
INSERT INTO sys_dept VALUES (102, 100, '广州分公司', 2, '李四', '15888888882', 'gz@qq.com', '0', '0', 'admin', NOW(), '', NULL);
INSERT INTO sys_dept VALUES (103, 100, '北京分公司', 3, '王五', '15888888883', 'bj@qq.com', '0', '0', 'admin', NOW(), '', NULL);
-- 子部门(parent_id != 100,不应该显示)
INSERT INTO sys_dept VALUES (201, 101, '深圳技术部', 1, '赵六', '15888888884', 'szjs@qq.com', '0', '0', 'admin', NOW(), '', NULL);
INSERT INTO sys_dept VALUES (202, 101, '深圳业务部', 2, '钱七', '15888888885', 'szyw@qq.com', '0', '0', 'admin', NOW(), '', NULL);
deptList 中所有部门的 parentId 都等于 100parent_id = 100 的分公司数据parent_id = 100parentId 参数,前端也能正确过滤建议后端提供专门的分公司查询接口:javascript // 新增API export function listBranchCompany() { return request({ url: '/system/dept/branch', method: 'get' }) }
后端实现:java /** * 查询分公司列表 */ @GetMapping("/branch") public AjaxResult listBranchCompany() { SysDept query = new SysDept(); query.setParentId(100L); List<SysDept> list = deptService.selectDeptList(query); return success(list); }
将 parent_id = 100 这个值配置化:
```javascript
// 在配置文件中定义
const BRANCH_COMPANY_PARENT_ID = 100;
// 使用时引用
getDeptList() {
listDept({ parentId: BRANCH_COMPANY_PARENT_ID }).then(response => {
if (response.data) {
this.deptList = response.data.filter(
dept => dept.parentId === BRANCH_COMPANY_PARENT_ID
);
}
});
}
```
如果分公司数据变化不频繁,可以考虑缓存:
```javascript
// 使用Vuex缓存分公司列表
computed: {
...mapState({
cachedDeptList: state => state.dept.branchCompanyList
})
},
mounted() {
if (!this.cachedDeptList || this.cachedDeptList.length === 0) {
this.getDeptList();
} else {
this.deptList = this.cachedDeptList;
}
}
```
| 版本 | 日期 | 修改内容 | 修改人 |
|---|
| 1.0 | 2025-10-15 | 初始版本,实现部门过滤功能 | - |
通过此次修改,车辆管理功能中的部门选择更加规范,确保车辆只能归属于分公司,避免了数据层级混乱的问题。同时采用了双重过滤的安全策略,保证了数据的准确性和系统的稳定性。