A00002Service.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package com.miniframe.bisiness.system;
  2. import com.miniframe.constant.XIConstant;
  3. import com.miniframe.generate.business.system.model.A00002BaseModel;
  4. import com.miniframe.service.LoginService;
  5. import com.miniframe.core.ExecProcessFlow;
  6. import com.miniframe.core.exception.BusinessException;
  7. import com.miniframe.core.ext.UtilTools;
  8. import com.miniframe.generate.appcode.*;
  9. import com.miniframe.generate.comm.system.D_A00002_FUNCLIST_RECODE;
  10. import com.miniframe.generate.comm.system.D_A00002_MENULIST_RECODE;
  11. import com.miniframe.generate.comm.system.D_A00002_ROLELIST_RECODE;
  12. import com.miniframe.model.XIFuncRecode;
  13. import com.miniframe.model.XIMenuRecode;
  14. import com.miniframe.model.XIRoleRecode;
  15. import com.miniframe.model.system.*;
  16. import com.miniframe.model.system.dao.SysDepartmentMapper;
  17. import com.miniframe.model.system.dao.SysUserMapper;
  18. import com.miniframe.model.system.dao.UserLoginMapper;
  19. import com.miniframe.spring.properties.MFMiniCoreProperties;
  20. import com.miniframe.tools.encode.XIDESedeCoderUtils;
  21. import com.miniframe.tools.XIDateTimeUtils;
  22. import com.miniframe.tools.XIUtils;
  23. import com.miniframe.tools.jwt.JwtManageUtil;
  24. import org.springframework.util.CollectionUtils;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.UUID;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 基础系统,“登录”逻辑处理(重新生成不覆盖)。
  33. */
  34. public class A00002Service extends A00002BaseModel implements ExecProcessFlow {
  35. private static final long serialVersionUID = -7051358269847459502L;
  36. /**
  37. * 基础系统,“登录”业务核心处理
  38. */
  39. public void transExecute() throws Exception {
  40. String loginMedia = UtilTools.isNullOrBlank(this.getA_a00002().getLoginMedia()) ? LoginMedia.APP.getIndex() : this.getA_a00002().getLoginMedia();
  41. if (LoginMedia.getLoginMedia(loginMedia) == null) {
  42. throw new BusinessException("EB8000015");
  43. }
  44. String loginType = UtilTools.isNullOrBlank(this.getA_a00002().getType()) ? LoginType.PASSWD.getIndex() : this.getA_a00002().getType();
  45. if (LoginType.getLoginType(loginType) == null) {
  46. throw new BusinessException("EB8000015");
  47. }
  48. //0手动登录 1-自动登录
  49. String loginWay = UtilTools.isNullOrBlank(this.getA_a00002().getLoginWay()) ? LoginWay.MANUAL.getIndex() : this.getA_a00002().getLoginWay();
  50. if (LoginWay.getLoginWay(loginWay) == null) {
  51. throw new BusinessException("EB8000015");
  52. }
  53. SysUser sysUser;
  54. SysUserMapper sysUserDAO = UtilTools.getBean(SysUserMapper.class);
  55. if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录
  56. if (UtilTools.isNotNullAndBlank(this.getA_a00002().getAuthCode())) {
  57. //验证图片
  58. LoginService.validateGraph(this.getA_a00002().getAuthCode(), this.getA_systemhead().getClientToken());
  59. }
  60. //本地是否存在
  61. SysUserSQLBuilder sysUserSQLBuilder = new SysUserSQLBuilder();
  62. String loginName = this.getA_a00002().getLoginName().trim();
  63. //先使用loginName查询用户,再用手机号查询
  64. sysUserSQLBuilder.createCriteria().andUserstateNotEqualTo(UserState.delete.getIndex()).andUsernameEqualTo(loginName);
  65. List<SysUser> userInfoList = sysUserDAO.selectByExample(sysUserSQLBuilder);
  66. if (CollectionUtils.isEmpty(userInfoList)) {
  67. //再用手机号查询
  68. sysUserSQLBuilder = new SysUserSQLBuilder();
  69. sysUserSQLBuilder.createCriteria().andUsermobnubEqualTo(loginName);
  70. userInfoList = sysUserDAO.selectByExample(sysUserSQLBuilder);
  71. if (CollectionUtils.isEmpty(userInfoList)) {
  72. // 不存在就报错
  73. throw new BusinessException("EB8000015");
  74. }
  75. }
  76. sysUser = userInfoList.get(0);
  77. } else {
  78. sysUser = sysUserDAO.selectByPrimaryKey(this.getA_systemhead().getUserId());
  79. if (sysUser == null) {
  80. throw new BusinessException("EB8000015");
  81. }
  82. }
  83. UserLoginMapper userLoginDAO = UtilTools.getBean(UserLoginMapper.class);
  84. UserLogin userLogin = new UserLogin();
  85. userLogin.setId(UtilTools.getUUid());
  86. userLogin.setUid(sysUser.getId());
  87. userLogin.setLoginName(this.getA_a00002().getLoginName());
  88. userLogin.setLoginTime(XIDateTimeUtils.getNowDate());
  89. userLogin.setLoginIp(XIUtils.getClientIp());
  90. userLogin.setWrongTimes(0);
  91. userLogin.setLoginType(LoginType.getLoginType(loginType).getDesc());
  92. userLogin.setLoginWay(LoginWay.getLoginWay(loginWay).getDesc()); //0手动登录 1-自动登录
  93. userLogin.setLoginMedia(LoginMedia.getLoginMedia(loginMedia).getDesc());
  94. userLogin.setCreateTime(XIDateTimeUtils.getNowDate());
  95. userLogin.setUpdateTime(userLogin.getCreateTime());
  96. userLogin.setStatus(Loginflag.sucess.getIndex()); //默认登录成功
  97. userLogin.setLoginCode(XIUtils.getNormalReturnCode());
  98. userLogin.setLoginMsg(Loginflag.sucess.getDesc());
  99. if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录
  100. if (LoginType.PASSWD.getIndex().equals(loginType)) {// 密码登录
  101. String password_ = XIDESedeCoderUtils.dec(this.getA_a00002().getPassword(), this.getA_systemhead().getClientToken());//3DES解密
  102. if (!LoginService.encodePassword(sysUser.getId(), password_).equals(sysUser.getUserpwd())) {//比较密码是否相同
  103. userLogin.setStatus(Loginflag.fail.getIndex());
  104. userLogin.setLoginCode("EB8000015");
  105. userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode()));
  106. userLoginDAO.insertSelective(userLogin);
  107. throw new BusinessException(userLogin.getLoginCode());
  108. }
  109. } else if (LoginType.VERIFYCODE.getIndex().equals(loginType)) {// 验证码登录
  110. // if (!IdentifyingCodeUtil.verification(this.getA_a00002().getPhoneNo(), this.getA_a00002().getAuthCode())) {
  111. // // 验证码校验失败!
  112. // throw new BusinessException("EB8000021");
  113. // }
  114. }
  115. } else { //自动登录,验证clientToken
  116. boolean isCheckJwtToken=false;
  117. try {
  118. isCheckJwtToken=LoginService.checkJwtTokenForUser(this.getA_systemhead().getUserId(), this.getA_systemhead().getClientToken());
  119. }catch (Exception e){}
  120. if (!isCheckJwtToken) {
  121. userLogin.setStatus("1");
  122. userLogin.setLoginCode("EB8000015");
  123. userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode()));
  124. userLoginDAO.insertSelective(userLogin);
  125. throw new BusinessException(userLogin.getLoginCode());
  126. }
  127. }
  128. if (sysUser.getUserstate().equals(UserState.delete.getIndex())) {
  129. userLogin.setStatus(Loginflag.fail.getIndex());
  130. userLogin.setLoginCode("EB8000015");
  131. userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode()));
  132. userLoginDAO.insertSelective(userLogin);
  133. throw new BusinessException(userLogin.getLoginCode());
  134. }
  135. if (sysUser.getUserstate().equals(UserState.black.getIndex())) {
  136. userLogin.setStatus(Loginflag.fail.getIndex());
  137. userLogin.setLoginCode("EB8000104");
  138. userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode()));
  139. userLoginDAO.insertSelective(userLogin);
  140. throw new BusinessException(userLogin.getLoginCode());
  141. }
  142. // //用户是否可用此登录渠道进行登录权限判断
  143. // if (!XIUtils.getListFromStrDelEmpty(sysUser.getFuncids()).stream()
  144. // .anyMatch(func -> func.equals(LoginMedia.getLoginMedia(loginMedia).getDesc()))) {
  145. // userLogin.setStatus(Loginflag.fail.getIndex());
  146. // userLogin.setLoginCode("EB8000012");
  147. // userLogin.setLoginMsg(UtilTools.getText(userLogin.getLoginCode()));
  148. // userLoginDAO.insertSelective(userLogin);
  149. //
  150. // throw new BusinessException(userLogin.getLoginCode());
  151. // }
  152. ///////////////////////////////
  153. userLoginDAO.insertSelective(userLogin);
  154. String userId = sysUser.getId();
  155. String tocken = this.getA_systemhead().getClientToken();
  156. if (loginWay.equals(LoginWay.MANUAL.getIndex())) { //手动登录
  157. if (UtilTools.isNotNullAndBlank(this.getA_a00002().getIsLoginAuto()) && this.getA_a00002().getIsLoginAuto().equals("1")) {
  158. tocken = JwtManageUtil.getClientLoginAutoJWT(MFMiniCoreProperties.getInstance().getUniqueid(), userId, userId);
  159. } else {
  160. tocken = UUID.randomUUID().toString().replace("-", "").trim();
  161. }
  162. }
  163. //部门自定义的用户权限
  164. Map deptFuncMap = new HashMap();
  165. SysDepartment sysDepartment = UtilTools.getBean(SysDepartmentMapper.class).selectByPrimaryKey(sysUser.getOrgDeptCode());
  166. if (sysDepartment != null) {
  167. XIUtils.getListFromStrDelEmpty(sysDepartment.getDeptfunc()).stream()
  168. .forEach(func->deptFuncMap.put(func,""));
  169. }
  170. //用户cache
  171. UtilTools.putUserCache(XIConstant.UserKeyPre + sysUser.getId().trim(), tocken);
  172. UtilTools.putUserCache(XIConstant.TokenKeyPre + tocken, sysUser.getId());
  173. LoginService.initUserAccess(sysUser.getId());
  174. this.getD_systemhead().setClientToken(tocken);
  175. this.getD_systemhead().setUserId(userId);
  176. this.getD_a00002().setNickName(sysUser.getNickname() == null ? sysUser.getUsername() : sysUser.getNickname());
  177. this.getD_a00002().setUserName(sysUser.getUsername());
  178. this.getD_a00002().setOrgCode(sysUser.getOrgcode());
  179. this.getD_a00002().setOrgDeptCode(sysUser.getOrgDeptCode());
  180. this.getD_a00002().setRegTime(XIDateTimeUtils.getStrFromDate(sysUser.getCreateTime()));
  181. this.getD_a00002().setMobileNo(sysUser.getUsermobnub());
  182. this.getD_a00002().setHeadProfile(sysUser.getHeadPortait());
  183. this.getD_a00002().setUserType(sysUser.getUserType());
  184. if(sysUser.getUserType().equals(UserType.son.getIndex())){
  185. }else {
  186. SysUserSQLBuilder sonUserSQLBuilder=new SysUserSQLBuilder();
  187. sonUserSQLBuilder.createCriteria().andSuperiorUserIdEqualTo(sysUser.getId());
  188. List<SysUser> sonUserList = UtilTools.getBean(SysUserMapper.class).selectByExample(sonUserSQLBuilder);
  189. List<String> userIdList = null;
  190. if(sonUserList!=null && sonUserList.size()>0) {
  191. userIdList=sonUserList.stream().map(data->data.getId()).distinct()
  192. .collect(Collectors.toList());
  193. }
  194. if(userIdList == null){
  195. userIdList=new ArrayList<>();
  196. }
  197. userIdList.add(sysUser.getId());
  198. }
  199. List<XIRoleRecode> roleList = LoginService.getAccessRole(sysUser.getId());
  200. if (roleList != null && roleList.size() > 0) {
  201. this.getD_a00002().setRoleList(roleList.stream().map(role -> {
  202. D_A00002_ROLELIST_RECODE recode = new D_A00002_ROLELIST_RECODE();
  203. recode.setRoleId(role.getRoleId());
  204. recode.setRoleName(role.getRoleName());
  205. recode.setRoleDesc(role.getRoleDesc());
  206. return recode;
  207. }).collect(Collectors.toList()));
  208. }
  209. List<XIFuncRecode> funcList = LoginService.getAccessFunction(sysUser.getId(),deptFuncMap);
  210. if (funcList != null && funcList.size() > 0) {
  211. this.getD_a00002().setFuncList(funcList.stream().map(func -> {
  212. D_A00002_FUNCLIST_RECODE recode = new D_A00002_FUNCLIST_RECODE();
  213. recode.setFuncId(func.getFuncId());
  214. recode.setFuncClass(func.getFuncClass());
  215. recode.setFuncDesc(func.getFuncDesc());
  216. recode.setFuncName(func.getFuncName());
  217. return recode;
  218. }).collect(Collectors.toList()));
  219. }
  220. List<XIMenuRecode> menuList = LoginService.getAccessMenu(sysUser.getId());
  221. if (menuList != null && menuList.size() > 0) {
  222. List<D_A00002_MENULIST_RECODE> menulistRecodes = menuList.stream().map(menu -> {
  223. D_A00002_MENULIST_RECODE recode = new D_A00002_MENULIST_RECODE();
  224. recode.setMenuId(menu.getMenuId());
  225. recode.setMenuDesc(menu.getMenuDesc());
  226. recode.setMenuFlag(menu.getMenuFlag());
  227. recode.setMenuImage(menu.getMenuImage());
  228. recode.setMenuName(menu.getMenuName());
  229. recode.setMenuParentId(menu.getMenuParentId());
  230. recode.setMenuUrl(menu.getMenuUrl());
  231. return recode;
  232. }).collect(Collectors.toList());
  233. this.getD_a00002().setMenuList(menulistRecodes);
  234. }
  235. // VExtUserSQLBuilder VExtUserSQLBuilder = new VExtUserSQLBuilder();
  236. // VExtUserSQLBuilder.createCriteria().andIdEqualTo(sysUser.getId());
  237. // List<VExtUser> VExtUserList = UtilTools.getBean(VExtUserMapper.class).selectByExample(VExtUserSQLBuilder);
  238. // if (VExtUserList != null && VExtUserList.size() > 0) {
  239. // VExtUser VExtUser = VExtUserList.get(0);
  240. //
  241. // String certState = VExtUser.getCertState();
  242. // if (certState == null) {
  243. // certState = CertificationState.noCertificate.getIndex();
  244. // }
  245. // this.getD_a00002().setAuthenticationState(certState);
  246. // this.getD_a00002().setCertType(VExtUser.getCertType());
  247. // this.getD_a00002().setCertName(VExtUser.getCertName());
  248. // this.getD_a00002().setCertNo(VExtUser.getCertNumber());
  249. // this.getD_a00002().setCertFile(VExtUser.getCertFile());
  250. // this.getD_a00002().setCertTime(XIDateTimeUtils.getStrFromDate(VExtUser.getCertAuthTime()));
  251. // }
  252. }
  253. /**
  254. * 基础系统,“登录”业务前处理
  255. */
  256. public void preTransFlow() throws Exception {
  257. this.validater();
  258. }
  259. /**
  260. * 基础系统,“登录”业务后处理
  261. */
  262. public void afterTransFlow() throws Exception {
  263. }
  264. /**
  265. * 基础系统,“登录”逻辑入口处理方法
  266. */
  267. @SuppressWarnings("rawtypes")
  268. @Override
  269. public Map execute(Map vars) throws Exception {
  270. this.setTransMap(vars);
  271. preTransFlow();// 执行业务开始的规则检查和校验
  272. transExecute();// 执行核心业务段
  273. afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
  274. return this.getTransMap();
  275. }
  276. }