修改微信登录方法-未调用,需要测试
This commit is contained in:
@@ -107,6 +107,7 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper,AppUser> imple
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int insertAppUser(AppUser appUser)
|
public int insertAppUser(AppUser appUser)
|
||||||
{
|
{
|
||||||
return appUserMapper.insert(appUser);
|
return appUserMapper.insert(appUser);
|
||||||
@@ -119,6 +120,7 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper,AppUser> imple
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int updateAppUser(AppUser appUser)
|
public int updateAppUser(AppUser appUser)
|
||||||
{
|
{
|
||||||
//工作经历
|
//工作经历
|
||||||
|
|||||||
@@ -441,6 +441,12 @@ public class ESJobSearchImpl implements IESJobSearchService
|
|||||||
if(esJobSearch.getJobId()!=null){
|
if(esJobSearch.getJobId()!=null){
|
||||||
wrapper.and(x->x.eq(ESJobDocument::getJobId,esJobSearch.getJobId()));
|
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(Objects.nonNull(esJobSearch.getOrder())){
|
||||||
if(esJobSearch.getOrder()==1){
|
if(esJobSearch.getOrder()==1){
|
||||||
wrapper.orderByDesc(ESJobDocument::getIsHot);
|
wrapper.orderByDesc(ESJobDocument::getIsHot);
|
||||||
|
|||||||
@@ -12,11 +12,24 @@ public class StringUtil {
|
|||||||
/*1101(求职者)、1102(招聘者)、1103(网格员)、1104(内部工作者)*/
|
/*1101(求职者)、1102(招聘者)、1103(网格员)、1104(内部工作者)*/
|
||||||
public static final Long COMPANY_ADMIN_ROLE_KEY = 1102L;
|
public static final Long COMPANY_ADMIN_ROLE_KEY = 1102L;
|
||||||
|
|
||||||
|
/************************移动端角色开始***************************/
|
||||||
/**
|
/**
|
||||||
* 企业用户
|
* 移动端-企业用户
|
||||||
*/
|
*/
|
||||||
public static final String IS_COMPANY_USER = "0";
|
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端-求职者
|
* pc端-求职者
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import javax.annotation.Resource;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.ruoyi.cms.service.IAppUserService;
|
import com.ruoyi.cms.service.IAppUserService;
|
||||||
|
import com.ruoyi.cms.util.StringUtil;
|
||||||
import com.ruoyi.cms.util.WechatUtil;
|
import com.ruoyi.cms.util.WechatUtil;
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
import com.ruoyi.common.core.domain.entity.AppUser;
|
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.LoginSiteUser;
|
||||||
import com.ruoyi.common.core.domain.model.RegisterBody;
|
import com.ruoyi.common.core.domain.model.RegisterBody;
|
||||||
import com.ruoyi.common.utils.*;
|
import com.ruoyi.common.utils.*;
|
||||||
|
import com.ruoyi.framework.web.exception.ParamErrorConstants;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
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.framework.security.context.AuthenticationContextHolder;
|
||||||
import com.ruoyi.system.service.ISysConfigService;
|
import com.ruoyi.system.service.ISysConfigService;
|
||||||
import com.ruoyi.system.service.ISysUserService;
|
import com.ruoyi.system.service.ISysUserService;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录校验方法
|
* 登录校验方法
|
||||||
@@ -242,67 +245,240 @@ public class SysLoginService
|
|||||||
* @param dto
|
* @param dto
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public AjaxResult appLogin(LoginBody dto){
|
public AjaxResult appLogin(LoginBody dto){
|
||||||
AjaxResult ajax = AjaxResult.success();
|
AjaxResult ajax = AjaxResult.success();
|
||||||
System.out.println("小程序微信授权登录---------参数列表========dto========"+ JSON.toJSONString(dto));
|
try {
|
||||||
JSONObject sessionInfo = wechatUtil.code2Session(dto.getCode());
|
System.out.println("小程序微信授权登录---------参数列表========dto========"+ JSON.toJSONString(dto));
|
||||||
String openid = sessionInfo.getString("openid");
|
JSONObject sessionInfo = wechatUtil.code2Session(dto.getCode());
|
||||||
String unionid = sessionInfo.getString("unionid");
|
String openid = sessionInfo.getString("openid");
|
||||||
String sessionKey = sessionInfo.getString("session_key");
|
String unionid = sessionInfo.getString("unionid");
|
||||||
if (openid == null) {
|
String sessionKey = sessionInfo.getString("session_key");
|
||||||
return AjaxResult.error("微信授权失败");
|
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);
|
|
||||||
}
|
}
|
||||||
String token = loginUserIdApp(existingUser);
|
//验证是否登录过
|
||||||
ajax.put(Constants.TOKEN, token);
|
AppUser existingUser=appUserService.selectByOpenid(openid);
|
||||||
ajax.put("isNewUser", false);
|
if(existingUser!=null){
|
||||||
ajax.put("idCard",existingUser.getIdCard());
|
if(StringUtils.isEmpty(existingUser.getIsCompanyUser())){
|
||||||
ajax.put("isCompanyUser",existingUser.getIsCompanyUser());
|
updateAppUserCommon(existingUser,openid,unionid,dto.getUserType());
|
||||||
return ajax;
|
}
|
||||||
}else {
|
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());
|
JSONObject phoneInfo = wechatUtil.decryptPhoneNumber(dto.getEncryptedData(), sessionKey, dto.getIv());
|
||||||
String phone = phoneInfo.getString("phoneNumber");
|
String phone = phoneInfo.getString("phoneNumber");
|
||||||
|
|
||||||
if (phone == null) {
|
if (phone == null) {
|
||||||
return AjaxResult.error("获取手机号失败");
|
return AjaxResult.error("获取手机号失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 检查手机号是否已被绑定
|
AppUser phoneUser = appUserService.getPhone(phone);
|
||||||
AppUser existUser = appUserService.getPhone(phone);
|
if (phoneUser != null) {
|
||||||
String token="";
|
return handlePhoneBoundUser(phoneUser, openid, unionid, dto.getUserType());
|
||||||
boolean isNewUser=false;
|
} else {
|
||||||
if (existUser != null) {
|
return handleNewUser(openid, unionid, phone, dto.getUserType());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
ajax.put("isNewUser", isNewUser);
|
} catch (Exception e) {
|
||||||
ajax.put(Constants.TOKEN, token);
|
System.err.println("登录失败:" + e.getMessage());
|
||||||
return ajax;
|
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
|
* @param registerBody
|
||||||
|
|||||||
Reference in New Issue
Block a user