package com.miniframe.bisiness.system; import com.miniframe.constant.XIConstant; import com.miniframe.generate.business.system.model.A00002BaseModel; import com.miniframe.service.LoginService; import com.miniframe.core.ExecProcessFlow; import com.miniframe.core.exception.BusinessException; import com.miniframe.core.ext.UtilTools; import com.miniframe.generate.appcode.*; import com.miniframe.generate.comm.system.D_A00002_FUNCLIST_RECODE; import com.miniframe.generate.comm.system.D_A00002_MENULIST_RECODE; import com.miniframe.generate.comm.system.D_A00002_ROLELIST_RECODE; import com.miniframe.model.XIFuncRecode; import com.miniframe.model.XIMenuRecode; import com.miniframe.model.XIRoleRecode; import com.miniframe.model.system.*; import com.miniframe.model.system.dao.SysDepartmentMapper; import com.miniframe.model.system.dao.SysUserMapper; import com.miniframe.model.system.dao.UserLoginMapper; import com.miniframe.spring.properties.MFMiniCoreProperties; import com.miniframe.tools.encode.XIDESedeCoderUtils; import com.miniframe.tools.XIDateTimeUtils; import com.miniframe.tools.XIUtils; import com.miniframe.tools.jwt.JwtManageUtil; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; /** * 基础系统,“登录”逻辑处理(重新生成不覆盖)。 */ public class A00002Service extends A00002BaseModel implements ExecProcessFlow { private static final long serialVersionUID = -7051358269847459502L; /** * 基础系统,“登录”业务核心处理 */ public void transExecute() throws Exception { String loginMedia = UtilTools.isNullOrBlank(this.getA_a00002().getLoginMedia()) ? LoginMedia.APP.getIndex() : this.getA_a00002().getLoginMedia(); if (LoginMedia.getLoginMedia(loginMedia) == null) { throw new BusinessException("EB8000015"); } String loginType = UtilTools.isNullOrBlank(this.getA_a00002().getType()) ? LoginType.PASSWD.getIndex() : this.getA_a00002().getType(); if (LoginType.getLoginType(loginType) == null) { throw new BusinessException("EB8000015"); } //0手动登录 1-自动登录 String loginWay = UtilTools.isNullOrBlank(this.getA_a00002().getLoginWay()) ? LoginWay.MANUAL.getIndex() : this.getA_a00002().getLoginWay(); if (LoginWay.getLoginWay(loginWay) == null) { throw new BusinessException("EB8000015"); } SysUser sysUser; SysUserMapper sysUserDAO = UtilTools.getBean(SysUserMapper.class); if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录 if (UtilTools.isNotNullAndBlank(this.getA_a00002().getAuthCode())) { //验证图片 LoginService.validateGraph(this.getA_a00002().getAuthCode(), this.getA_systemhead().getClientToken()); } //本地是否存在 SysUserSQLBuilder sysUserSQLBuilder = new SysUserSQLBuilder(); String loginName = this.getA_a00002().getLoginName().trim(); //先使用loginName查询用户,再用手机号查询 sysUserSQLBuilder.createCriteria().andUserstateNotEqualTo(UserState.delete.getIndex()).andUsernameEqualTo(loginName); List userInfoList = sysUserDAO.selectByExample(sysUserSQLBuilder); if (CollectionUtils.isEmpty(userInfoList)) { //再用手机号查询 sysUserSQLBuilder = new SysUserSQLBuilder(); sysUserSQLBuilder.createCriteria().andUsermobnubEqualTo(loginName); userInfoList = sysUserDAO.selectByExample(sysUserSQLBuilder); if (CollectionUtils.isEmpty(userInfoList)) { // 不存在就报错 throw new BusinessException("EB8000015"); } } sysUser = userInfoList.get(0); } else { sysUser = sysUserDAO.selectByPrimaryKey(this.getA_systemhead().getUserId()); if (sysUser == null) { throw new BusinessException("EB8000015"); } } UserLoginMapper userLoginDAO = UtilTools.getBean(UserLoginMapper.class); UserLogin userLogin = new UserLogin(); userLogin.setId(UtilTools.getUUid()); userLogin.setUid(sysUser.getId()); userLogin.setLoginName(this.getA_a00002().getLoginName()); userLogin.setLoginTime(XIDateTimeUtils.getNowDate()); userLogin.setLoginIp(XIUtils.getClientIp()); userLogin.setWrongTimes(0); userLogin.setLoginType(LoginType.getLoginType(loginType).getDesc()); userLogin.setLoginWay(LoginWay.getLoginWay(loginWay).getDesc()); //0手动登录 1-自动登录 userLogin.setLoginMedia(LoginMedia.getLoginMedia(loginMedia).getDesc()); userLogin.setCreateTime(XIDateTimeUtils.getNowDate()); userLogin.setUpdateTime(userLogin.getCreateTime()); userLogin.setStatus(Loginflag.sucess.getIndex()); //默认登录成功 userLogin.setLoginCode(XIUtils.getNormalReturnCode()); userLogin.setLoginMsg(Loginflag.sucess.getDesc()); if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录 if (LoginType.PASSWD.getIndex().equals(loginType)) {// 密码登录 String password_ = XIDESedeCoderUtils.dec(this.getA_a00002().getPassword(), this.getA_systemhead().getClientToken());//3DES解密 if (!LoginService.encodePassword(sysUser.getId(), password_).equals(sysUser.getUserpwd())) {//比较密码是否相同 userLogin.setStatus(Loginflag.fail.getIndex()); userLogin.setLoginCode("EB8000015"); userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode())); userLoginDAO.insertSelective(userLogin); throw new BusinessException(userLogin.getLoginCode()); } } else if (LoginType.VERIFYCODE.getIndex().equals(loginType)) {// 验证码登录 // if (!IdentifyingCodeUtil.verification(this.getA_a00002().getPhoneNo(), this.getA_a00002().getAuthCode())) { // // 验证码校验失败! // throw new BusinessException("EB8000021"); // } } } else { //自动登录,验证clientToken boolean isCheckJwtToken=false; try { isCheckJwtToken=LoginService.checkJwtTokenForUser(this.getA_systemhead().getUserId(), this.getA_systemhead().getClientToken()); }catch (Exception e){} if (!isCheckJwtToken) { userLogin.setStatus("1"); userLogin.setLoginCode("EB8000015"); userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode())); userLoginDAO.insertSelective(userLogin); throw new BusinessException(userLogin.getLoginCode()); } } if (sysUser.getUserstate().equals(UserState.delete.getIndex())) { userLogin.setStatus(Loginflag.fail.getIndex()); userLogin.setLoginCode("EB8000015"); userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode())); userLoginDAO.insertSelective(userLogin); throw new BusinessException(userLogin.getLoginCode()); } if (sysUser.getUserstate().equals(UserState.black.getIndex())) { userLogin.setStatus(Loginflag.fail.getIndex()); userLogin.setLoginCode("EB8000104"); userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode())); userLoginDAO.insertSelective(userLogin); throw new BusinessException(userLogin.getLoginCode()); } // //用户是否可用此登录渠道进行登录权限判断 // if (!XIUtils.getListFromStrDelEmpty(sysUser.getFuncids()).stream() // .anyMatch(func -> func.equals(LoginMedia.getLoginMedia(loginMedia).getDesc()))) { // userLogin.setStatus(Loginflag.fail.getIndex()); // userLogin.setLoginCode("EB8000012"); // userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode())); // userLoginDAO.insertSelective(userLogin); // // throw new BusinessException(userLogin.getLoginCode()); // } /////////////////////////////// userLoginDAO.insertSelective(userLogin); String userId = sysUser.getId(); String tocken = this.getA_systemhead().getClientToken(); if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录 if (UtilTools.isNotNullAndBlank(this.getA_a00002().getIsLoginAuto()) && this.getA_a00002().getIsLoginAuto().equals("1")) { tocken = JwtManageUtil.getClientLoginAutoJWT(MFMiniCoreProperties.getInstance().getUniqueid(), userId, userId); } else { tocken = UUID.randomUUID().toString().replace("-", "").trim(); } } //部门自定义的用户权限 Map deptFuncMap = new HashMap(); SysDepartment sysDepartment = UtilTools.getBean(SysDepartmentMapper.class).selectByPrimaryKey(sysUser.getOrgDeptCode()); if (sysDepartment != null) { XIUtils.getListFromStrDelEmpty(sysDepartment.getDeptfunc()).stream() .forEach(func->deptFuncMap.put(func,"")); } //用户cache UtilTools.putUserCache(XIConstant.UserKeyPre + sysUser.getId().trim(), tocken); UtilTools.putUserCache(XIConstant.TokenKeyPre + tocken, sysUser.getId()); LoginService.initUserAccess(sysUser.getId()); this.getD_systemhead().setClientToken(tocken); this.getD_systemhead().setUserId(userId); this.getD_a00002().setNickName(sysUser.getNickname() == null ? sysUser.getUsername() : sysUser.getNickname()); this.getD_a00002().setUserName(sysUser.getUsername()); this.getD_a00002().setOrgCode(sysUser.getOrgcode()); this.getD_a00002().setOrgDeptCode(sysUser.getOrgDeptCode()); this.getD_a00002().setRegTime(XIDateTimeUtils.getStrFromDate(sysUser.getCreateTime())); this.getD_a00002().setMobileNo(sysUser.getUsermobnub()); this.getD_a00002().setHeadProfile(sysUser.getHeadPortait()); this.getD_a00002().setUserType(sysUser.getUserType()); if(sysUser.getUserType().equals(UserType.son.getIndex())){ }else { SysUserSQLBuilder sonUserSQLBuilder=new SysUserSQLBuilder(); sonUserSQLBuilder.createCriteria().andSuperiorUserIdEqualTo(sysUser.getId()); List sonUserList = UtilTools.getBean(SysUserMapper.class).selectByExample(sonUserSQLBuilder); List userIdList = null; if(sonUserList!=null && sonUserList.size()>0) { userIdList=sonUserList.stream().map(data->data.getId()).distinct() .collect(Collectors.toList()); } if(userIdList == null){ userIdList=new ArrayList<>(); } userIdList.add(sysUser.getId()); } List roleList = LoginService.getAccessRole(sysUser.getId()); if (roleList != null && roleList.size() > 0) { this.getD_a00002().setRoleList(roleList.stream().map(role -> { D_A00002_ROLELIST_RECODE recode = new D_A00002_ROLELIST_RECODE(); recode.setRoleId(role.getRoleId()); recode.setRoleName(role.getRoleName()); recode.setRoleDesc(role.getRoleDesc()); return recode; }).collect(Collectors.toList())); } List funcList = LoginService.getAccessFunction(sysUser.getId(),deptFuncMap); if (funcList != null && funcList.size() > 0) { this.getD_a00002().setFuncList(funcList.stream().map(func -> { D_A00002_FUNCLIST_RECODE recode = new D_A00002_FUNCLIST_RECODE(); recode.setFuncId(func.getFuncId()); recode.setFuncClass(func.getFuncClass()); recode.setFuncDesc(func.getFuncDesc()); recode.setFuncName(func.getFuncName()); return recode; }).collect(Collectors.toList())); } List menuList = LoginService.getAccessMenu(sysUser.getId()); if (menuList != null && menuList.size() > 0) { List menulistRecodes = menuList.stream().map(menu -> { D_A00002_MENULIST_RECODE recode = new D_A00002_MENULIST_RECODE(); recode.setMenuId(menu.getMenuId()); recode.setMenuDesc(menu.getMenuDesc()); recode.setMenuFlag(menu.getMenuFlag()); recode.setMenuImage(menu.getMenuImage()); recode.setMenuName(menu.getMenuName()); recode.setMenuParentId(menu.getMenuParentId()); recode.setMenuUrl(menu.getMenuUrl()); return recode; }).collect(Collectors.toList()); this.getD_a00002().setMenuList(menulistRecodes); } // VExtUserSQLBuilder VExtUserSQLBuilder = new VExtUserSQLBuilder(); // VExtUserSQLBuilder.createCriteria().andIdEqualTo(sysUser.getId()); // List VExtUserList = UtilTools.getBean(VExtUserMapper.class).selectByExample(VExtUserSQLBuilder); // if (VExtUserList != null && VExtUserList.size() > 0) { // VExtUser VExtUser = VExtUserList.get(0); // // String certState = VExtUser.getCertState(); // if (certState == null) { // certState = CertificationState.noCertificate.getIndex(); // } // this.getD_a00002().setAuthenticationState(certState); // this.getD_a00002().setCertType(VExtUser.getCertType()); // this.getD_a00002().setCertName(VExtUser.getCertName()); // this.getD_a00002().setCertNo(VExtUser.getCertNumber()); // this.getD_a00002().setCertFile(VExtUser.getCertFile()); // this.getD_a00002().setCertTime(XIDateTimeUtils.getStrFromDate(VExtUser.getCertAuthTime())); // } } /** * 基础系统,“登录”业务前处理 */ public void preTransFlow() throws Exception { this.validater(); } /** * 基础系统,“登录”业务后处理 */ public void afterTransFlow() throws Exception { } /** * 基础系统,“登录”逻辑入口处理方法 */ @SuppressWarnings("rawtypes") @Override public Map execute(Map vars) throws Exception { this.setTransMap(vars); preTransFlow();// 执行业务开始的规则检查和校验 transExecute();// 执行核心业务段 afterTransFlow();// 执行核心逻辑完成后的收尾逻辑 return this.getTransMap(); } }