From ded40ebbd1613490fc68190dbd92db5d7243267f Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期二, 22 八月 2023 16:25:20 +0800
Subject: [PATCH] 优化代码

---
 service-api/src/main/java/com/java110/api/smo/login/impl/OwnerAppLoginSMOImpl.java |  170 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 158 insertions(+), 12 deletions(-)

diff --git a/service-api/src/main/java/com/java110/api/smo/login/impl/OwnerAppLoginSMOImpl.java b/service-api/src/main/java/com/java110/api/smo/login/impl/OwnerAppLoginSMOImpl.java
index e68cecc..874b0d8 100644
--- a/service-api/src/main/java/com/java110/api/smo/login/impl/OwnerAppLoginSMOImpl.java
+++ b/service-api/src/main/java/com/java110/api/smo/login/impl/OwnerAppLoginSMOImpl.java
@@ -2,18 +2,24 @@
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.java110.core.base.smo.front.AbstractFrontServiceSMO;
+import com.java110.api.properties.WechatAuthProperties;
+import com.java110.api.smo.DefaultAbstractComponentSMO;
+import com.java110.api.smo.login.IOwnerAppLoginSMO;
 import com.java110.core.context.IPageData;
 import com.java110.core.context.PageData;
 import com.java110.core.factory.AuthenticationFactory;
 import com.java110.core.factory.WechatFactory;
+import com.java110.core.log.LoggerFactory;
 import com.java110.dto.owner.OwnerAppUserDto;
-import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.dto.owner.OwnerCarOpenUserDto;
+import com.java110.dto.wechat.SmallWeChatDto;
 import com.java110.dto.user.UserDto;
-import com.java110.api.properties.WechatAuthProperties;
-import com.java110.api.smo.login.IOwnerAppLoginSMO;
+import com.java110.intf.user.IOwnerCarOpenUserV1InnerServiceSMO;
 import com.java110.utils.cache.CommonCache;
-import com.java110.utils.constant.*;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ResponseConstant;
+import com.java110.utils.constant.ServiceCodeConstant;
+import com.java110.utils.constant.WechatConstant;
 import com.java110.utils.exception.SMOException;
 import com.java110.utils.util.Assert;
 import com.java110.utils.util.Base64Convert;
@@ -21,7 +27,6 @@
 import com.java110.utils.util.StringUtil;
 import com.java110.vo.ResultVo;
 import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
@@ -42,13 +47,14 @@
  * wx鐧诲綍
  */
 @Service("ownerAppLoginSMOImpl")
-public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOwnerAppLoginSMO {
+public class OwnerAppLoginSMOImpl extends DefaultAbstractComponentSMO implements IOwnerAppLoginSMO {
 
     private final static Logger logger = LoggerFactory.getLogger(OwnerAppLoginSMOImpl.class);
 
     private final static int expireTime = 7200;
 
     private final static int LOGIN_PAGE = 1;
+    private final static int COMMON_PAGE = 2;
 
     @Autowired
     private RestTemplate restTemplate;
@@ -58,6 +64,9 @@
 
     @Autowired
     private WechatAuthProperties wechatAuthProperties;
+
+    @Autowired
+    private IOwnerCarOpenUserV1InnerServiceSMO ownerCarOpenUserV1InnerServiceSMOImpl;
 
     @Override
     public ResponseEntity<String> doLogin(IPageData pd) throws SMOException {
@@ -206,13 +215,12 @@
         logger.debug("璋冪敤寰俊鎹㈠幓openId " + paramOut);
         if (paramOut.getStatusCode() != HttpStatus.OK) {
             return ResultVo.redirectPage(errorUrl);
-
         }
-
         JSONObject paramObj = JSONObject.parseObject(paramOut.getBody());
 
         //鑾峰彇 openId
         String openId = paramObj.getString("openid");
+
         String userinfo_url = WechatConstant.APP_GET_USER_INFO_URL
                 .replace("ACCESS_TOKEN", paramObj.getString("access_token"))
                 .replace("OPENID", openId);
@@ -227,6 +235,11 @@
         //澶勭悊鏄电О鏈夌壒娈婄鍙峰鑷� 鍏ュ簱澶辫触闂
         userinfo_paramObj.put("nickname", Base64Convert.byteToBase64(userinfo_paramObj.getString("nickname").getBytes()));
 
+        //鍏紬鍙锋湭缁戝畾 寮�鏀惧钩鍙�
+        if (StringUtil.isEmpty(userinfo_paramObj.getString("unionid"))) {
+            userinfo_paramObj.put("unionid", "-1");
+        }
+
         int loginFlag = paramIn.getInteger("loginFlag");
 
         //璇存槑鏄櫥褰曢〉闈紝涓嬪彂code 灏卞彲浠ワ紝涓嶉渶瑕佷笅鍙慿ey 涔嬬被
@@ -236,12 +249,26 @@
             CommonCache.setValue(code, openId, expireTime);
             CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
             CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
+            CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime);
             if (errorUrl.indexOf("?") > 0) {
                 errorUrl += ("&code=" + code);
             } else {
                 errorUrl += ("?code=" + code);
             }
+            logger.debug("鐧诲綍璺宠浆url:{}", errorUrl);
+
             return ResultVo.redirectPage(errorUrl);
+        }
+
+        if (loginFlag == COMMON_PAGE) {
+            //灏唎penId鏀惧埌redis 缂撳瓨锛岀粰鍓嶆涓嬪彂涓存椂绁ㄦ嵁
+            if (errorUrl.indexOf("?") > 0) {
+                redirectUrl += ("&openId=" + openId);
+            } else {
+                redirectUrl += ("?openId=" + openId);
+            }
+            logger.debug("璺宠浆url:{}", redirectUrl);
+            return ResultVo.redirectPage(redirectUrl);
         }
 
         //鍒ゆ柇褰撳墠openId 鏄惁缁戝畾浜嗕笟涓�
@@ -258,6 +285,7 @@
             CommonCache.setValue(code, openId, expireTime);
             CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
             CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
+            CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime);
             if (errorUrl.indexOf("?") > 0) {
                 errorUrl += ("&code=" + code);
             } else {
@@ -287,6 +315,8 @@
             CommonCache.setValue(code, openId, expireTime);
             CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime);
             CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime);
+            CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime);
+
             if (errorUrl.indexOf("?") > 0) {
                 errorUrl += ("&code=" + code);
             } else {
@@ -376,6 +406,120 @@
         return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj);
     }
 
+    @Override
+    public ResponseEntity<String> refreshOpenId(IPageData pd, String redirectUrl, String wAppId, HttpServletRequest request, HttpServletResponse response) {
+
+        SmallWeChatDto smallWeChatDto = null;
+        if (!StringUtil.isEmpty(wAppId)) {
+            JSONObject paramIn = new JSONObject();
+            paramIn.put("appId", wAppId);
+            smallWeChatDto = getSmallWechat(pd, paramIn);
+        }
+        if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+            wAppId = wechatAuthProperties.getWechatAppId();
+        }
+
+        //鍒嗛厤urlCode
+        String urlCode = UUID.randomUUID().toString();
+        JSONObject param = new JSONObject();
+        if (redirectUrl.indexOf("appId") < 0) {
+            redirectUrl += ("&appId=" + smallWeChatDto.getAppId());
+        }
+        param.put("redirectUrl", redirectUrl);
+        CommonCache.setValue(urlCode, param.toJSONString(), expireTime);
+
+        URL url = null;
+        String openUrl = "";
+        try {
+            url = new URL(redirectUrl);
+
+            String newUrl = url.getProtocol() + "://" + url.getHost();
+            if (url.getPort() > 0) {
+                newUrl += (":" + url.getPort());
+            }
+
+            openUrl = WechatConstant.OPEN_AUTH
+                    .replace("APPID", smallWeChatDto.getAppId())
+                    .replace("SCOPE", "snsapi_base")
+                    .replace(
+                            "REDIRECT_URL",
+                            URLEncoder
+                                    .encode(
+                                            (newUrl
+                                                    + "/app/openServiceNotifyOpenId?appId=992020061452450002&urlCode=" +
+                                                    urlCode + "&wId=" + WechatFactory.getWId(wAppId)),
+                                            "UTF-8")).replace("STATE", "1");
+
+        } catch (Exception e) {
+            logger.error("寰俊鍏紬鍙烽壌鏉� redirectUrl 閿欒 " + redirectUrl, e);
+            throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, e.getLocalizedMessage());
+        }
+        JSONObject urlObj = new JSONObject();
+        urlObj.put("openUrl", openUrl);
+
+        return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj);
+    }
+
+    @Override
+    public ResponseEntity openServiceNotifyOpenId(IPageData pd, HttpServletRequest request) {
+        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
+        String authCode = paramIn.getString("code");
+        String state = paramIn.getString("state");
+        String paramStr = CommonCache.getAndRemoveValue(paramIn.getString("urlCode"));
+
+        if (StringUtil.isEmpty(paramStr)) {
+            return ResultVo.redirectPage("/");
+        }
+
+        JSONObject param = JSONObject.parseObject(paramStr);
+        String redirectUrl = param.getString("redirectUrl");
+        String wId = paramIn.getString("wId");
+        SmallWeChatDto smallWeChatDto = null;
+        if (!StringUtil.isEmpty(wId)) {
+            paramIn.put("appId", WechatFactory.getAppId(wId));
+            smallWeChatDto = getSmallWechat(pd, paramIn);
+        }
+        if (smallWeChatDto == null) { //浠庨厤缃枃浠朵腑鑾峰彇 灏忕▼搴忛厤缃俊鎭�
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
+
+        String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", smallWeChatDto.getAppId())
+                .replace("SECRET", smallWeChatDto.getAppSecret())
+                .replace("CODE", authCode);
+
+        ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class);
+
+        logger.debug("璋冪敤寰俊鎹㈠幓openId " + paramOut);
+        if (paramOut.getStatusCode() != HttpStatus.OK) {
+            return ResultVo.redirectPage("/");
+        }
+        JSONObject paramObj = JSONObject.parseObject(paramOut.getBody());
+        //鑾峰彇 openId
+        String openId = paramObj.getString("openid");
+        redirectUrl = redirectUrl + "&openId=" + openId;
+
+        //鏌ヨ鏄惁鏈夎溅鐗屽彿
+        OwnerCarOpenUserDto ownerCarOpenUserDto = new OwnerCarOpenUserDto();
+        ownerCarOpenUserDto.setOpenId(openId);
+        List<OwnerCarOpenUserDto> ownerCarOpenUserDtos = ownerCarOpenUserV1InnerServiceSMOImpl.queryOwnerCarOpenUsers(ownerCarOpenUserDto);
+        if (ownerCarOpenUserDtos != null && ownerCarOpenUserDtos.size() > 0) {
+            redirectUrl += ("&carNum=" + ownerCarOpenUserDtos.get(0).getCarNum());
+        }
+
+
+        //redirectUrl = redirectUrl + (redirectUrl.indexOf("?") > 0 ? "&key=" + tmpUserDto.getKey() : "?key=" + tmpUserDto.getKey());
+        return ResultVo.redirectPage(redirectUrl);
+    }
+
     /**
      * 鍏紬鍙风櫥褰�
      *
@@ -401,6 +545,7 @@
         String openId = CommonCache.getValue(code);
         String nickname = CommonCache.getValue(code + "-nickname");
         String headimgurl = CommonCache.getValue(code + "-headimgurl");
+        String unionid = CommonCache.getValue(code + "-unionid");
 
         if (StringUtil.isEmpty(openId)) {
             responseEntity = new ResponseEntity<>("椤甸潰澶辨晥锛岃鍒锋柊鍚庨噸璇�", HttpStatus.UNAUTHORIZED);
@@ -417,6 +562,7 @@
         JSONObject userOwnerInfo = new JSONObject();
         OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
         ownerAppUserDto.setOpenId(openId);
+        ownerAppUserDto.setUnionId(unionid);
         // ownerAppUserDto.setNickName(StringUtil.encodeEmoji(nickname));
         ownerAppUserDto.setNickName(nickname);
         ownerAppUserDto.setHeadImgUrl(headimgurl);
@@ -448,7 +594,7 @@
                 "", "", "", "",
                 pd.getAppId());
         responseEntity = this.callCenterService(restTemplate, pd, "",
-                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                "smallWeChat.listSmallWeChats?appId="
                         + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET);
 
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
@@ -538,7 +684,7 @@
                 "", "", "", "",
                 pd.getAppId());
         responseEntity = this.callCenterService(restTemplate, pd, "",
-                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                "smallWeChat.listSmallWeChats?appId="
                         + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
 
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
@@ -561,5 +707,5 @@
     public void setRestTemplate(RestTemplate restTemplate) {
         this.restTemplate = restTemplate;
     }
-    
+
 }

--
Gitblit v1.8.0