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

代码优化

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