@@ -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,8 +245,10 @@ public class SysLoginService
* @param dto
* @return
*/
@Transactional ( rollbackFor = Exception . class )
public AjaxResult appLogin ( LoginBody dto ) {
AjaxResult ajax = AjaxResult . success ( ) ;
try {
System . out . println ( " 小程序微信授权登录---------参数列表========dto======== " + JSON . toJSONString ( dto ) ) ;
JSONObject sessionInfo = wechatUtil . code2Session ( dto . getCode ( ) ) ;
String openid = sessionInfo . getString ( " openid " ) ;
@@ -256,14 +261,14 @@ public class SysLoginService
AppUser existingUser = appUserService . selectByOpenid ( openid ) ;
if ( existingUser ! = null ) {
if ( StringUtils . isEmpty ( existingUser . getIsCompanyUser ( ) ) ) {
existingUser . setIsCompanyUser ( dto . getUserType ( ) ) ;
appUserService . updateAppUser ( existingUser ) ;
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 ( ) ) ;
@@ -277,8 +282,7 @@ public class SysLoginService
String token = " " ;
boolean isNewUser = false ;
if ( existUser ! = null ) {
existUser . setO penid( openid ) ;
appUserService . updateAppUser ( existUser ) ;
updateAppUserCommon ( existUser , o penid, unionid , dto . getUserType ( ) ) ;
// 5. 生成系统令牌
token = loginUserIdApp ( existUser ) ;
ajax . put ( " idCard " , existUser . getIdCard ( ) ) ;
@@ -299,8 +303,180 @@ public class SysLoginService
}
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 ( " 获取手机号失败 " ) ;
}
AppUser phoneUser = appUserService . getPhone ( phone ) ;
if ( phoneUser ! = null ) {
return handlePhoneBoundUser ( phoneUser , openid , unionid , dto . getUserType ( ) ) ;
} else {
return handleNewUser ( openid , unionid , phone , dto . getUserType ( ) ) ;
}
} 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 ) ;
}
/**