fix(system): 优化微信登录接口的响应处理和异常处理
- 改进了微信手机号和 access_token 获取接口的响应清理逻辑 - 增加了对响应格式的验证和错误处理 -优化了缓存 access_token 的存储和解析 - 提升了代码的健壮性和错误提示的详细性
This commit is contained in:
@@ -195,14 +195,46 @@ public class WxLoginController extends BaseController {
|
||||
System.out.println("用户不存在,注册新用户: " + phone);
|
||||
|
||||
try {
|
||||
if ((userParam.getOpenid() == null || userParam.getOpenid().isEmpty()) && userParam.getAuthCode() != null) {
|
||||
UserParam userParam2 = new UserParam();
|
||||
userParam2.setCode(userParam.getAuthCode());
|
||||
JSONObject result = getOpenIdByCode(userParam2);
|
||||
System.out.println("userInfo res:" + result);
|
||||
String openid = result.getString("openid");
|
||||
userParam.setOpenid(openid);
|
||||
// 确保获取openid - 优先使用已有的openid,否则通过code获取
|
||||
if (StrUtil.isBlank(userParam.getOpenid())) {
|
||||
String codeToUse = null;
|
||||
// 优先使用authCode,如果没有则使用code
|
||||
if (StrUtil.isNotBlank(userParam.getAuthCode())) {
|
||||
codeToUse = userParam.getAuthCode();
|
||||
} 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);
|
||||
user = addUser(userParam);
|
||||
user.setRecommend(1);
|
||||
|
||||
@@ -308,4 +308,8 @@ public class UserParam extends BaseParam {
|
||||
@ApiModelProperty(value = "微信小程序会话密钥")
|
||||
@TableField(exist = false)
|
||||
private String sessionKey;
|
||||
|
||||
@ApiModelProperty(value = "场景类型")
|
||||
@TableField(exist = false)
|
||||
private String sceneType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user