From c93a86960454d18c0e4cb61b65df651849fe50e9 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期五, 22 三月 2024 09:35:53 +0800
Subject: [PATCH] 优化业主端逻辑
---
service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java | 72 ++++++++++++++++++++++++++++++++----
1 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java b/service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java
index 63997f7..d3633a9 100644
--- a/service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java
+++ b/service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java
@@ -5,18 +5,31 @@
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
+import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
+import com.java110.dto.user.UserAttrDto;
+import com.java110.dto.wechat.SmallWeChatDto;
+import com.java110.intf.store.ISmallWechatV1InnerServiceSMO;
+import com.java110.intf.user.IUserAttrV1InnerServiceSMO;
+import com.java110.po.user.UserAttrPo;
import com.java110.utils.cache.MappingCache;
+import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
+import com.java110.utils.util.StringUtil;
+import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.text.ParseException;
+import java.util.List;
/**
+ * 杩欎釜鎺ュ彛娌℃湁鍐欏ソ 缁欏晢鍩庝笓鐢紝
+ * 閲嶆柊鍐欎竴涓紝杩欎釜鎺ュ彛鏌ヨ瀹屽悗杩樿 鍒峰叆鐢ㄦ埛涓�
+ * 娌℃湁鑰冭檻 涓嶇櫥褰曟儏鍐典笅 鐨刼penId 鑾峰彇
* 鏍规嵁灏忕▼搴廲ode 鑾峰彇openId
*/
@@ -25,6 +38,12 @@
private final static Logger logger = LoggerFactory.getLogger(GetOpenIdByCodeCmd.class);
@Autowired
private RestTemplate outRestTemplate;
+
+ @Autowired
+ private ISmallWechatV1InnerServiceSMO smallWechatV1InnerServiceSMOImpl;
+
+ @Autowired
+ private IUserAttrV1InnerServiceSMO userAttrV1InnerServiceSMOImpl;
@Override
public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
@@ -35,8 +54,24 @@
@Override
public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
- if("MALL".equals(reqJson.getString("appId"))){
- MappingCache.getValue()
+ String userId = context.getReqHeaders().get("user-id");
+
+ String appId = "";
+ String appSecret = "";
+ if ("MALL".equals(reqJson.getString("appId"))) {
+ appId = MappingCache.getValue(MappingConstant.MALL_WECHAT_DOMAIN, "wechatAppId");
+ appSecret = MappingCache.getValue(MappingConstant.MALL_WECHAT_DOMAIN, "wechatAppSecret");
+ } else {
+ SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
+ smallWeChatDto.setAppId(reqJson.getString("appId"));
+ List<SmallWeChatDto> smallWeChatDtos = smallWechatV1InnerServiceSMOImpl.querySmallWechats(smallWeChatDto);
+
+ if (smallWeChatDtos == null || smallWeChatDtos.size() < 1) {
+ throw new IllegalArgumentException("鏈厤缃皬绋嬪簭淇℃伅");
+ }
+
+ appId = smallWeChatDtos.get(0).getAppId();
+ appSecret = smallWeChatDtos.get(0).getAppSecret();
}
@@ -45,12 +80,10 @@
String urlString = "https://api.weixin.qq.com/sns/jscode2session?appid={appId}&secret={secret}&js_code={code}&grant_type={grantType}";
String response = outRestTemplate.getForObject(
urlString, String.class,
- smallWeChatDto.getAppId(),
- smallWeChatDto.getAppSecret(),
+ appId,
+ appSecret,
code,
- wechatAuthProperties.getGrantType());
-
- logger.debug("wechatAuthProperties:" + JSONObject.toJSONString(wechatAuthProperties));
+ "authorization_code");
logger.debug("寰俊杩斿洖鎶ユ枃锛�" + response);
@@ -62,7 +95,30 @@
}
String openId = responseObj.getString("openid");
- String sessionKey = responseObj.getString("session_key");
+
+ if (StringUtil.isEmpty(userId) || userId.startsWith("-")) {
+ context.setResponseEntity(ResultVo.createResponseEntity(openId));
+ return;
+ }
+
+ UserAttrDto userAttrDto = new UserAttrDto();
+ userAttrDto.setUserId(userId);
+ userAttrDto.setSpecCd(UserAttrDto.SPEC_MALL_OPEN_ID);
+ List<UserAttrDto> userAttrDtos = userAttrV1InnerServiceSMOImpl.queryUserAttrs(userAttrDto);
+ if (userAttrDtos == null || userAttrDtos.size() < 1) {
+ UserAttrPo userAttrPo = new UserAttrPo();
+ userAttrPo.setAttrId(GenerateCodeFactory.getAttrId());
+ userAttrPo.setUserId(userId);
+ userAttrPo.setSpecCd(UserAttrDto.SPEC_MALL_OPEN_ID);
+ userAttrPo.setValue(openId);
+ userAttrV1InnerServiceSMOImpl.saveUserAttr(userAttrPo);
+ } else {
+ UserAttrPo userAttrPo = new UserAttrPo();
+ userAttrPo.setAttrId(userAttrDtos.get(0).getAttrId());
+ userAttrPo.setValue(openId);
+ userAttrV1InnerServiceSMOImpl.updateUserAttr(userAttrPo);
+ }
+ context.setResponseEntity(ResultVo.createResponseEntity(openId));
}
}
--
Gitblit v1.8.0