fix: 微信登录,接口修改。

This commit is contained in:
tianyongbao
2026-01-18 20:23:15 +08:00
parent 32844af3dd
commit a2e6579a3a
4 changed files with 178 additions and 29 deletions

View File

@@ -40,6 +40,7 @@ import com.intc.web.service.SysLoginService;
import com.intc.web.service.SysRegisterService;
import com.intc.weixin.domain.bo.ReqWxLogin;
import com.intc.weixin.service.WxLoginService;
import com.intc.weixin.config.WxMaProperties;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -83,6 +84,9 @@ public class AuthController {
@Autowired(required = false)
private WxLoginService wxLoginService;
@Autowired(required = false)
private WxMaProperties wxMaProperties;
/**
* 登录方法
@@ -250,14 +254,27 @@ public class AuthController {
/**
* 微信小程序登录
*
* @param request 微信登录请求
* @param body 微信登录请求JSON字符串
* @return 结果
*/
@ApiEncrypt
@PostMapping("/wechat_login")
public R<AquUser> wechatLogin(@Validated @RequestBody ReqWxLogin request) {
public R<LoginVo> wechatLogin(@RequestBody String body) {
try {
log.info("收到微信登录请求: clientId={}, tenantId={}", request.getClientId(), request.getTenantId());
ReqWxLogin request = JsonUtils.parseObject(body, ReqWxLogin.class);
ValidatorUtils.validate(request);
// 如果未传递 clientId使用默认配置的 clientId
if (StringUtils.isBlank(request.getClientId()) && wxMaProperties != null) {
request.setClientId(wxMaProperties.getDefaultClientId());
// 更新 body 中的 clientId
body = JsonUtils.toJsonString(request);
}
String clientId = request.getClientId();
String grantType = "wechat"; // 微信登录的授权类型
log.info("收到微信登录请求: clientId={}, tenantId={}", clientId, request.getTenantId());
// 1. 检查服务是否可用
if (wxLoginService == null) {
@@ -266,43 +283,33 @@ public class AuthController {
}
// 2. 校验客户端
SysClientVo client = clientService.queryByClientId(request.getClientId());
SysClientVo client = clientService.queryByClientId(clientId);
if (ObjectUtil.isNull(client)) {
log.error("客户端不存在: clientId={}", request.getClientId());
log.error("客户端不存在: clientId={}", clientId);
return R.fail(MessageUtils.message("auth.grant.type.error"));
}
// 校验 client 内是否包含 grantType
if (!StringUtils.contains(client.getGrantType(), grantType)) {
log.error("客户端不支持该授权类型: clientId={}, grantType={}", clientId, grantType);
return R.fail(MessageUtils.message("auth.grant.type.error"));
}
if (!SystemConstants.NORMAL.equals(client.getStatus())) {
log.error("客户端已被禁用: clientId={}, status={}", request.getClientId(), client.getStatus());
log.error("客户端已被禁用: clientId={}, status={}", clientId, client.getStatus());
return R.fail(MessageUtils.message("auth.grant.type.blocked"));
}
// 3. 校验租户
if (StringUtils.isNotBlank(request.getTenantId())) {
try {
loginService.checkTenant(request.getTenantId());
} catch (Exception e) {
log.error("租户校验失败: tenantId={}", request.getTenantId(), e);
return R.fail("租户校验失败: " + e.getMessage());
}
loginService.checkTenant(request.getTenantId());
}
// 4. 执行微信登录
AquUser aquUser = wxLoginService.loginByWeChat(
request.getCode(),
request.getJsCode(),
request.getTenantId()
);
// 4. 使用标准的 IAuthStrategy 登录方法
LoginVo loginVo = IAuthStrategy.login(body, client, grantType);
log.info("微信登录成功: userId={}, accessToken={}",
loginVo.getUserId(), loginVo.getAccessToken() != null ? "***" : "null");
if (aquUser == null) {
log.error("微信登录失败,未返回用户信息");
return R.fail("登录失败,请重试");
}
// 5. 返回用户信息
log.info("微信登录成功: userId={}, mobilePhone={}",
aquUser.getId(), aquUser.getMobilePhone());
return R.ok(aquUser);
return R.ok(loginVo);
} catch (ServiceException e) {
log.error("微信登录业务异常: {}", e.getMessage(), e);