ruoyi后端获取当前登录人的部门方法
时间: 2023-06-19 14:08:16 浏览: 830
在Ruoyi后端,可以通过以下步骤获取当前登录人的部门:
1. 首先,在登录成功后,会把用户信息存储到Session中,可以通过HttpServletRequest获取Session。
2. 从Session中获取当前登录用户的用户信息,包括用户ID等信息。
3. 调用UserService中的方法,通过用户ID获取用户所在的部门信息。
4. 返回部门信息。
具体的代码如下:
```
@Autowired
private UserService userService;
@RequestMapping("/getDept")
@ResponseBody
public Dept getDept(HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User) session.getAttribute(Constants.SESSION_USER_INFO);
Long userId = user.getUserId();
Dept dept = userService.selectDeptByUserId(userId);
return dept;
}
```
其中,Constants.SESSION_USER_INFO是一个常量,用于存储Session中用户信息的key值。selectDeptByUserId是UserService中定义的方法,用于通过用户ID获取用户所在的部门信息。
相关问题
ruoyi后端如何获取当前登录人的部门
在Ruoyi后端中,可以通过以下代码获取当前登录人的部门信息:
1. 首先,需要在后端的Controller中获取当前登录人的信息。可以使用Spring Security框架提供的SecurityContextHolder来获取当前登录的用户信息,例如:
```
// 获取当前登录用户
SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
```
2. 接着,可以通过获取到的用户信息中的部门ID来获取部门信息。假设部门信息存储在数据库中,可以使用MyBatis等ORM框架来查询:
```
// 获取当前登录用户的部门ID
Long deptId = user.getDeptId();
// 查询部门信息
SysDept dept = deptMapper.selectDeptById(deptId);
```
这里假设SysDept为部门实体类,deptMapper为部门Mapper接口。查询的结果将会是当前登录用户所属的部门信息。
ruoyi根据部门获取当前部门及以下部门的信息
在Ruoyi中,可以通过使用部门ID来获取当前部门及以下部门的信息。具体实现步骤如下:
1. 通过部门ID获取当前部门信息
```java
SysDept dept = deptService.selectDeptById(deptId);
```
2. 通过部门ID获取下级部门信息列表
```java
List<SysDept> deptList = deptService.selectDeptList(new SysDept(deptId));
```
3. 将当前部门信息和下级部门信息合并到一个列表中
```java
deptList.add(0, dept);
```
4. 使用递归函数获取所有下级部门信息,并将其添加到部门信息列表中
```java
List<SysDept> childDepts = getChildDepts(deptId, deptList);
deptList.addAll(childDepts);
```
完整代码示例:
```java
public List<SysDept> getChildDepts(Long deptId, List<SysDept> deptList) {
List<SysDept> childDepts = new ArrayList<>();
for (SysDept dept : deptList) {
if (dept.getParentId().equals(deptId)) {
childDepts.add(dept);
List<SysDept> grandchildDepts = getChildDepts(dept.getDeptId(), deptList);
childDepts.addAll(grandchildDepts);
}
}
return childDepts;
}
public List<SysDept> selectDeptListByDeptId(Long deptId) {
SysDept dept = deptService.selectDeptById(deptId);
List<SysDept> deptList = deptService.selectDeptList(new SysDept(deptId));
deptList.add(0, dept);
List<SysDept> childDepts = getChildDepts(deptId, deptList);
deptList.addAll(childDepts);
return deptList;
}
```
调用示例:
```java
List<SysDept> deptList = deptService.selectDeptListByDeptId(deptId);
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)