From ac12331e8d78f1048f1dcc524db75f2a203deefa Mon Sep 17 00:00:00 2001 From: sh Date: Sat, 6 Dec 2025 13:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=96=B9=E6=B3=95-=E6=9C=AA=E8=B0=83=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E9=9C=80=E8=A6=81=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cms/service/impl/AppUserServiceImpl.java | 2 + .../cms/service/impl/ESJobSearchImpl.java | 6 + .../java/com/ruoyi/cms/util/StringUtil.java | 17 +- .../web/service/SysLoginService.java | 272 ++++++++++++++---- 4 files changed, 247 insertions(+), 50 deletions(-) diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/AppUserServiceImpl.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/AppUserServiceImpl.java index e896b8e..20f20b6 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/AppUserServiceImpl.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/AppUserServiceImpl.java @@ -107,6 +107,7 @@ public class AppUserServiceImpl extends ServiceImpl imple * @return 结果 */ @Override + @Transactional(rollbackFor = Exception.class) public int insertAppUser(AppUser appUser) { return appUserMapper.insert(appUser); @@ -119,6 +120,7 @@ public class AppUserServiceImpl extends ServiceImpl imple * @return 结果 */ @Override + @Transactional(rollbackFor = Exception.class) public int updateAppUser(AppUser appUser) { //工作经历 diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/ESJobSearchImpl.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/ESJobSearchImpl.java index 8888758..98b4385 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/ESJobSearchImpl.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/service/impl/ESJobSearchImpl.java @@ -441,6 +441,12 @@ public class ESJobSearchImpl implements IESJobSearchService if(esJobSearch.getJobId()!=null){ wrapper.and(x->x.eq(ESJobDocument::getJobId,esJobSearch.getJobId())); } + if(!StringUtil.isEmptyOrNull(esJobSearch.getJobAddress())){ + wrapper.and(x->x.like(ESJobDocument::getJobAddress,esJobSearch.getJobAddress())); + } + if(!StringUtil.isEmptyOrNull(esJobSearch.getJobLocation())){ + wrapper.and(x->x.like(ESJobDocument::getJobLocation,esJobSearch.getJobLocation())); + } if(Objects.nonNull(esJobSearch.getOrder())){ if(esJobSearch.getOrder()==1){ wrapper.orderByDesc(ESJobDocument::getIsHot); diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/util/StringUtil.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/util/StringUtil.java index 3a71f2b..b061589 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/util/StringUtil.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/util/StringUtil.java @@ -12,11 +12,24 @@ public class StringUtil { /*1101(求职者)、1102(招聘者)、1103(网格员)、1104(内部工作者)*/ public static final Long COMPANY_ADMIN_ROLE_KEY = 1102L; + /************************移动端角色开始***************************/ /** - * 企业用户 + * 移动端-企业用户 */ public static final String IS_COMPANY_USER = "0"; - + /** + * 移动端-求职者 + */ + public static final String IS_JOB_REQUEST_USER = "1"; + /** + * 移动端-网格员 + */ + public static final String IS_GRID_USER = "2"; + /** + * 移动端-内部工作者 + */ + public static final String IS_INTERNAL_USER = "3"; + /************************移动端角色结束***************************/ /** * pc端-求职者 */ diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 31ee9eb..47258c3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -5,6 +5,7 @@ import javax.annotation.Resource; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson2.JSON; import com.ruoyi.cms.service.IAppUserService; +import com.ruoyi.cms.util.StringUtil; import com.ruoyi.cms.util.WechatUtil; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.AppUser; @@ -12,6 +13,7 @@ import com.ruoyi.common.core.domain.model.LoginBody; import com.ruoyi.common.core.domain.model.LoginSiteUser; import com.ruoyi.common.core.domain.model.RegisterBody; import com.ruoyi.common.utils.*; +import com.ruoyi.framework.web.exception.ParamErrorConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; @@ -36,6 +38,7 @@ import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.framework.security.context.AuthenticationContextHolder; import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysUserService; +import org.springframework.transaction.annotation.Transactional; /** * 登录校验方法 @@ -242,67 +245,240 @@ public class SysLoginService * @param dto * @return */ + @Transactional(rollbackFor = Exception.class) public AjaxResult appLogin(LoginBody dto){ AjaxResult ajax = AjaxResult.success(); - System.out.println("小程序微信授权登录---------参数列表========dto========"+ JSON.toJSONString(dto)); - JSONObject sessionInfo = wechatUtil.code2Session(dto.getCode()); - String openid = sessionInfo.getString("openid"); - String unionid = sessionInfo.getString("unionid"); - String sessionKey = sessionInfo.getString("session_key"); - if (openid == null) { - return AjaxResult.error("微信授权失败"); - } - //验证是否登录过 - AppUser existingUser=appUserService.selectByOpenid(openid); - if(existingUser!=null){ - if(StringUtils.isEmpty(existingUser.getIsCompanyUser())){ - existingUser.setIsCompanyUser(dto.getUserType()); - appUserService.updateAppUser(existingUser); + try { + System.out.println("小程序微信授权登录---------参数列表========dto========"+ JSON.toJSONString(dto)); + JSONObject sessionInfo = wechatUtil.code2Session(dto.getCode()); + String openid = sessionInfo.getString("openid"); + String unionid = sessionInfo.getString("unionid"); + String sessionKey = sessionInfo.getString("session_key"); + if (openid == null) { + return AjaxResult.error("微信授权失败"); } - String token = loginUserIdApp(existingUser); - ajax.put(Constants.TOKEN, token); - ajax.put("isNewUser", false); - ajax.put("idCard",existingUser.getIdCard()); - ajax.put("isCompanyUser",existingUser.getIsCompanyUser()); - return ajax; - }else { + //验证是否登录过 + AppUser existingUser=appUserService.selectByOpenid(openid); + if(existingUser!=null){ + if(StringUtils.isEmpty(existingUser.getIsCompanyUser())){ + updateAppUserCommon(existingUser,openid,unionid,dto.getUserType()); + } + String token = loginUserIdApp(existingUser); + ajax.put(Constants.TOKEN, token); + ajax.put("isNewUser", false); + ajax.put("idCard",existingUser.getIdCard()); + ajax.put("isCompanyUser",existingUser.getIsCompanyUser()); + System.out.println("返回ajax====================================="+JSON.toJSONString(ajax)); + return ajax; + }else { + JSONObject phoneInfo = wechatUtil.decryptPhoneNumber(dto.getEncryptedData(), sessionKey, dto.getIv()); + String phone = phoneInfo.getString("phoneNumber"); + if (phone == null) { + return AjaxResult.error("获取手机号失败"); + } + + // 3. 检查手机号是否已被绑定 + AppUser existUser = appUserService.getPhone(phone); + String token=""; + boolean isNewUser=false; + if (existUser != null) { + updateAppUserCommon(existUser,openid,unionid,dto.getUserType()); + // 5. 生成系统令牌 + token = loginUserIdApp(existUser); + ajax.put("idCard",existUser.getIdCard()); + ajax.put("isCompanyUser",existUser.getIsCompanyUser()); + }else{ + // 4. 创建用户并存储所有信息 + AppUser appUser = new AppUser(); + appUser.setOpenid(openid); + appUser.setUnionid(unionid); + appUser.setPhone(phone); + appUser.setIsCompanyUser(dto.getUserType());//保存角色 + appUserService.insertAppUser(appUser); + // 5. 生成系统令牌 + token = loginUserIdApp(appUser); + isNewUser=true; + ajax.put("idCard",null); + ajax.put("isCompanyUser",dto.getUserType()); + } + ajax.put("isNewUser", isNewUser); + ajax.put(Constants.TOKEN, token); + System.out.println("返回ajax====================================="+JSON.toJSONString(ajax)); + return ajax; + } + }catch (Exception e) { + System.err.println("登录失败:" + e.getMessage()); + return AjaxResult.error("登录失败,请稍后重试"); + } + } + + /** + * 小程序登录主逻辑 + */ + public AjaxResult appLoginNew(LoginBody dto) { + AjaxResult validateResult = validateLoginParam(dto, false); + if (validateResult != null) { + return validateResult; + } + + try { + JSONObject sessionInfo = wechatUtil.code2Session(dto.getCode()); + String openid = sessionInfo.getString("openid"); + String unionid = sessionInfo.getString("unionid"); + String sessionKey = sessionInfo.getString("session_key"); + + if (openid == null) { + return AjaxResult.error("微信授权失败"); + } + + AppUser existingUser = appUserService.selectByOpenid(openid); + if (existingUser != null) { + return handleExistingUser(existingUser, dto.getUserType()); + } + + validateResult = validateLoginParam(dto, true); + if (validateResult != null) { + return validateResult; + } + JSONObject phoneInfo = wechatUtil.decryptPhoneNumber(dto.getEncryptedData(), sessionKey, dto.getIv()); String phone = phoneInfo.getString("phoneNumber"); + if (phone == null) { return AjaxResult.error("获取手机号失败"); } - // 3. 检查手机号是否已被绑定 - AppUser existUser = appUserService.getPhone(phone); - String token=""; - boolean isNewUser=false; - if (existUser != null) { - existUser.setOpenid(openid); - appUserService.updateAppUser(existUser); - // 5. 生成系统令牌 - token = loginUserIdApp(existUser); - ajax.put("idCard",existUser.getIdCard()); - ajax.put("isCompanyUser",existUser.getIsCompanyUser()); - }else{ - // 4. 创建用户并存储所有信息 - AppUser appUser = new AppUser(); - appUser.setOpenid(openid); - appUser.setUnionid(unionid); - appUser.setPhone(phone); - appUser.setIsCompanyUser(dto.getUserType());//保存角色 - appUserService.insertAppUser(appUser); - // 5. 生成系统令牌 - token = loginUserIdApp(appUser); - isNewUser=true; - ajax.put("idCard",null); - ajax.put("isCompanyUser",dto.getUserType()); + AppUser phoneUser = appUserService.getPhone(phone); + if (phoneUser != null) { + return handlePhoneBoundUser(phoneUser, openid, unionid, dto.getUserType()); + } else { + return handleNewUser(openid, unionid, phone, dto.getUserType()); } - ajax.put("isNewUser", isNewUser); - ajax.put(Constants.TOKEN, token); - return ajax; + } catch (Exception e) { + System.err.println("登录失败:" + e.getMessage()); + return AjaxResult.error("登录失败,请稍后重试"); } } + /** + * 参数校验方法(仅返回错误信息,补充userType校验) + */ + private AjaxResult validateLoginParam(LoginBody dto, boolean needDecryptPhone) { + if (dto == null) { + return AjaxResult.error(ParamErrorConstants.PARAM_NULL_MSG); + } + if (StringUtils.isEmpty(dto.getCode())) { + return AjaxResult.error(ParamErrorConstants.CODE_EMPTY_MSG); + } + String userType = dto.getUserType(); + if (StringUtils.isEmpty(userType) || + !StringUtil.IS_COMPANY_USER.equals(userType) && + !StringUtil.IS_JOB_REQUEST_USER.equals(userType)) { + return AjaxResult.error(ParamErrorConstants.USER_TYPE_INVALID_MSG); + } + if (needDecryptPhone) { + if (StringUtils.isEmpty(dto.getEncryptedData())) { + return AjaxResult.error(ParamErrorConstants.ENCRYPTED_DATA_EMPTY_MSG); + } + if (StringUtils.isEmpty(dto.getIv())) { + return AjaxResult.error(ParamErrorConstants.IV_EMPTY_MSG); + } + } + return null; + } + + /** + * 处理老用户登录(日志用println) + */ + @Transactional(rollbackFor = Exception.class) + public AjaxResult handleExistingUser(AppUser existingUser, String userType) { + AjaxResult ajax = AjaxResult.success(); + updateAppUserCommon(existingUser, null, null, userType); + String token = loginUserIdApp(existingUser); + ajax.put(Constants.TOKEN, token); + ajax.put("isNewUser", false); + ajax.put("idCard", existingUser.getIdCard()); + ajax.put("isCompanyUser", existingUser.getIsCompanyUser()); + System.out.println("老用户登录成功,openid:" + existingUser.getOpenid() + "==========="); + System.out.println(ParamErrorConstants.LOG_AJAX_RETURN + JSON.toJSONString(ajax)); + return ajax; + } + + + /** + * 处理手机号已绑定的用户 + */ + @Transactional(rollbackFor = Exception.class) + public AjaxResult handlePhoneBoundUser(AppUser phoneUser, String openid, String unionid, String userType) { + AjaxResult ajax = AjaxResult.success(); + if (StringUtils.hasText(phoneUser.getOpenid()) && !openid.equals(phoneUser.getOpenid())) { + System.out.printf("手机号绑定冲突,phone:%s, oldOpenid:%s, newOpenid:%s%n", + phoneUser.getPhone(), phoneUser.getOpenid(), openid); + return AjaxResult.error("该手机号已绑定其他微信账号"); + } + //修改用户信息 + updateAppUserCommon(phoneUser, openid, unionid, userType); + phoneUser.setOpenid(openid); + phoneUser.setUnionid(unionid); + phoneUser.setIsCompanyUser(userType); + String token = loginUserIdApp(phoneUser); + ajax.put(Constants.TOKEN, token); + ajax.put("isNewUser", false); + ajax.put("idCard", phoneUser.getIdCard()); + ajax.put("isCompanyUser", phoneUser.getIsCompanyUser()); + System.out.println("手机号绑定用户登录成功,phone:" + phoneUser.getPhone()); + System.out.println(ParamErrorConstants.LOG_AJAX_RETURN + JSON.toJSONString(ajax)); + return ajax; + } + + /** + * 处理新用户注册 + */ + @Transactional(rollbackFor = Exception.class) + public AjaxResult handleNewUser(String openid, String unionid, String phone, String userType) { + AjaxResult ajax = AjaxResult.success(); + AppUser newUser = new AppUser(); + newUser.setOpenid(openid); + newUser.setUnionid(unionid); + newUser.setPhone(phone); + newUser.setIsCompanyUser(userType); + appUserService.insertAppUser(newUser); + String token = loginUserIdApp(newUser); + ajax.put(Constants.TOKEN, token); + ajax.put("isNewUser", true); + ajax.put("idCard", null); + ajax.put("isCompanyUser", userType); + System.out.printf("新用户创建成功,openid:%s, phone:%s%n", openid, phone); + System.out.println(ParamErrorConstants.LOG_AJAX_RETURN + JSON.toJSONString(ajax)); + return ajax; + } + + /** + * 抽取用户更新公共方法 + */ + private void updateAppUserCommon(AppUser targetUser, String openid, String unionid, String userType) { + AppUser updateParm = new AppUser(); + updateParm.setUserId(targetUser.getUserId()); + + String currentRole = targetUser.getIsCompanyUser(); + if (!StringUtil.IS_GRID_USER.equals(currentRole) && !StringUtil.IS_INTERNAL_USER.equals(currentRole)) { + updateParm.setIsCompanyUser(userType); + targetUser.setIsCompanyUser(userType); + }else{ + System.out.printf("用户角色不允许修改,openid:%s, 当前角色:%s, 传入角色:%s%n", + targetUser.getOpenid(), currentRole, userType); + } + if (StringUtils.isNotBlank(openid)) { + updateParm.setOpenid(openid); + targetUser.setOpenid(openid); + } + if (StringUtils.isNotBlank(unionid)) { + updateParm.setUnionid(unionid); + targetUser.setUnionid(unionid); + } + appUserService.updateAppUser(updateParm); + } + /** * 注册 * @param registerBody