2 Commits f9fb2f24e0 ... 146d26646d

Autor SHA1 Mensaje Fecha
  yys 146d26646d Merge remote-tracking branch 'origin/Payment-Configuration' into Payment-Configuration hace 1 semana
  yys c2bfefd1f4 1、合并业务域名授权 hace 1 semana

+ 53 - 4
fs-company-app/src/main/java/com/fs/app/controller/WxH5MpController.java

@@ -16,6 +16,7 @@ import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
 import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.validation.Valid;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
@@ -40,6 +42,9 @@ public class WxH5MpController {
     @Autowired
     private WxMpService wxMpService;
 
+    @Autowired
+    private WxMpProperties wxMpProperties;
+
     @Autowired
     private IFsUserService userService;
 
@@ -58,8 +63,8 @@ public class WxH5MpController {
     @PostMapping("/loginByMp")
     public R loginByMp(@Valid @RequestBody FsUserLoginByMpParam param) {
         try {
-            //获取微信用户信息
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            //获取微信用户信息(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
 //            FsUser user = userService.selectFsUserByUnionid(wxMpUser.getUnionId());
             FsUser user;
@@ -110,8 +115,8 @@ public class WxH5MpController {
     @PostMapping("/userInfo")
     public R mpGetUserInfo(@Valid @RequestBody FsUserLoginByMpParam param) {
         try {
-            //获取微信用户信息
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            //获取微信用户信息(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
             String nickname = wxMpUser.getNickname();
             String headImgUrl = wxMpUser.getHeadImgUrl();
@@ -131,4 +136,48 @@ public class WxH5MpController {
     }
 
 
+    /**
+     * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+     * 1. 如果传了appId,直接切换到指定公众号配置
+     * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+     */
+    private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+        // 传了appId,直接使用指定配置
+        if (StringUtils.isNotBlank(appId)) {
+            if (!wxMpService.switchover(appId)) {
+                throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+            }
+            log.info("使用指定appId切换公众号配置, appId={}", appId);
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 未传appId,遍历所有公众号配置逐个尝试
+        List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+        if (configs == null || configs.isEmpty()) {
+            throw new WxErrorException("未配置任何公众号信息");
+        }
+
+        // 只有一个配置时直接使用
+        if (configs.size() == 1) {
+            wxMpService.switchover(configs.get(0).getAppId());
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 多个配置时,逐个尝试
+        WxErrorException lastError = null;
+        for (WxMpProperties.MpConfig config : configs) {
+            try {
+                wxMpService.switchover(config.getAppId());
+                WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+                log.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+                return accessToken;
+            } catch (WxErrorException e) {
+                log.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+                lastError = e;
+            }
+        }
+        // 所有配置都失败,抛出最后一次错误
+        throw lastError;
+    }
+
 }

+ 4 - 0
fs-service/src/main/resources/application-config-zkzh.yml

@@ -94,6 +94,10 @@ wx:
         secret: 473a992e28d5c524b4ff3783559026cb
         token: PPKOdAlCoMO # 接口配置里的Token值
         aesKey: Eswa6VjwtVMCcw03qZy6fWllgrv5aytIA1SZPEU0kU2 # 接口配置里的EncodingAESKey值
+      - appId: wx47bd20b13a39f39c # 第一个公众号的appid  //公众号名称:中康未来健康服务
+        secret: 9a41e786928115d24c9350324ca61263
+        token: PPKOdAlCoMO # 接口配置里的Token值
+        aesKey: Eswa6VjwtVMCcw03qZy6fWllgrv5aytIA1SZPEU0kU2 # 接口配置里的EncodingAESKey值
   open:
     appId: wx40f3de7bd405fb5c
     secret: 4038e9209ddaf5f23dcde35e41be3120

+ 53 - 4
fs-user-app/src/main/java/com/fs/app/controller/WxH5MpController.java

@@ -30,6 +30,7 @@ import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
 import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.checkerframework.checker.units.qual.C;
 import org.slf4j.Logger;
@@ -43,6 +44,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.validation.Valid;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
@@ -56,6 +58,9 @@ public class WxH5MpController {
     @Autowired
     private WxMpService wxMpService;
 
+    @Autowired
+    private WxMpProperties wxMpProperties;
+
     @Autowired
     private IFsUserService userService;
 
@@ -101,8 +106,8 @@ public class WxH5MpController {
         }
 
         try {
-            // 获取微信用户信息
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            // 获取微信用户信息(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
 
             if (StringUtils.isEmpty(wxMpUser.getUnionId())){
@@ -157,8 +162,8 @@ public class WxH5MpController {
         }
 
         try {
-            // 获取微信用户信息
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            // 获取微信用户信息(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), null);
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
 
             if (StringUtils.isEmpty(wxMpUser.getUnionId())){
@@ -342,4 +347,48 @@ public class WxH5MpController {
         fsUserWxService.saveOrUpdateByUniqueKey(fsUserWx);
     }
 
+    /**
+     * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+     * 1. 如果传了appId,直接切换到指定公众号配置
+     * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+     */
+    private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+        // 传了appId,直接使用指定配置
+        if (StringUtils.isNotBlank(appId)) {
+            if (!wxMpService.switchover(appId)) {
+                throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+            }
+            log.info("使用指定appId切换公众号配置, appId={}", appId);
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 未传appId,遍历所有公众号配置逐个尝试
+        List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+        if (configs == null || configs.isEmpty()) {
+            throw new WxErrorException("未配置任何公众号信息");
+        }
+
+        // 只有一个配置时直接使用
+        if (configs.size() == 1) {
+            wxMpService.switchover(configs.get(0).getAppId());
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 多个配置时,逐个尝试
+        WxErrorException lastError = null;
+        for (WxMpProperties.MpConfig config : configs) {
+            try {
+                wxMpService.switchover(config.getAppId());
+                WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+                log.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+                return accessToken;
+            } catch (WxErrorException e) {
+                log.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+                lastError = e;
+            }
+        }
+        // 所有配置都失败,抛出最后一次错误
+        throw lastError;
+    }
+
 }

+ 49 - 1
fs-user-app/src/main/java/com/fs/app/controller/WxUserController.java

@@ -42,6 +42,7 @@ import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.api.WxMpUserService;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -85,6 +86,8 @@ public class WxUserController extends AppBaseController{
     @Autowired
     private WxMpService wxMpService;
     @Autowired
+    private WxMpProperties wxMpProperties;
+    @Autowired
     private IFsUserWxService userWxService;
 
     @Autowired
@@ -334,7 +337,8 @@ public class WxUserController extends AppBaseController{
             return R.error("code不存在");
         }
         try{
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            // 获取微信accessToken(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
             if (StringUtils.isEmpty(wxMpUser.getUnionId())){
                 return R.error("未绑定开放平台");
@@ -543,4 +547,48 @@ public class WxUserController extends AppBaseController{
         fsUserWxService.saveOrUpdateByUniqueKey(fsUserWx);
     }
 
+    /**
+     * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+     * 1. 如果传了appId,直接切换到指定公众号配置
+     * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+     */
+    private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+        // 传了appId,直接使用指定配置
+        if (StringUtils.isNotBlank(appId)) {
+            if (!wxMpService.switchover(appId)) {
+                throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+            }
+            this.logger.info("使用指定appId切换公众号配置, appId={}", appId);
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 未传appId,遍历所有公众号配置逐个尝试
+        List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+        if (configs == null || configs.isEmpty()) {
+            throw new WxErrorException("未配置任何公众号信息");
+        }
+
+        // 只有一个配置时直接使用
+        if (configs.size() == 1) {
+            wxMpService.switchover(configs.get(0).getAppId());
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 多个配置时,逐个尝试
+        WxErrorException lastError = null;
+        for (WxMpProperties.MpConfig config : configs) {
+            try {
+                wxMpService.switchover(config.getAppId());
+                WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+                this.logger.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+                return accessToken;
+            } catch (WxErrorException e) {
+                this.logger.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+                lastError = e;
+            }
+        }
+        // 所有配置都失败,抛出最后一次错误
+        throw lastError;
+    }
+
 }

+ 50 - 1
fs-user-app/src/main/java/com/fs/app/controller/course/CourseMpLoginController.java

@@ -30,6 +30,7 @@ import me.chanjar.weixin.mp.api.WxMpMenuService;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.api.WxMpUserService;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -43,6 +44,7 @@ import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
@@ -55,6 +57,8 @@ public class CourseMpLoginController {
   @Autowired
   private WxMpService wxMpService;
   @Autowired
+  private WxMpProperties wxMpProperties;
+  @Autowired
   private IFsUserService userService;
 
   @Autowired
@@ -78,7 +82,8 @@ public class CourseMpLoginController {
       return R.error("code不存在");
     }
     try{
-      WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+      // 获取微信accessToken(支持多公众号,自动遍历匹配)
+      WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
       WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
       if(StringUtils.isBlank(wxMpUser.getUnionId())){
         return R.error("请使用微信扫码登录");
@@ -126,4 +131,48 @@ public class CourseMpLoginController {
 
   }
 
+  /**
+   * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+   * 1. 如果传了appId,直接切换到指定公众号配置
+   * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+   */
+  private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+      // 传了appId,直接使用指定配置
+      if (StringUtils.isNotBlank(appId)) {
+          if (!wxMpService.switchover(appId)) {
+              throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+          }
+          logger.info("使用指定appId切换公众号配置, appId={}", appId);
+          return wxMpService.getOAuth2Service().getAccessToken(code);
+      }
+
+      // 未传appId,遍历所有公众号配置逐个尝试
+      List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+      if (configs == null || configs.isEmpty()) {
+          throw new WxErrorException("未配置任何公众号信息");
+      }
+
+      // 只有一个配置时直接使用
+      if (configs.size() == 1) {
+          wxMpService.switchover(configs.get(0).getAppId());
+          return wxMpService.getOAuth2Service().getAccessToken(code);
+      }
+
+      // 多个配置时,逐个尝试
+      WxErrorException lastError = null;
+      for (WxMpProperties.MpConfig config : configs) {
+          try {
+              wxMpService.switchover(config.getAppId());
+              WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+              logger.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+              return accessToken;
+          } catch (WxErrorException e) {
+              logger.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+              lastError = e;
+          }
+      }
+      // 所有配置都失败,抛出最后一次错误
+      throw lastError;
+  }
+
 }

+ 51 - 2
fs-user-app/src/main/java/com/fs/app/controller/store/WxH5MpScrmController.java

@@ -24,6 +24,7 @@ import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
 import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,6 +37,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.validation.Valid;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
@@ -49,6 +51,9 @@ public class WxH5MpScrmController {
     @Autowired
     private WxMpService wxMpService;
 
+    @Autowired
+    private WxMpProperties wxMpProperties;
+
     @Autowired
     private IFsUserScrmService userService;
 
@@ -92,8 +97,8 @@ public class WxH5MpScrmController {
         }
 
         try {
-            // 获取微信用户信息
-            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+            // 获取微信用户信息(支持多公众号,自动遍历匹配)
+            WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
             WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
 
 
@@ -163,4 +168,48 @@ public class WxH5MpScrmController {
         }
     }
 
+    /**
+     * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+     * 1. 如果传了appId,直接切换到指定公众号配置
+     * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+     */
+    private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+        // 传了appId,直接使用指定配置
+        if (StringUtils.isNotBlank(appId)) {
+            if (!wxMpService.switchover(appId)) {
+                throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+            }
+            log.info("使用指定appId切换公众号配置, appId={}", appId);
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 未传appId,遍历所有公众号配置逐个尝试
+        List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+        if (configs == null || configs.isEmpty()) {
+            throw new WxErrorException("未配置任何公众号信息");
+        }
+
+        // 只有一个配置时直接使用
+        if (configs.size() == 1) {
+            wxMpService.switchover(configs.get(0).getAppId());
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
+
+        // 多个配置时,逐个尝试
+        WxErrorException lastError = null;
+        for (WxMpProperties.MpConfig config : configs) {
+            try {
+                wxMpService.switchover(config.getAppId());
+                WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+                log.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+                return accessToken;
+            } catch (WxErrorException e) {
+                log.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+                lastError = e;
+            }
+        }
+        // 所有配置都失败,抛出最后一次错误
+        throw lastError;
+    }
+
 }

+ 46 - 4
fs-user-app/src/main/java/com/fs/app/controller/store/WxMpScrmController.java

@@ -21,6 +21,7 @@ import me.chanjar.weixin.mp.api.WxMpMenuService;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.api.WxMpUserService;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
+import com.fs.core.config.WxMpProperties;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -31,6 +32,7 @@ import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
@@ -42,6 +44,8 @@ public class WxMpScrmController {
     Logger logger= LoggerFactory.getLogger(getClass());
     @Autowired
     private WxMpService wxMpService;
+    @Autowired
+    private WxMpProperties wxMpProperties;
     private static final String MP_TOKEN = "U2qmxEbsp0PJFoLRvUDvIjVi9XPzuVc2";
 
     private static final String EncodingAESKey = "P3HE7Gd1PJVQqCLoOMop5uYfjx9LwfY53rnC3VUuLZS";
@@ -97,7 +101,8 @@ public class WxMpScrmController {
         return R.error("code不存在");
       }
       try{
-        WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
+        // 获取微信accessToken(支持多公众号,自动遍历匹配)
+        WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
         WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
         WxMpUserService wxMpUserService = wxMpService.getUserService();
         WxMpUser userInfo = wxMpUserService.userInfo(wxMpUser.getOpenid());
@@ -159,10 +164,47 @@ public class WxMpScrmController {
 
     }
 
+    /**
+     * 获取微信OAuth2 AccessToken,支持多公众号自动匹配
+     * 1. 如果传了appId,直接切换到指定公众号配置
+     * 2. 如果未传appId,遍历所有公众号配置逐个尝试,找到能成功解析code的配置
+     */
+    private WxOAuth2AccessToken getAccessToken(String code, String appId) throws WxErrorException {
+        // 传了appId,直接使用指定配置
+        if (StringUtils.isNotBlank(appId)) {
+            if (!wxMpService.switchover(appId)) {
+                throw new WxErrorException("未找到对应appId=[" + appId + "]的配置,请核实!");
+            }
+            logger.info("使用指定appId切换公众号配置, appId={}", appId);
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
 
+        // 未传appId,遍历所有公众号配置逐个尝试
+        List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
+        if (configs == null || configs.isEmpty()) {
+            throw new WxErrorException("未配置任何公众号信息");
+        }
 
+        // 只有一个配置时直接使用
+        if (configs.size() == 1) {
+            wxMpService.switchover(configs.get(0).getAppId());
+            return wxMpService.getOAuth2Service().getAccessToken(code);
+        }
 
-
-
-
+        // 多个配置时,逐个尝试
+        WxErrorException lastError = null;
+        for (WxMpProperties.MpConfig config : configs) {
+            try {
+                wxMpService.switchover(config.getAppId());
+                WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
+                logger.info("自动匹配公众号配置成功, appId={}", config.getAppId());
+                return accessToken;
+            } catch (WxErrorException e) {
+                logger.debug("使用appId=[{}]获取accessToken失败: {}", config.getAppId(), e.getMessage());
+                lastError = e;
+            }
+        }
+        // 所有配置都失败,抛出最后一次错误
+        throw lastError;
+    }
 }