微信小程序登录修改

This commit is contained in:
2026-01-09 12:17:21 +08:00
parent f948362d1b
commit 8073a95f74
23 changed files with 1417 additions and 58 deletions

View File

@@ -25,6 +25,10 @@
<groupId>org.xyzh.common</groupId>
<artifactId>common-all</artifactId>
</dependency>
<dependency>
<groupId>org.xyzh.common</groupId>
<artifactId>common-wechat</artifactId>
</dependency>
<dependency>
<groupId>org.xyzh.apis</groupId>
<artifactId>api-system</artifactId>

View File

@@ -8,6 +8,8 @@ import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson2.JSON;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -34,6 +36,9 @@ import org.xyzh.common.dto.sys.TbSysUserRoleDTO;
import org.xyzh.common.dto.sys.TbSysViewDTO;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.common.utils.validation.ValidationUtils;
import org.xyzh.common.wechat.pojo.WeChatPhoneResult;
import org.xyzh.common.wechat.pojo.WeChatSessionResult;
import org.xyzh.common.wechat.service.WeChatMiniProgramService;
import org.xyzh.common.auth.utils.JwtTokenUtil;
import org.xyzh.common.redis.service.RedisService;
@@ -54,6 +59,8 @@ import jakarta.validation.constraints.NotNull;
@RequestMapping("/system/guest")
public class GuestController {
private static final Logger logger = LoggerFactory.getLogger(GuestController.class);
@Autowired
private GuestService guestService;
@@ -69,6 +76,9 @@ public class GuestController {
@Autowired
private RedisService redisService;
@Autowired
private WeChatMiniProgramService weChatMiniProgramService;
@PostMapping
public ResultDomain<TbGuestDTO> createGuest(TbGuestDTO guest) {
@@ -120,6 +130,63 @@ public class GuestController {
@Operation(summary = "微信小程序用户识别登录")
@PostMapping("/identify")
public ResultDomain<LoginDomain> identifyUser(@RequestBody LoginParam loginParam, HttpServletRequest request) {
logger.info("微信小程序登录请求: wechatId={}, code={}, phoneCode={}",
loginParam.getWechatId(),
loginParam.getCode() != null ? loginParam.getCode().substring(0, Math.min(10, loginParam.getCode().length())) + "..." : null,
loginParam.getPhoneCode() != null ? "" : "");
// 1. 处理微信登录code获取openid
String openid = null;
String sessionKey = null;
if (loginParam.getCode() != null && !loginParam.getCode().trim().isEmpty()) {
ResultDomain<WeChatSessionResult> sessionResult = weChatMiniProgramService.code2Session(loginParam.getCode());
if (sessionResult.getSuccess() && sessionResult.getData() != null) {
openid = sessionResult.getData().getOpenid();
sessionKey = sessionResult.getData().getSessionKey();
logger.info("获取openid成功: {}", openid);
// 使用openid作为wechatId
loginParam.setWechatId(openid);
} else {
logger.warn("获取openid失败: {}", sessionResult.getMessage());
}
}
// 2. 处理手机号授权
String phoneNumber = null;
// 方式1使用phoneCode获取手机号新版API推荐
if (loginParam.getPhoneCode() != null && !loginParam.getPhoneCode().trim().isEmpty()) {
ResultDomain<WeChatPhoneResult> phoneResult = weChatMiniProgramService.getPhoneNumber(loginParam.getPhoneCode());
if (phoneResult.getSuccess() && phoneResult.getData() != null && phoneResult.getData().getPhoneInfo() != null) {
phoneNumber = phoneResult.getData().getPhoneInfo().getPurePhoneNumber();
if (phoneNumber == null) {
phoneNumber = phoneResult.getData().getPhoneInfo().getPhoneNumber();
}
logger.info("通过phoneCode获取手机号成功: {}", phoneNumber);
} else {
logger.warn("通过phoneCode获取手机号失败: {}", phoneResult.getMessage());
}
}
// 方式2使用encryptedData和iv解密手机号旧版API
if (phoneNumber == null && sessionKey != null
&& loginParam.getEncryptedData() != null && !loginParam.getEncryptedData().trim().isEmpty()
&& loginParam.getIv() != null && !loginParam.getIv().trim().isEmpty()) {
ResultDomain<String> decryptResult = weChatMiniProgramService.decryptPhoneNumber(
sessionKey, loginParam.getEncryptedData(), loginParam.getIv());
if (decryptResult.getSuccess() && decryptResult.getData() != null) {
phoneNumber = decryptResult.getData();
logger.info("通过解密获取手机号成功: {}", phoneNumber);
} else {
logger.warn("解密手机号失败: {}", decryptResult.getMessage());
}
}
// 设置手机号
if (phoneNumber != null) {
loginParam.setPhone(phoneNumber);
}
// 验证参数必须有wechatId或phone
if ((loginParam.getWechatId() == null || loginParam.getWechatId().trim().isEmpty())
&& (loginParam.getPhone() == null || loginParam.getPhone().trim().isEmpty())) {
@@ -132,14 +199,15 @@ public class GuestController {
// 从 request 中提取客户端 IP 并设置到 loginParam
loginParam.setClientIp(getClientIP(request));
// 1. 尝试通过AuthService登录员工
// 3. 尝试通过AuthService登录员工
ResultDomain<LoginDomain> loginResult = authService.login(loginParam);
if (loginResult.getSuccess() && loginResult.getData() != null) {
// 登录成功,是系统员工
logger.info("员工登录成功: userId={}", loginResult.getData().getUser().getUserId());
return loginResult;
}
// 2. 登录失败,查询/注册来客
// 4. 登录失败,查询/注册来客
return handleGuestLogin(loginParam);
}