package com.jeesite.modules.mes.web; import com.jeesite.common.config.Global; import com.jeesite.common.lang.StringUtils; import com.jeesite.common.web.BaseController; import com.jeesite.modules.sys.entity.EmpUser; import com.jeesite.modules.sys.entity.EmployeeOffice; import com.jeesite.modules.sys.entity.Role; import com.jeesite.modules.sys.service.EmpUserService; import com.jeesite.modules.sys.web.user.EmpUserController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; @Controller @RequestMapping(value = "${adminPath}/mes/empUserCustom") public class EmpUserCustomController extends BaseController { @Autowired private EmpUserController empUserController; @Autowired private EmpUserService empUserService; @ModelAttribute public EmpUser get(String userCode, boolean isNewRecord) { return empUserService.get(userCode, isNewRecord); } @RequestMapping(value = "form") public String form(EmpUser empUser, String op, Model model) { if ("copy".equals(op)) { // 借用原生的 form 方法,获取全量数据(包括岗位、机构、角色) String view = empUserController.form(empUser, "edit", model); EmpUser fullUser = (EmpUser) model.asMap().get("empUser"); if (fullUser != null) { // 将原记录转化为新记录 fullUser.setIsNewRecord(true); fullUser.setUserCode(null); // 转换员工及附属机构的主键 if (fullUser.getEmployee() != null) { fullUser.getEmployee().setIsNewRecord(true); fullUser.getEmployee().setEmpCode(null); List officeList = fullUser.getEmployee().getEmployeeOfficeList(); if (officeList != null && !officeList.isEmpty()) { for (EmployeeOffice eo : officeList) { eo.setIsNewRecord(true); eo.setId(null); } } } // 处理角色回显 List roleList = fullUser.getRoleList(); if (roleList != null && !roleList.isEmpty()) { List roleCodes = new ArrayList<>(); for (Role role : roleList) { roleCodes.add(role.getRoleCode()); } fullUser.setUserRoleString(StringUtils.join(roleCodes, ",")); } // 覆盖 Model 中的对象并注入角色列表和操作标识 model.addAttribute("empUser", fullUser); model.addAttribute("roleList", roleList); model.addAttribute("op", "add"); } // 注入自定义保存脚本,修改表单 action 为我们自己的 save String customSaveScript = ""; model.addAttribute("customSaveScript", customSaveScript); // 为了防止 customSaveScript 没有被模板渲染,我们利用 extend 字段进行 XSS 注入(JeeSite 中扩展字段常用于透传) // 或者更安全的做法,只需 model 传参。这里保留之前的做法。 return view; } return empUserController.form(empUser, op, model); } @RequestMapping(value = "save") @ResponseBody public String save(@Validated EmpUser empUser, String oldLoginCode, HttpServletRequest request) { EmpUser existUser = new EmpUser(); existUser.setLoginCode(empUser.getLoginCode()); List uList = empUserService.findList(existUser); if (uList != null && !uList.isEmpty()) { return renderResult(Global.FALSE, "保存用户失败,登录账号已存在(不能与原账号相同),请修改登录账号!"); } if (StringUtils.isNotBlank(empUser.getUserName())) { EmpUser where = new EmpUser(); where.setUserName(empUser.getUserName()); List list = empUserService.findList(where); if (list != null && !list.isEmpty()) { return renderResult(Global.FALSE, "保存用户失败,用户昵称已存在(不能与原昵称相同),请修改用户昵称!"); } } return empUserController.save(empUser, oldLoginCode, request); } }