Jelajahi Sumber

员工复制功能

chefz 5 bulan lalu
induk
melakukan
6b2cee1e4d

+ 125 - 0
src/main/java/com/jeesite/modules/mes/web/EmpUserCustomController.java

@@ -0,0 +1,125 @@
+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<EmployeeOffice> officeList = fullUser.getEmployee().getEmployeeOfficeList();
+                    if (officeList != null && !officeList.isEmpty()) {
+                        for (EmployeeOffice eo : officeList) {
+                            eo.setIsNewRecord(true);
+                            eo.setId(null);
+                        }
+                    }
+                }
+                
+                // 处理角色回显
+                List<Role> roleList = fullUser.getRoleList();
+                if (roleList != null && !roleList.isEmpty()) {
+                    List<String> 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 = "<script>" +
+                    "$(function(){" +
+                    "  var overrideAction = function() {" +
+                    "      var form = $('#inputForm');" +
+                    "      if(form.length > 0 && form.attr('action').indexOf('/mes/empUserCustom/save') === -1) {" +
+                    "          form.attr('action', '" + Global.getAdminPath() + "/mes/empUserCustom/save');" +
+                    "      }" +
+                    "  };" +
+                    "  overrideAction();" +
+                    "  setInterval(overrideAction, 500);" +
+                    "});" +
+                    "</script>";
+            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<EmpUser> 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<EmpUser> list = empUserService.findList(where);
+            if (list != null && !list.isEmpty()) {
+                return renderResult(Global.FALSE, "保存用户失败,用户昵称已存在(不能与原昵称相同),请修改用户昵称!");
+            }
+        }
+        
+        return empUserController.save(empUser, oldLoginCode, request);
+    }
+}

+ 3 - 1
src/main/resources/views/modules/sys/user/empUserList.html

@@ -151,6 +151,7 @@ $('#dataGrid').dataGrid({
 			//<% } %>
 			//<% if(hasPermi('sys:empUser:edit')){ %>
 				actions.push('<a href="${ctx}/sys/empUser/delete?userCode='+row.userCode+'" class="btnList" title="${text("删除用户")}" data-confirm="${text("确认要删除该用户吗?")}"><i class="fa fa-trash-o"></i></a>&nbsp;');
+				actions.push('<a href="${ctx}/mes/empUserCustom/form?userCode='+row.userCode+'&op=copy" class="btnList" title="${text("复制用户")}"><i class="fa fa-copy"></i></a>&nbsp;');
 			//<% } %>
 			//<% if(hasPermi('sys:empUser:authRole,sys:empUser:authDataScope,sys:empUser:resetpwd', 'or')){ %>
 				actions.push('<a href="javascript:" class="btnMore" title="${text("更多操作")}"><i class="fa fa-chevron-circle-right"></i></a>&nbsp;');
@@ -171,7 +172,8 @@ $('#dataGrid').dataGrid({
 				actions.push('<a href="${ctx}/mes/mesLogin/qrcode?userCode='+row.userCode+'" class="btn btn-default btn-xs btnList" title="${text("二维码")}"><i class="fa fa-fw fa-qrcode"></i> ${text("二维码")}</a>&nbsp;');
 				actions.push('</div>');
 			//<% } %>
-			return actions.join('');
+			var html = '<div style="overflow-x:auto;white-space:nowrap;width:100%;text-align:left;padding:2px 0;">' + actions.join('') + '<\/div>';
+			return html;
 		}}
 	],
 	frozenCols: true,