java110
2020-03-22 4e05734fb00accfaded060f022629379ca0d43c9
FrontService/src/main/java/com/java110/front/controller/CallComponentController.java
@@ -35,8 +35,69 @@
    @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
     *
     * @return
     */
@@ -235,7 +296,7 @@
            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;
    }