diff --git a/src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java b/src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java index 1dde8e6..460509e 100644 --- a/src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java +++ b/src/main/java/com/gxwebsoft/common/system/controller/WxLoginController.java @@ -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); diff --git a/src/main/java/com/gxwebsoft/common/system/param/UserParam.java b/src/main/java/com/gxwebsoft/common/system/param/UserParam.java index e912b90..a12d746 100644 --- a/src/main/java/com/gxwebsoft/common/system/param/UserParam.java +++ b/src/main/java/com/gxwebsoft/common/system/param/UserParam.java @@ -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; }