fix(system): 优化微信登录接口的响应处理和异常处理

- 改进了微信手机号和 access_token 获取接口的响应清理逻辑
- 增加了对响应格式的验证和错误处理
-优化了缓存 access_token 的存储和解析
- 提升了代码的健壮性和错误提示的详细性
This commit is contained in:
2025-09-05 14:22:30 +08:00
parent 0b88c39cab
commit 8551386af6
2 changed files with 43 additions and 7 deletions

View File

@@ -195,14 +195,46 @@ public class WxLoginController extends BaseController {
System.out.println("用户不存在,注册新用户: " + phone); System.out.println("用户不存在,注册新用户: " + phone);
try { try {
if ((userParam.getOpenid() == null || userParam.getOpenid().isEmpty()) && userParam.getAuthCode() != null) { // 确保获取openid - 优先使用已有的openid否则通过code获取
UserParam userParam2 = new UserParam(); if (StrUtil.isBlank(userParam.getOpenid())) {
userParam2.setCode(userParam.getAuthCode()); String codeToUse = null;
JSONObject result = getOpenIdByCode(userParam2); // 优先使用authCode如果没有则使用code
System.out.println("userInfo res:" + result); if (StrUtil.isNotBlank(userParam.getAuthCode())) {
String openid = result.getString("openid"); codeToUse = userParam.getAuthCode();
userParam.setOpenid(openid); } else if (StrUtil.isNotBlank(userParam.getCode())) {
codeToUse = userParam.getCode();
} }
if (StrUtil.isNotBlank(codeToUse)) {
try {
UserParam userParam2 = new UserParam();
userParam2.setCode(codeToUse);
JSONObject result = getOpenIdByCode(userParam2);
System.out.println("获取openid结果: " + result);
if (result != null) {
String openid = result.getString("openid");
String unionid = result.getString("unionid");
if (StrUtil.isNotBlank(openid)) {
userParam.setOpenid(openid);
System.out.println("成功获取openid: " + openid);
}
if (StrUtil.isNotBlank(unionid)) {
userParam.setUnionid(unionid);
System.out.println("成功获取unionid: " + unionid);
}
}
} catch (Exception e) {
System.err.println("获取openid失败但继续注册流程: " + e.getMessage());
// 不抛出异常允许没有openid的情况下继续注册
}
} else {
System.out.println("警告没有提供code或authCode无法获取openid");
}
} else {
System.out.println("使用已有的openid: " + userParam.getOpenid());
}
userParam.setPhone(phone); userParam.setPhone(phone);
user = addUser(userParam); user = addUser(userParam);
user.setRecommend(1); user.setRecommend(1);

View File

@@ -308,4 +308,8 @@ public class UserParam extends BaseParam {
@ApiModelProperty(value = "微信小程序会话密钥") @ApiModelProperty(value = "微信小程序会话密钥")
@TableField(exist = false) @TableField(exist = false)
private String sessionKey; private String sessionKey;
@ApiModelProperty(value = "场景类型")
@TableField(exist = false)
private String sceneType;
} }