|
@@ -16,6 +16,7 @@ import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
|
|
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
|
|
+import com.fs.core.config.WxMpProperties;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -28,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import javax.validation.Valid;
|
|
import javax.validation.Valid;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
@@ -40,6 +42,9 @@ public class WxH5MpController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private WxMpService wxMpService;
|
|
private WxMpService wxMpService;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WxMpProperties wxMpProperties;
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private IFsUserService userService;
|
|
private IFsUserService userService;
|
|
|
|
|
|
|
@@ -58,8 +63,8 @@ public class WxH5MpController {
|
|
|
@PostMapping("/loginByMp")
|
|
@PostMapping("/loginByMp")
|
|
|
public R loginByMp(@Valid @RequestBody FsUserLoginByMpParam param) {
|
|
public R loginByMp(@Valid @RequestBody FsUserLoginByMpParam param) {
|
|
|
try {
|
|
try {
|
|
|
- //获取微信用户信息
|
|
|
|
|
- WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
|
|
|
|
|
|
|
+ //获取微信用户信息(支持多公众号,自动遍历匹配)
|
|
|
|
|
+ WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
|
|
|
WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
|
|
WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
|
|
|
// FsUser user = userService.selectFsUserByUnionid(wxMpUser.getUnionId());
|
|
// FsUser user = userService.selectFsUserByUnionid(wxMpUser.getUnionId());
|
|
|
FsUser user;
|
|
FsUser user;
|
|
@@ -110,8 +115,8 @@ public class WxH5MpController {
|
|
|
@PostMapping("/userInfo")
|
|
@PostMapping("/userInfo")
|
|
|
public R mpGetUserInfo(@Valid @RequestBody FsUserLoginByMpParam param) {
|
|
public R mpGetUserInfo(@Valid @RequestBody FsUserLoginByMpParam param) {
|
|
|
try {
|
|
try {
|
|
|
- //获取微信用户信息
|
|
|
|
|
- WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
|
|
|
|
|
|
|
+ //获取微信用户信息(支持多公众号,自动遍历匹配)
|
|
|
|
|
+ WxOAuth2AccessToken wxMpOAuth2AccessToken = getAccessToken(param.getCode(), param.getAppId());
|
|
|
WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
|
|
WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
|
|
|
String nickname = wxMpUser.getNickname();
|
|
String nickname = wxMpUser.getNickname();
|
|
|
String headImgUrl = wxMpUser.getHeadImgUrl();
|
|
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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|