Ver código fonte

优化代码

java110 2 anos atrás
pai
commit
5ff56bcae8

+ 3 - 12
service-api/src/main/java/com/java110/api/smo/DefaultAbstractComponentSMO.java

@@ -381,10 +381,6 @@ public class DefaultAbstractComponentSMO extends AbstractComponentSMO {
         return new ResponseEntity<String>(resultVo.getMsg(), resultVo.getCode() == ResultVo.CODE_OK ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
     }
 
-    private ResponseEntity<String> getStoreEnterCommunitys(IPageData pd, String storeId, String storeTypeCd, RestTemplate restTemplate) {
-        ResultVo resultVo = getCommunityStoreInfoSMOImpl.getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
-        return new ResponseEntity<String>(resultVo.getMsg(), resultVo.getCode() == ResultVo.CODE_OK ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
-    }
 
     /**
      * 查询商户信息
@@ -393,17 +389,12 @@ public class DefaultAbstractComponentSMO extends AbstractComponentSMO {
      */
     protected void checkStoreEnterCommunity(IPageData pd, String storeId, String storeTypeCd, String communityId, RestTemplate restTemplate) {
         Assert.hasLength(pd.getUserId(), "用户未登录请先登录");
-        ResponseEntity<String> responseEntity = null;
-        responseEntity = getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
-        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+        ResultVo resultVo =  getCommunityStoreInfoSMOImpl.getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
+        if (resultVo.getCode() != ResultVo.CODE_OK) {
             throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, "还未入驻小区,请先入驻小区");
         }
 
-        Assert.jsonObjectHaveKey(responseEntity.getBody().toString(), "data", "还未入驻小区,请先入驻小区");
-
-        JSONObject community = JSONObject.parseObject(responseEntity.getBody().toString());
-
-        JSONArray communitys = community.getJSONArray("data");
+        JSONArray communitys = JSONArray.parseArray(resultVo.getData().toString());
 
         if (communitys == null || communitys.size() == 0) {
             throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, "还未入驻小区,请先入驻小区");

+ 7 - 7
service-api/src/main/java/com/java110/api/smo/GetCommunityStoreInfoSMOImpl.java

@@ -35,13 +35,13 @@ public class GetCommunityStoreInfoSMOImpl extends DefaultAbstractComponentSMO im
             throw new IllegalArgumentException(responseEntity.getBody());
         }
 
-        if(!StringUtil.isJsonObject(responseEntity.getBody())){
-           // return new ResultVo(responseEntity.getStatusCode() == HttpStatus.OK ? ResultVo.CODE_OK : ResultVo.CODE_ERROR, responseEntity.getBody());
+        if (!StringUtil.isJsonObject(responseEntity.getBody())) {
+            // return new ResultVo(responseEntity.getStatusCode() == HttpStatus.OK ? ResultVo.CODE_OK : ResultVo.CODE_ERROR, responseEntity.getBody());
             throw new IllegalArgumentException(responseEntity.getBody());
         }
 
         JSONObject paramJson = JSONObject.parseObject(responseEntity.getBody());
-        if(paramJson.containsKey("code") && paramJson.getIntValue("code") != 0){
+        if (paramJson.containsKey("code") && paramJson.getIntValue("code") != 0) {
             throw new IllegalArgumentException(paramJson.getString("msg"));
         }
 
@@ -64,10 +64,10 @@ public class GetCommunityStoreInfoSMOImpl extends DefaultAbstractComponentSMO im
             throw new IllegalArgumentException(responseEntity.getBody());
         }
         JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
-        if(paramOut.containsKey("code") && ResultVo.CODE_OK != paramOut.getIntValue("code")){
+        if (paramOut.containsKey("code") && ResultVo.CODE_OK != paramOut.getIntValue("code")) {
             throw new IllegalArgumentException(paramOut.getString("msg"));
         }
-        return new ResultVo(responseEntity.getStatusCode() == HttpStatus.OK ? ResultVo.CODE_OK : ResultVo.CODE_ERROR, responseEntity.getBody(),responseEntity.getBody());
+        return new ResultVo(paramOut.getIntValue("code"), paramOut.getString("msg"), paramOut.get("data"));
     }
 
     @Override
@@ -98,8 +98,8 @@ public class GetCommunityStoreInfoSMOImpl extends DefaultAbstractComponentSMO im
 
         JSONArray privileges = data.getJSONArray("privileges");
 
-        if(!SecureInvocation.secure(this.getClass())){
-            return new ResultVo(ResultVo.CODE_OK,privileges.toJSONString(),ResultVo.EMPTY_ARRAY);
+        if (!SecureInvocation.secure(this.getClass())) {
+            return new ResultVo(ResultVo.CODE_OK, privileges.toJSONString(), ResultVo.EMPTY_ARRAY);
         }
 
         return new ResultVo(responseEntity.getStatusCode() == HttpStatus.OK ? ResultVo.CODE_OK : ResultVo.CODE_ERROR, privileges.toJSONString());

+ 55 - 8
service-user/src/main/java/com/java110/user/cmd/wechat/GetOpenIdByCodeCmd.java

@@ -5,16 +5,25 @@ import com.java110.core.annotation.Java110Cmd;
 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.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;
 
 /**
  * 根据小程序code 获取openId
@@ -26,6 +35,12 @@ public class GetOpenIdByCodeCmd extends Cmd {
     @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 {
         Assert.hasKeyAndValue(reqJson, "code", "未包含code");
@@ -35,8 +50,24 @@ public class GetOpenIdByCodeCmd extends Cmd {
     @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, "appId");
+            appSecret = MappingCache.getValue(MappingConstant.MALL_WECHAT_DOMAIN, "appSecret");
+        } 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 +76,10 @@ public class GetOpenIdByCodeCmd extends Cmd {
         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 +91,25 @@ public class GetOpenIdByCodeCmd extends Cmd {
         }
 
         String openId = responseObj.getString("openid");
-        String sessionKey = responseObj.getString("session_key");
+
+        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));
 
     }
 }

+ 3 - 13
springboot/src/main/java/com/java110/boot/smo/DefaultAbstractComponentSMO.java

@@ -385,10 +385,6 @@ public class DefaultAbstractComponentSMO extends AbstractComponentSMO {
         return new ResponseEntity<String>(resultVo.getMsg(), resultVo.getCode() == ResultVo.CODE_OK ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
     }
 
-    private ResponseEntity<String> getStoreEnterCommunitys(IPageData pd, String storeId, String storeTypeCd, RestTemplate restTemplate) {
-        ResultVo resultVo = getCommunityStoreInfoSMOImpl.getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
-        return new ResponseEntity<String>(resultVo.getMsg(), resultVo.getCode() == ResultVo.CODE_OK ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
-    }
 
     /**
      * 查询商户信息
@@ -397,17 +393,12 @@ public class DefaultAbstractComponentSMO extends AbstractComponentSMO {
      */
     protected void checkStoreEnterCommunity(IPageData pd, String storeId, String storeTypeCd, String communityId, RestTemplate restTemplate) {
         Assert.hasLength(pd.getUserId(), "用户未登录请先登录");
-        ResponseEntity<String> responseEntity = null;
-        responseEntity = getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
-        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+        ResultVo resultVo =  getCommunityStoreInfoSMOImpl.getStoreEnterCommunitys(pd, storeId, storeTypeCd, restTemplate);
+        if (resultVo.getCode() != ResultVo.CODE_OK) {
             throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, "还未入驻小区,请先入驻小区");
         }
 
-        Assert.jsonObjectHaveKey(responseEntity.getBody().toString(), "data", "还未入驻小区,请先入驻小区");
-
-        JSONObject community = JSONObject.parseObject(responseEntity.getBody().toString());
-
-        JSONArray communitys = community.getJSONArray("data");
+        JSONArray communitys = JSONArray.parseArray(resultVo.getData().toString());
 
         if (communitys == null || communitys.size() == 0) {
             throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, "还未入驻小区,请先入驻小区");
@@ -421,7 +412,6 @@ public class DefaultAbstractComponentSMO extends AbstractComponentSMO {
 
     }
 
-
     private JSONObject getCurrentCommunity(JSONArray communitys, String communityId) {
         for (int communityIndex = 0; communityIndex < communitys.size(); communityIndex++) {
             if (communityId.equals(communitys.getJSONObject(communityIndex).getString("communityId"))) {

+ 1 - 1
springboot/src/main/java/com/java110/boot/smo/GetCommunityStoreInfoSMOImpl.java

@@ -66,7 +66,7 @@ public class GetCommunityStoreInfoSMOImpl extends DefaultAbstractComponentSMO im
         if(paramOut.containsKey("code") && ResultVo.CODE_OK != paramOut.getIntValue("code")){
             throw new IllegalArgumentException(paramOut.getString("msg"));
         }
-        return new ResultVo(responseEntity.getStatusCode() == HttpStatus.OK ? ResultVo.CODE_OK : ResultVo.CODE_ERROR, responseEntity.getBody());
+        return new ResultVo(paramOut.getIntValue("code"), paramOut.getString("msg"), paramOut.get("data"));
     }
 
     @Override