Просмотр исходного кода

加入front 加入 api 调用方式

java110 лет назад: 6
Родитель
Сommit
4e05734fb0

+ 5 - 1
FrontService/src/main/java/com/java110/front/aop/PageProcessAspect.java

@@ -11,6 +11,7 @@ import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpMethod;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -108,6 +109,9 @@ public class PageProcessAspect {
             if (urls.length == 6) {
                 componentCode = urls[4];
                 componentMethod = urls[5];
+            } else {
+                componentCode = "api";
+                componentMethod = "callApi";
             }
         } else if (url.contains("flow")) { //流程处理
             String[] urls = url.split("/");
@@ -117,8 +121,8 @@ public class PageProcessAspect {
             }
         }
         pd = PageData.newInstance().builder(userId, userName, this.getToken(request), reqData, componentCode, componentMethod, url, sessionId, appId);
+        pd.setMethod(request.getMethod().equals("GET") ? HttpMethod.GET : HttpMethod.POST);
         request.setAttribute(CommonConstant.CONTEXT_PAGE_DATA, pd);
-
     }
 
     @AfterReturning(returning = "ret", pointcut = "dataProcess()")

+ 46 - 0
FrontService/src/main/java/com/java110/front/components/api/ApiComponent.java

@@ -0,0 +1,46 @@
+package com.java110.front.components.api;
+
+
+import com.java110.core.context.IPageData;
+import com.java110.front.smo.common.ICommonGetSMO;
+import com.java110.front.smo.common.ICommonPostSMO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * 活动组件管理类
+ * <p>
+ * add by wuxw
+ * <p>
+ * 2019-06-29
+ */
+@Component("api")
+public class ApiComponent {
+
+    @Autowired
+    private ICommonGetSMO commonGetSMOImpl;
+
+    @Autowired
+    private ICommonPostSMO commonPostSMOImpl;
+
+    /**
+     * 查询活动列表
+     *
+     * @param pd 页面数据封装
+     * @return 返回 ResponseEntity 对象
+     */
+    public ResponseEntity<String> callApi(IPageData pd) {
+
+        // get方式调用
+        if(HttpMethod.GET == pd.getMethod()){
+            return commonGetSMOImpl.doService(pd);
+        }
+        //post 方式调用
+        return commonPostSMOImpl.doService(pd);
+    }
+
+
+}

+ 61 - 1
FrontService/src/main/java/com/java110/front/controller/CallComponentController.java

@@ -35,6 +35,66 @@ public class CallComponentController extends BaseController {
     @Autowired
     private RestTemplate restTemplate;
 
+
+    /**
+     * 前台调用api方法
+     * add by wuxw 2020-03-16
+     *
+     * @return
+     */
+
+    @RequestMapping(path = "/callComponent/{api}")
+    public ResponseEntity<String> callApi(
+            @PathVariable String api,
+            //@RequestBody String info,
+            HttpServletRequest request) {
+        ResponseEntity<String> responseEntity = null;
+        String componentCode = "api";
+        String componentMethod = "callApi";
+        try {
+            Assert.hasLength(api, "参数错误,未传入api编码");
+
+            IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+            pd.setApiUrl("/api/" + api);
+            //权限校验
+            hasPrivilege(restTemplate, pd, "/" + api);
+
+            Object componentInstance = ApplicationContextFactory.getBean(componentCode);
+
+            Assert.notNull(componentInstance, "未找到组件对应的处理类,请确认 " + componentCode);
+
+            Method cMethod = componentInstance.getClass().getDeclaredMethod(componentMethod, IPageData.class);
+
+            Assert.notNull(cMethod, "未找到组件对应处理类的方法,请确认 " + componentCode + "方法:" + componentMethod);
+
+
+            logger.debug("组件编码{},组件方法{},pd 为{}", componentCode, componentMethod, pd.toString());
+
+            responseEntity = (ResponseEntity<String>) cMethod.invoke(componentInstance, pd);
+
+        } catch (SMOException e) {
+            /*MultiValueMap<String, String> headers = new HttpHeaders();
+            headers.add("code", e.getResult().getCode());*/
+            logger.error("调用api异常", e);
+            responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        } catch (Exception e) {
+            logger.error("调用api异常", e);
+            String msg = "";
+            if (e instanceof InvocationTargetException) {
+                Throwable targetEx = ((InvocationTargetException) e).getTargetException();
+                if (targetEx != null) {
+                    msg = targetEx.getMessage();
+                }
+            } else {
+                msg = e.getMessage();
+            }
+            responseEntity = new ResponseEntity<>(msg, HttpStatus.INTERNAL_SERVER_ERROR);
+        } finally {
+            logger.debug("api调用返回信息为{}", responseEntity);
+            return responseEntity;
+        }
+    }
+
     /**
      * 调用组件方法
      * add by wuxw 2020-03-16
@@ -236,7 +296,7 @@ public class CallComponentController extends BaseController {
             reqData = paramObj.toJSONString();
         }
 
-        IPageData newPd = PageData.newInstance().builder(pd.getUserId(),pd.getUserName(), pd.getToken(),
+        IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(),
                 reqData, pd.getComponentCode(), pd.getComponentMethod(), "", pd.getSessionId(), "");
         return newPd;
     }

+ 5 - 0
java110-core/src/main/java/com/java110/core/context/IPageData.java

@@ -1,5 +1,6 @@
 package com.java110.core.context;
 
+import org.springframework.http.HttpMethod;
 import org.springframework.http.ResponseEntity;
 
 /**
@@ -99,6 +100,10 @@ import org.springframework.http.ResponseEntity;
     //设置调用api 服务地址
     public void setApiUrl(String apiUrl);
 
+    public HttpMethod getMethod();
+
+    public void setMethod(HttpMethod method);
+
     /**
      * 构建 pd 对象
      * @param userId 用户ID

+ 11 - 0
java110-core/src/main/java/com/java110/core/context/PageData.java

@@ -2,6 +2,7 @@ package com.java110.core.context;
 
 import com.alibaba.fastjson.JSONObject;
 import com.java110.utils.util.DateUtil;
+import org.springframework.http.HttpMethod;
 import org.springframework.http.ResponseEntity;
 
 import java.io.Serializable;
@@ -48,6 +49,8 @@ public class PageData implements IPageData, Serializable {
 
     private String apiUrl;
 
+    private HttpMethod method;
+
     private ResponseEntity responseEntity;
 
     public String getUserId() {
@@ -227,4 +230,12 @@ public class PageData implements IPageData, Serializable {
     public void setApiUrl(String apiUrl) {
         this.apiUrl = apiUrl;
     }
+
+    public HttpMethod getMethod() {
+        return method;
+    }
+
+    public void setMethod(HttpMethod method) {
+        this.method = method;
+    }
 }

+ 7 - 7
java110-db/src/main/resources/mapper/community/ActivitiesServiceDaoImplMapper.xml

@@ -6,13 +6,13 @@
 
     <!-- 保存活动信息 add by wuxw 2018-07-03 -->
     <insert id="saveBusinessActivitiesInfo" parameterType="Map">
-           insert into business_activities(
-collect_count,like_count,title,read_count,user_name,user_id,activities_id,operate,type_cd,context,start_time,end_time,community_id,b_id,header_img,state
-) values (
-#{collectCount},#{likeCount},#{title},#{readCount},#{userName},#{userId},#{activitiesId},#{operate},#{typeCd},#{context},#{startTime},#{endTime},
-#{communityId},#{bId},#{headerImg},#{state}
-)
-       </insert>
+        insert into business_activities(
+        collect_count,like_count,title,read_count,user_name,user_id,activities_id,operate,type_cd,context,start_time,end_time,community_id,b_id,header_img,state
+        ) values (
+        #{collectCount},#{likeCount},#{title},#{readCount},#{userName},#{userId},#{activitiesId},#{operate},#{typeCd},#{context},#{startTime},#{endTime},
+        #{communityId},#{bId},#{headerImg},#{state}
+        )
+    </insert>
 
 
     <!-- 查询活动信息(Business) add by wuxw 2018-07-03 -->