提交 4ab38072 authored 作者: hzh's avatar hzh

代码优化

上级 edef017d
...@@ -61,7 +61,7 @@ public interface RemoteUserService { ...@@ -61,7 +61,7 @@ public interface RemoteUserService {
* @param tenantId 租户id * @param tenantId 租户id
* @return 结果 * @return 结果
*/ */
XcxLoginUser getUserInfoByOpenid(String openid, String tenantId) throws UserException; XcxLoginUser getUserInfoByOpenid(String openid, String phone, String tenantId) throws UserException;
/** /**
* 注册用户信息 * 注册用户信息
......
...@@ -30,4 +30,17 @@ public class XcxLoginBody extends LoginBody { ...@@ -30,4 +30,17 @@ public class XcxLoginBody extends LoginBody {
@NotBlank(message = "{xcx.code.not.blank}") @NotBlank(message = "{xcx.code.not.blank}")
private String xcxCode; private String xcxCode;
/**
* 用户信息,base64编码
*/
private String encryptedData;
/**
* 加密算法的初始向量
*/
private String iv;
/**
* 用户登录凭证
*/
private String sessionKey;
} }
...@@ -9,15 +9,18 @@ import org.dromara.auth.domain.vo.LoginVo; ...@@ -9,15 +9,18 @@ import org.dromara.auth.domain.vo.LoginVo;
import org.dromara.auth.form.XcxLoginBody; import org.dromara.auth.form.XcxLoginBody;
import org.dromara.auth.service.IAuthStrategy; import org.dromara.auth.service.IAuthStrategy;
import org.dromara.auth.service.SysLoginService; import org.dromara.auth.service.SysLoginService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.weixin.dto.WxAuthPhoneParseReq;
import org.dromara.common.weixin.dto.WxAuthReq; import org.dromara.common.weixin.dto.WxAuthReq;
import org.dromara.common.weixin.dto.WxAuthResp; import org.dromara.common.weixin.dto.WxAuthResp;
import org.dromara.common.weixin.utils.WeixinUtils; import org.dromara.common.weixin.utils.WeixinUtils;
import org.dromara.server.api.RemoteServerService; import org.dromara.server.api.RemoteServerService;
import org.dromara.server.api.domain.RemoteUser; import org.dromara.server.api.domain.RemoteUser;
import org.dromara.system.api.RemoteUserService; import org.dromara.system.api.RemoteUserService;
import org.dromara.system.api.domain.bo.RemoteWxUserBo;
import org.dromara.system.api.domain.vo.RemoteClientVo; import org.dromara.system.api.domain.vo.RemoteClientVo;
import org.dromara.system.api.model.XcxLoginUser; import org.dromara.system.api.model.XcxLoginUser;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -49,22 +52,39 @@ public class XcxAuthStrategy extends AbstractMallStrategy implements IAuthStrate ...@@ -49,22 +52,39 @@ public class XcxAuthStrategy extends AbstractMallStrategy implements IAuthStrate
// xcxCode 为 小程序调用 wx.login 授权后获取 // xcxCode 为 小程序调用 wx.login 授权后获取
String xcxCode = loginBody.getXcxCode(); String xcxCode = loginBody.getXcxCode();
// 多个小程序识别使用 // 多个小程序识别使用
// String appid = loginBody.getAppid();
WxAuthResp auth = WeixinUtils.auth(new WxAuthReq() WxAuthResp auth = WeixinUtils.auth(new WxAuthReq()
.setCode(xcxCode) .setCode(xcxCode)
.setAppid(client.getClientKey()) .setAppid(client.getClientKey())
.setSecret(client.getClientSecret())); .setSecret(client.getClientSecret()));
// 校验 appid + appsrcret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
String openid = auth.getOpenId(); String openid = auth.getOpenId();
XcxLoginUser loginUser = remoteUserService.getUserInfoByOpenid(openid, loginBody.getTenantId());
String phone = null;
if (StringUtils.isNotBlank(loginBody.getEncryptedData())) {
phone = WeixinUtils.authParsePhone(new WxAuthPhoneParseReq()
.setEncryptedData(loginBody.getEncryptedData())
.setIv(loginBody.getIv())
.setSessionKey(loginBody.getSessionKey()));
remoteUserService.registerWxUserInfo(new RemoteWxUserBo()
.setTenantId(loginBody.getTenantId())
.setOpenId(openid)
.setPhonenumber(phone));
}
XcxLoginUser loginUser = remoteUserService.getUserInfoByOpenid(openid, phone, loginBody.getTenantId());
// 用户不存在 返回 openid // 用户不存在 返回 openid
if (loginUser == null) { if (loginUser == null) {
LoginVo loginVo = new LoginVo(); if (phone == null) {
loginVo.setOpenid(openid); LoginVo loginVo = new LoginVo();
loginVo.setScope(auth.getSessionKey()); loginVo.setOpenid(openid);
return loginVo; loginVo.setScope(auth.getSessionKey());
return loginVo;
} else {
throw new RuntimeException("用户不存在");
}
} }
loginUser.setClientKey(client.getClientKey()); loginUser.setClientKey(client.getClientKey());
......
...@@ -60,7 +60,7 @@ public class XcxPhoneAuthStrategy extends AbstractMallStrategy implements IAuthS ...@@ -60,7 +60,7 @@ public class XcxPhoneAuthStrategy extends AbstractMallStrategy implements IAuthS
//校验手机号 //校验手机号
validatePhone(loginBody.getPhone(), phone); validatePhone(loginBody.getPhone(), phone);
XcxLoginUser loginUser = remoteUserService.getUserInfoByOpenid(loginBody.getOpenId(), loginBody.getTenantId()); XcxLoginUser loginUser = remoteUserService.getUserInfoByOpenid(loginBody.getOpenId(), phone, loginBody.getTenantId());
if (loginUser == null) { if (loginUser == null) {
throw new RuntimeException("用户不存在"); throw new RuntimeException("用户不存在");
......
...@@ -164,12 +164,16 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -164,12 +164,16 @@ public class RemoteUserServiceImpl implements RemoteUserService {
* @return 结果 * @return 结果
*/ */
@Override @Override
public XcxLoginUser getUserInfoByOpenid(String openid, String tenantId) throws UserException { public XcxLoginUser getUserInfoByOpenid(String openid, String phone, String tenantId) throws UserException {
WxUserVo wxUser = wxUserService.selectUserByOpenId(openid, tenantId); WxUserVo wxUser = wxUserService.selectUserByOpenId(openid, phone, tenantId);
if (!ObjectUtil.isNotNull(wxUser)) { if (!ObjectUtil.isNotNull(wxUser)) {
return null; return null;
} }
if (StringUtils.isNotBlank(phone) && !StringUtils.equals(phone, wxUser.getPhonenumber())) {
throw new UserException("user.not.exists", phone);
}
//根据手机号查询用户信息 //根据手机号查询用户信息
SysUserVo user = userService.selectUserByPhonenumber(wxUser.getPhonenumber(), wxUser.getTenantId()); SysUserVo user = userService.selectUserByPhonenumber(wxUser.getPhonenumber(), wxUser.getTenantId());
if (!ObjectUtil.isNotNull(user)) { if (!ObjectUtil.isNotNull(user)) {
...@@ -223,7 +227,7 @@ public class RemoteUserServiceImpl implements RemoteUserService { ...@@ -223,7 +227,7 @@ public class RemoteUserServiceImpl implements RemoteUserService {
public Boolean registerWxUserInfo(RemoteWxUserBo remoteWxUserBo) { public Boolean registerWxUserInfo(RemoteWxUserBo remoteWxUserBo) {
WxUserBo user = BeanUtil.copyProperties(remoteWxUserBo, WxUserBo.class); WxUserBo user = BeanUtil.copyProperties(remoteWxUserBo, WxUserBo.class);
WxUserVo wx = wxUserService.selectUserByOpenId(remoteWxUserBo.getOpenId(), remoteWxUserBo.getTenantId()); WxUserVo wx = wxUserService.selectUserByOpenId(remoteWxUserBo.getOpenId(), remoteWxUserBo.getPhonenumber(), remoteWxUserBo.getTenantId());
if (ObjectUtil.isNotNull(wx)) { if (ObjectUtil.isNotNull(wx)) {
return true; return true;
} }
......
...@@ -13,11 +13,12 @@ public interface IWxUserService { ...@@ -13,11 +13,12 @@ public interface IWxUserService {
/** /**
* 通过openId查询用户 * 通过openId查询用户
* *
* @param openId openId * @param openId openId
* @param phone phone
* @param tenantId 租户ID * @param tenantId 租户ID
* @return 用户对象信息 * @return 用户对象信息
*/ */
WxUserVo selectUserByOpenId(String openId,String tenantId); WxUserVo selectUserByOpenId(String openId, String phone, String tenantId);
/** /**
* 注册用户 * 注册用户
......
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.WxUser; import org.dromara.system.domain.WxUser;
import org.dromara.system.domain.bo.WxUserBo; import org.dromara.system.domain.bo.WxUserBo;
import org.dromara.system.domain.vo.WxUserVo; import org.dromara.system.domain.vo.WxUserVo;
...@@ -32,8 +33,12 @@ public class WxUserServiceImpl implements IWxUserService { ...@@ -32,8 +33,12 @@ public class WxUserServiceImpl implements IWxUserService {
* @return 用户对象信息 * @return 用户对象信息
*/ */
@Override @Override
public WxUserVo selectUserByOpenId(String openId, String tenantId) { public WxUserVo selectUserByOpenId(String openId, String phone, String tenantId) {
return baseMapper.selectVoOne(new LambdaQueryWrapper<WxUser>().eq(WxUser::getOpenId, openId).eq(WxUser::getTenantId, tenantId)); return baseMapper.selectVoOne(
new LambdaQueryWrapper<WxUser>()
.eq(WxUser::getOpenId, openId)
.eq(StringUtils.isNotBlank(phone), WxUser::getPhonenumber, phone)
.eq(WxUser::getTenantId, tenantId));
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论