Commit be88ece7 authored by 李健华's avatar 李健华

微信公众号优化需求

parent 738586df
...@@ -77,6 +77,7 @@ public interface Constants { ...@@ -77,6 +77,7 @@ public interface Constants {
String PROTOCOL_HEAD_HTTPS = "https:"; String PROTOCOL_HEAD_HTTPS = "https:";
String PROTOCOL_HEAD_HTTP = "http:"; String PROTOCOL_HEAD_HTTP = "http:";
String REDIRECT = "redirect"; String REDIRECT = "redirect";
String CUSTOMREDIRECT = "customredirect";
String LOCAL = "local"; String LOCAL = "local";
String REDIRECT_ORDER = "orders"; String REDIRECT_ORDER = "orders";
String DOCKER_DOMAIN = "liangkebang"; String DOCKER_DOMAIN = "liangkebang";
......
...@@ -178,13 +178,14 @@ public class WeChatController implements IBaseController { ...@@ -178,13 +178,14 @@ public class WeChatController implements IBaseController {
Long registerFrom = null; Long registerFrom = null;
String redirect = null; String redirect = null;
String schema = null; String schema = null;
String customRedirect = null;
try { try {
extData = new String(Base64.decodeBase64(extData), "UTF-8"); extData = new String(Base64.decodeBase64(extData), "UTF-8");
} catch (Exception ex) { } catch (Exception ex) {
extData = ""; extData = "";
} }
if (StringUtils.isBlank(extData)) { if (StringUtils.isBlank(extData)) {
receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, response); receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, customRedirect, response);
return; return;
} }
log.info("从微信extdata版本接口进入:{}, extData:{}", schema, extData); log.info("从微信extdata版本接口进入:{}, extData:{}", schema, extData);
...@@ -194,15 +195,16 @@ public class WeChatController implements IBaseController { ...@@ -194,15 +195,16 @@ public class WeChatController implements IBaseController {
}); });
} catch (Exception ex) { } catch (Exception ex) {
log.error("解析extData发生错误", ex); log.error("解析extData发生错误", ex);
receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, response); receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, customRedirect, response);
return; return;
} }
schema = extDataObj.getOrDefault("protocol", Constants.PROTOCOL_HEAD_HTTP).toString(); schema = extDataObj.getOrDefault("protocol", Constants.PROTOCOL_HEAD_HTTP).toString();
log.info("从微信登录extData中获得协议信息,protocol:{}", schema); log.info("从微信登录extData中获得协议信息,protocol:{}", schema);
registerFrom = Long.valueOf(extDataObj.getOrDefault("registerFrom", "1").toString()); registerFrom = Long.valueOf(extDataObj.getOrDefault("registerFrom", "1").toString());
redirect = (String) extDataObj.getOrDefault(Constants.REDIRECT, Constants.REDIRECT); redirect = (String) extDataObj.getOrDefault(Constants.REDIRECT, Constants.REDIRECT);
customRedirect = (String) extDataObj.getOrDefault(Constants.CUSTOMREDIRECT, null);
log.info("从微信登录,registerFrom:{}, redirect:{}", registerFrom, redirect); log.info("从微信登录,registerFrom:{}, redirect:{}", registerFrom, redirect);
receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, response); receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, customRedirect, response);
} }
/** /**
...@@ -215,7 +217,7 @@ public class WeChatController implements IBaseController { ...@@ -215,7 +217,7 @@ public class WeChatController implements IBaseController {
* @param redirect * @param redirect
* @param response * @param response
*/ */
private void receiveCodeWithDefault(String code, String systemKey, String schema, Long registerFrom, String redirect, HttpServletResponse response) { private void receiveCodeWithDefault(String code, String systemKey, String schema, Long registerFrom, String redirect, String customRedirect, HttpServletResponse response) {
// 微信跳转请求入参监控 // 微信跳转请求入参监控
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
log.info("[WeChatController][receiveCodeWithDefault]微信授权及跳转:code:{},systemKey:{},schema:{},registerFrom:{},redirect:{},request:{}", code, systemKey, schema, registerFrom, redirect, JSON.toJSONString(getRequestHeaderMap(request))); log.info("[WeChatController][receiveCodeWithDefault]微信授权及跳转:code:{},systemKey:{},schema:{},registerFrom:{},redirect:{},request:{}", code, systemKey, schema, registerFrom, redirect, JSON.toJSONString(getRequestHeaderMap(request)));
...@@ -247,6 +249,7 @@ public class WeChatController implements IBaseController { ...@@ -247,6 +249,7 @@ public class WeChatController implements IBaseController {
log.info("[WeChatController][receiveCodeWithDefault]微信授权及跳转:WechatUserInfo - from wechat api:{}", userInfo); log.info("[WeChatController][receiveCodeWithDefault]微信授权及跳转:WechatUserInfo - from wechat api:{}", userInfo);
if (Objects.isNull(userInfo) || StringUtils.isBlank(userInfo.getOpenId())) { if (Objects.isNull(userInfo) || StringUtils.isBlank(userInfo.getOpenId())) {
// 让用户登录,不关联微信, 构造不关联微信的url // 让用户登录,不关联微信, 构造不关联微信的url
// 如果有自定义跳转链接
redirectNormalUrl(response, merchant, registerFrom, domain); redirectNormalUrl(response, merchant, registerFrom, domain);
return; return;
} }
...@@ -257,19 +260,31 @@ public class WeChatController implements IBaseController { ...@@ -257,19 +260,31 @@ public class WeChatController implements IBaseController {
userInfo.setNickName(EmojiUtil.BYTE_4_REPLACE_TEMPLATE); userInfo.setNickName(EmojiUtil.BYTE_4_REPLACE_TEMPLATE);
userInfo = wechatService.saveWechatUserInfo(userInfo); userInfo = wechatService.saveWechatUserInfo(userInfo);
} }
if (customRedirect != null) {
customRedirectWechatLoginUrlWithoutLogin(response, merchant, userInfo, registerFrom, customRedirect);
} else {
redirectWechatLoginUrlWithoutLogin(response, merchant, userInfo, registerFrom, domain); redirectWechatLoginUrlWithoutLogin(response, merchant, userInfo, registerFrom, domain);
}
return; return;
} }
if (Objects.isNull(userInfoInDb.getUserId())) { if (Objects.isNull(userInfoInDb.getUserId())) {
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面 // 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
if (customRedirect != null) {
customRedirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, customRedirect);
} else {
redirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, domain); redirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, domain);
}
return; return;
} }
User user = userService.findById(userInfoInDb.getUserId()); User user = userService.findById(userInfoInDb.getUserId());
// 用户不存在或已注销 // 用户不存在或已注销
if (Objects.isNull(user) || !user.getEnable()) { if (Objects.isNull(user) || !user.getEnable()) {
// 用户已经微信登录了,但是关联信用钱包用户信息无效,跳转到注册页面 // 用户已经微信登录了,但是关联信用钱包用户信息无效,跳转到注册页面
if (customRedirect != null) {
customRedirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, customRedirect);
} else {
redirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, domain); redirectWechatLoginUrlWithoutLogin(response, merchant, userInfoInDb, registerFrom, domain);
}
return; return;
} }
// 已经关联了用户 // 已经关联了用户
...@@ -329,7 +344,7 @@ public class WeChatController implements IBaseController { ...@@ -329,7 +344,7 @@ public class WeChatController implements IBaseController {
if (Objects.equals(Constants.PROTOCOL_HEAD_HTTPS, request.getScheme())) { if (Objects.equals(Constants.PROTOCOL_HEAD_HTTPS, request.getScheme())) {
schema = Constants.PROTOCOL_HEAD_HTTPS; schema = Constants.PROTOCOL_HEAD_HTTPS;
} }
receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, response); receiveCodeWithDefault(code, systemKey, schema, registerFrom, redirect, redirect, response);
} }
private String createUserSession(User user, Merchant merchant, String redirect, String domain, Long registerFrom) { private String createUserSession(User user, Merchant merchant, String redirect, String domain, Long registerFrom) {
...@@ -382,6 +397,13 @@ public class WeChatController implements IBaseController { ...@@ -382,6 +397,13 @@ public class WeChatController implements IBaseController {
response.setStatus(HttpStatus.SC_MOVED_PERMANENTLY); response.setStatus(HttpStatus.SC_MOVED_PERMANENTLY);
} }
private void customRedirectWechatLoginUrlWithoutLogin(HttpServletResponse response, Merchant merchant, WechatUserInfo userInfo, Long registerFrom, String customRedirect) {
String redirectUrl = customRedirect + "?registerFrom= " + registerFrom + "&channelId=1&isWechat=true&openId=" + userInfo.getOpenId();
log.info("[WeChatController][redirectWechatLoginUrlWithoutLogin]微信授权及自定义跳转:redirectUrl:[{}]", redirectUrl);
response.setHeader("Location", redirectUrl);
response.setStatus(HttpStatus.SC_MOVED_PERMANENTLY);
}
private String assembleNormalRedirectUrl(Merchant merchant, Long registerFrom, String domain) { private String assembleNormalRedirectUrl(Merchant merchant, Long registerFrom, String domain) {
if (Constants.MERCHANT_BAITIAO.equals(merchant.getName())) { if (Constants.MERCHANT_BAITIAO.equals(merchant.getName())) {
if (registerFrom == Constants.Channel.HENGCHANG || registerFrom == Constants.Channel.WECHAT) { if (registerFrom == Constants.Channel.HENGCHANG || registerFrom == Constants.Channel.WECHAT) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment