修改互联网单点登录

This commit is contained in:
sh
2025-11-16 23:35:14 +08:00
parent cc37ece461
commit 433ec1f8b0
6 changed files with 142 additions and 17 deletions

View File

@@ -1,8 +1,11 @@
package com.ruoyi.framework.web.service;
import com.ruoyi.cms.service.CompanyContactService;
import com.ruoyi.cms.service.ICompanyService;
import com.ruoyi.cms.util.StringUtil;
import com.ruoyi.cms.util.oauth.OauthClient;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.common.core.domain.entity.CompanyContact;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.tymh.nwToken.PortalTokenCacheDTO;
@@ -45,6 +48,11 @@ public class OauthLoginHlwService {
private ISysUserService sysUserService;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private ICompanyService companyService;
@Autowired
private CompanyContactService companyContactService;
// Redis缓存门户UserID → 若依本地用户名(避免重复匹配数据库)
private static final String REDIS_KEY_PORTAL_USER_MAPPING = "hlw:user:mapping:";
// 门户 Token 存储前缀Redis 键:门户 userId → 门户 Token 信息)
@@ -69,7 +77,7 @@ public class OauthLoginHlwService {
throw new ServiceException("获取门户 Token 失败:" + wwToken);
}
//获取门户userInfo
WwTyInfo portalUser = oauthClient.wwGetUserInfo(wwToken);
WwTyInfo portalUser = oauthClient.wwGetUserInfo(wwTokenResult);
//匹配/创建本地用户
String localUsername = getOrCreateLocalUser(portalUser);
//执行原来的登录流程
@@ -89,14 +97,22 @@ public class OauthLoginHlwService {
* 匹配/创建本地用户,返回若依本地用户名
*/
private String getOrCreateLocalUser(WwTyInfo wwTyInfo) {
SysUser localUser=sysUserService.selectUserByIdCard(wwTyInfo.getIdno());
String idCard="";
switch (wwTyInfo.getUsertype()){
case "1"://个人
idCard=wwTyInfo.getIdno();
break;
default:
idCard=wwTyInfo.getEnterprisecode();
}
// 先从Redis查询缓存的本地用户名
String cacheKey = REDIS_KEY_PORTAL_USER_MAPPING + localUser.getUserId();
String cacheKey = REDIS_KEY_PORTAL_USER_MAPPING + idCard;
String localUsername = redisCache.getCacheObject(cacheKey);
if (StringUtils.isNotBlank(localUsername)) {
return localUsername;
}
SysUser localUser=sysUserService.selectUserByIdCard(wwTyInfo.getIdno());
if (localUser == null) {
// 本地无用户,自动创建
localUser = createLocalUser(wwTyInfo);
@@ -138,10 +154,15 @@ public class OauthLoginHlwService {
newUser.setIdCard(wwTyInfo.getEnterprisecode());
newUser.setRoleIds(new Long[]{parseStringToLoing(StringUtil.SYS_QY)});
newUser.setUserName(wwTyInfo.getEnterprisename());
//企业联系人
CompanyContact companyContact=new CompanyContact();
companyContact.setContactPerson(wwTyInfo.getContactperson());
companyContact.setContactPersonPhone(wwTyInfo.getContactphone());
//企业联系人->现根据社会信用代码查询企业信息
Company company=companyService.queryCodeCompany(wwTyInfo.getEnterprisecode());
if(company!=null){
CompanyContact companyContact=new CompanyContact();
companyContact.setContactPerson(wwTyInfo.getContactperson());
companyContact.setContactPersonPhone(wwTyInfo.getContactphone());
companyContact.setCompanyId(company.getCompanyId());
companyContactService.insertContact(companyContact);
}
}
newUser.setPassword(SecurityUtils.encryptPassword("123456"));
newUser.setDelFlag("0");