新增附属部门切换接口,方便获取当前默认部门

This commit is contained in:
thinkgem
2024-07-04 16:45:00 +08:00
parent dd3e52a8ca
commit e4fc9057b3
3 changed files with 160 additions and 2 deletions

View File

@@ -55,8 +55,8 @@ public class Employee extends DataEntity<Employee> {
private String empNo; // 员工工号
private String empName; // 员工姓名
private String empNameEn; // 员工英文名
private Office office; // 机构编码
private Company company; // 公司编码
private Office office; // 机构对象
private Company company; // 公司对象
private String postCode; // 根据职位查询

View File

@@ -6,12 +6,17 @@ package com.jeesite.modules.sys.utils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.SetUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.common.web.http.ServletUtils;
import com.jeesite.modules.sys.entity.*;
import com.jeesite.modules.sys.service.CompanyService;
import com.jeesite.modules.sys.service.EmployeeService;
import com.jeesite.modules.sys.service.OfficeService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.shiro.session.Session;
import org.springframework.core.NamedThreadLocal;
import java.util.List;
import java.util.Set;
@@ -27,6 +32,10 @@ public class EmpUtils {
public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";
public static final String CACHE_COMPANY_ALL_LIST = "companyAllList";
public static final String CACHE_COMPANY_OFFICE_LIST = "employeeOfficeList";
// 当前线程部门没有session环境下使用优先级低于session
private static final ThreadLocal<String> currentOfficeCode = new NamedThreadLocal<>("CurrentOfficeCode");
private static final ThreadLocal<String> currentOfficeName = new NamedThreadLocal<>("CurrentOfficeName");
/**
* 静态内部类,延迟加载,懒汉式,线程安全的单例模式
@@ -326,4 +335,114 @@ public class EmpUtils {
CorpUtils.removeCache(key);
}
}
/**
* 获取当前登录用户的部门代码
* @return
*/
public static String getCurrentOfficeCode() {
String officeCode = StringUtils.EMPTY;
HttpServletRequest request = ServletUtils.getRequest();
if (request != null){
officeCode = (String)request.getAttribute("officeCode__");
}
if (StringUtils.isBlank(officeCode)){
Session session = UserUtils.getSubject().getSession(false);
if (session != null){
officeCode = ObjectUtils.toString(session.getAttribute("officeCode"));
}else{
officeCode = currentOfficeCode.get();
}
}
if (StringUtils.isBlank(officeCode)){
officeCode = getOffice().getOfficeCode();
}
if (request != null){
request.setAttribute("officeCode__", officeCode);
}
return ObjectUtils.toStringIgnoreNull(officeCode);
}
/**
* 获取当前登录用户的部门名称
* @return
*/
public static String getCurrentOfficeName() {
String officeName = StringUtils.EMPTY;
HttpServletRequest request = ServletUtils.getRequest();
if (request != null){
officeName = (String)request.getAttribute("officeName__");
}
if (StringUtils.isBlank(officeName)){
Session session = UserUtils.getSubject().getSession(false);
if (session != null){
officeName = ObjectUtils.toString(session.getAttribute("officeName"));
}else{
officeName = currentOfficeName.get();
}
}
if (StringUtils.isBlank(officeName)){
officeName = getOffice().getOfficeName();
}
if (request != null){
request.setAttribute("officeName__", officeName);
}
return ObjectUtils.toStringIgnoreNull(officeName);
}
/**
* 设置当前线程部门没有session环境下使用优先级低于session
* @author ThinkGem
*/
public static void setCurrentOffice(String officeCode, String officeName) {
Session session = UserUtils.getSubject().getSession(false);
setCurrentOffice(session, officeCode, officeName);
}
/**
* 设置当前线程部门没有session环境下使用优先级低于session
* @author ThinkGem
*/
public static void setCurrentOffice(Session session, String officeCode, String officeName) {
if (session != null){
session.setAttribute("officeCode", officeCode);
session.setAttribute("officeName", officeName);
}else{
currentOfficeCode.set(officeCode);
currentOfficeName.set(officeName);
}
HttpServletRequest request = ServletUtils.getRequest();
if (request != null){
request.setAttribute("officeCode__", officeCode);
request.setAttribute("officeName__", officeName);
}
}
/**
* 移除当前线程部门没有session环境下使用优先级低于session
* @author ThinkGem
*/
public static void removeCurrentOffice() {
Session session = UserUtils.getSubject().getSession(false);
removeCurrentOffice(session);
}
/**
* 移除当前线程部门没有session环境下使用优先级低于session
* @author ThinkGem
*/
public static void removeCurrentOffice(Session session) {
if (session != null){
session.removeAttribute("officeCode");
session.removeAttribute("officeName");
}else {
currentOfficeCode.remove();
currentOfficeName.remove();
}
HttpServletRequest request = ServletUtils.getRequest();
if (request != null){
request.removeAttribute("officeCode__");
request.removeAttribute("officeName__");
}
}
}

View File

@@ -41,6 +41,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 员工用户Controller
@@ -449,5 +450,43 @@ public class EmpUserController extends BaseController {
model.addAttribute("empUser", empUser);
return "modules/sys/user/empUserSelect";
}
/**
* 获取当前用户附属部门
* @return
*/
@RequiresPermissions("user")
@RequestMapping(value = "officeListData")
@ResponseBody
public List<EmployeeOffice> officeListData () {
Employee employee = EmpUtils.getEmployee();
return employeeService.findEmployeeOfficeList(employee);
}
/**
* 切换当前用户到附属部门
* @param officeCode
* @return
*/
@RequiresPermissions("user")
@RequestMapping(value = "switchOffice/{officeCode}")
@ResponseBody
public String switchOffice(@PathVariable String officeCode) {
if (StringUtils.equals(officeCode, "default")) {
Office office = EmpUtils.getOffice();
EmpUtils.setCurrentOffice(office.getOfficeCode(), office.getOfficeName());
return renderResult(Global.TRUE, text("部门切换成功!"));
}
List<EmployeeOffice> employeeOfficeList = EmpUtils.getEmployeeOfficeList().stream()
.filter(e -> StringUtils.equals(e.getOfficeCode(), officeCode)).collect(Collectors.toList());
if (!employeeOfficeList.isEmpty()) {
EmployeeOffice office = employeeOfficeList.get(0);
EmpUtils.setCurrentOffice(office.getOfficeCode(), office.getOfficeName());
return renderResult(Global.TRUE, text("部门切换成功!"));
} else {
return renderResult(Global.FALSE, text("部门切换失败,所切换部门不是您的附属部门!"));
}
}
}