Browse Source

优化 文档开发完成

wuxw 3 years ago
parent
commit
b0a706ed4d

+ 134 - 1
java110-doc/src/main/java/com/java110/doc/controller/DocController.java

@@ -3,18 +3,21 @@ package com.java110.doc.controller;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.doc.annotation.*;
 import com.java110.doc.entity.ApiDocDto;
 import com.java110.doc.entity.CmdDocDto;
 import com.java110.doc.entity.RequestMappingsDocDto;
 import com.java110.doc.registrar.ApiDocCmdPublishing;
 import com.java110.doc.registrar.ApiDocPublishing;
 import com.java110.utils.factory.ApplicationContextFactory;
+import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 
 import javax.servlet.http.HttpServletRequest;
+import java.lang.annotation.Annotation;
 import java.util.List;
 
 @RestController
@@ -82,10 +85,140 @@ public class DocController {
         return new ResponseEntity<>(JSONArray.toJSONString(cmdDocDtos), HttpStatus.OK);
     }
 
+
+    @RequestMapping(path = "/api/pageContent", method = RequestMethod.GET)
+    public ResponseEntity<String> pageContent(
+            @RequestParam("name") String name,
+            @RequestParam("serviceCode") String serviceCode,
+            @RequestParam("resource") String resource,
+            HttpServletRequest request) {
+
+        List<RequestMappingsDocDto> mappingsDocDtos = ApiDocPublishing.getMappingsDocDtos();
+
+        RequestMappingsDocDto newMappingsDocDto = null;
+        for (RequestMappingsDocDto mappingsDocDto : mappingsDocDtos) {
+            if (mappingsDocDto.getName().equals(name)) {
+                newMappingsDocDto = mappingsDocDto;
+            }
+        }
+
+        if (newMappingsDocDto == null) {
+            return new ResponseEntity<>("{}", HttpStatus.OK);
+        }
+
+        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        if ("boot".equals(newMappingsDocDto.getStartWay())) {
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        }
+        ResponseEntity<String> responseEntity = null;
+        HttpEntity<String> httpEntity = new HttpEntity<String>("", new HttpHeaders());
+        try {
+            responseEntity = restTemplate.exchange(newMappingsDocDto.getUrl()+"/doc/api/"+resource+"/"+serviceCode, HttpMethod.GET, httpEntity, String.class);
+        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
+            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
+        } catch (Exception e) {
+            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+        return responseEntity;
+    }
+
     @RequestMapping(path = "/api/{resource}/{serviceCode}", method = RequestMethod.GET)
     public ResponseEntity<String> api(@PathVariable String resource,
                                       @PathVariable String serviceCode,
                                       HttpServletRequest request) {
-        return null;
+        CmdDocDto cmdDocDto = ApiDocCmdPublishing.getCmdDocs(resource,serviceCode);
+
+        if(cmdDocDto == null){
+            return new ResponseEntity<>("{}", HttpStatus.OK);
+        }
+
+        JSONObject param = JSONObject.parseObject(JSONObject.toJSONString(cmdDocDto));
+
+        Class clazz = cmdDocDto.getCmdClass();
+
+
+        doJava110ParamsDoc(clazz,param);
+
+
+        doJava110ResponseDoc(param, clazz);
+
+        doJava110ExampleDoc(param, clazz);
+
+        return new ResponseEntity<>(param.toJSONString(), HttpStatus.OK);
+    }
+
+    private void doJava110ExampleDoc(JSONObject param, Class clazz) {
+        Java110ExampleDoc java110ExampleDoc = AnnotationUtils.findAnnotation(clazz,Java110ExampleDoc.class);
+
+        if(java110ExampleDoc == null){
+            return ;
+        }
+
+        param.put("reqBody",java110ExampleDoc.reqBody());
+        param.put("resBody",java110ExampleDoc.resBody());
+    }
+
+    private void doJava110ResponseDoc(JSONObject param, Class clazz) {
+        Java110ResponseDoc java110ResponseDoc = AnnotationUtils.findAnnotation(clazz,Java110ResponseDoc.class);
+
+        if(java110ResponseDoc == null){
+            return ;
+        }
+
+        Java110ParamDoc[] java110ParamDocs = java110ResponseDoc.params();
+
+        JSONArray params = new JSONArray();
+        JSONObject p = null;
+        for(Java110ParamDoc java110ParamDoc : java110ParamDocs){
+            p = new JSONObject();
+            p.put("name",java110ParamDoc.name());
+            p.put("defaultValue",java110ParamDoc.defaultValue());
+            p.put("remark",java110ParamDoc.remark());
+            p.put("type",java110ParamDoc.type());
+            p.put("length",java110ParamDoc.length());
+            p.put("parentNodeName",java110ParamDoc.parentNodeName());
+            params.add(p);
+        }
+        param.put("resParam",params);
+    }
+
+    private void doJava110ParamsDoc(Class clazz, JSONObject param) {
+
+        Java110ParamsDoc java110ParamsDoc = AnnotationUtils.findAnnotation(clazz,Java110ParamsDoc.class);
+
+        if(java110ParamsDoc == null){
+            return ;
+        }
+
+        Java110HeaderDoc[] java110HeaderDocs = java110ParamsDoc.headers();
+
+        JSONArray headers = new JSONArray();
+        JSONObject header = null;
+        for(Java110HeaderDoc java110HeaderDoc : java110HeaderDocs){
+            header = new JSONObject();
+            header.put("name",java110HeaderDoc.name());
+            header.put("defaultValue",java110HeaderDoc.defaultValue());
+            header.put("description",java110HeaderDoc.description());
+            headers.add(header);
+        }
+
+        param.put("header",headers);
+
+
+        Java110ParamDoc[] java110ParamDocs = java110ParamsDoc.params();
+
+        JSONArray params = new JSONArray();
+        JSONObject p = null;
+        for(Java110ParamDoc java110ParamDoc : java110ParamDocs){
+            p = new JSONObject();
+            header.put("name",java110ParamDoc.name());
+            header.put("defaultValue",java110ParamDoc.defaultValue());
+            header.put("remark",java110ParamDoc.remark());
+            header.put("type",java110ParamDoc.type());
+            header.put("length",java110ParamDoc.length());
+            header.put("parentNodeName",java110ParamDoc.parentNodeName());
+            params.add(p);
+        }
+        param.put("reqParam",params);
     }
 }

+ 16 - 0
java110-doc/src/main/java/com/java110/doc/registrar/ApiDocCmdPublishing.java

@@ -40,4 +40,20 @@ public class ApiDocCmdPublishing {
 
         return retCmdDocDto;
     }
+
+
+    public static CmdDocDto getCmdDocs(String resource,String serviceCode) {
+
+        if (cmdDocs.size() < 1) {
+            return null;
+        }
+
+        for (CmdDocDto cmdDocDto : cmdDocs) {
+            if (cmdDocDto.getResource().equals(resource) && cmdDocDto.getServiceCode().equals(serviceCode)) {
+                return cmdDocDto;
+            }
+        }
+
+        return null;
+    }
 }

+ 24 - 66
java110-doc/src/main/resources/static/doc-ui.html

@@ -53,14 +53,14 @@
             </ul>
         </div>
         <div class="margin-left right white-bg border-radius doc-interface">
-            <h3 class="">登录接口</h3>
+            <h3 class="">{{content.title}}</h3>
             <div class="doc-margin-top" >
                 <div class="doc-title">1、请求地址:</div>
-                <div >http://127.0.0.1:8008/app/login.userPcLogin</div>
+                <div >{{content.url}}</div>
             </div>
             <div class="doc-margin-top" >
                 <div class="doc-title">2、请求方式:</div>
-                <div >POST</div>
+                <div >{{content.method}}</div>
             </div>
             <div class="doc-margin-top" >
                 <div class="doc-title">3、请求头信息:</div>
@@ -74,20 +74,10 @@
                             </tr>
                             </thead>
                             <tbody>
-                            <tr>
-                                <th>1</th>
-                                <td>Mark</td>
-                                <td>Otto</td>
-                            </tr>
-                            <tr>
-                                <th>2</th>
-                                <td>Jacob</td>
-                                <td>Thornton</td>
-                            </tr>
-                            <tr>
-                                <th>3</th>
-                                <td>Larry</td>
-                                <td>the Bird</td>
+                            <tr v-for="(item,index) in content.headers">
+                                <th>{{item.name}}</th>
+                                <td>{{item.defaultValue}}</td>
+                                <td>{{item.description}}</td>
                             </tr>
                             </tbody>
                         </table>
@@ -109,36 +99,20 @@
                         </tr>
                         </thead>
                         <tbody>
-                        <tr>
-                            <th>1</th>
-                            <td>Mark</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                        </tr>
-                        <tr>
-                            <th>2</th>
-                            <td>Jacob</td>
-                            <td>Thornton</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                        </tr>
-                        <tr>
-                            <th>3</th>
-                            <td>Larry</td>
-                            <td>the Bird</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
+                        <tr v-for="(item,index) in content.reqParam">
+                            <th>{{item.parentNodeName}}</th>
+                            <td>{{item.name}}</td>
+                            <td>{{item.type}}</td>
+                            <td>{{item.length}}</td>
+                            <td>{{item.defaultValue}}</td>
+                            <td>{{item.remark}}</td>
                         </tr>
                         </tbody>
                     </table>
                 </div>
                 <div class="highlight">
                     <div>请求报文:</div>
-                    <div>{"111":"BB"}</div>
+                    <div>{{content.reqBody}}</div>
                 </div>
             </div>
 
@@ -157,37 +131,21 @@
                         </tr>
                         </thead>
                         <tbody>
-                        <tr>
-                            <th>1</th>
-                            <td>Mark</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                        </tr>
-                        <tr>
-                            <th>2</th>
-                            <td>Jacob</td>
-                            <td>Thornton</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                        </tr>
-                        <tr>
-                            <th>3</th>
-                            <td>Larry</td>
-                            <td>the Bird</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
-                            <td>Otto</td>
+                        <tr v-for="(item,index) in content.resParam">
+                            <th>{{item.parentNodeName}}</th>
+                            <td>{{item.name}}</td>
+                            <td>{{item.type}}</td>
+                            <td>{{item.length}}</td>
+                            <td>{{item.defaultValue}}</td>
+                            <td>{{item.remark}}</td>
                         </tr>
                         </tbody>
                     </table>
                 </div>
             </div>
             <div class="highlight">
-                <div>请求报文:</div>
-                <div>{"111":"BB"}</div>
+                <div>返回报文:</div>
+                <div>{{content.resBody}}</div>
             </div>
 
         </div>

+ 30 - 4
java110-doc/src/main/resources/static/js/doc-ui.js

@@ -11,7 +11,16 @@
             menus: [],
             pages: [],
             curMenuName: '',
-            logo: '',
+            content: {
+                title:'',
+                url:'',
+                method:'',
+                headers:[],
+                reqParam:[],
+                resParam:[],
+                reqBody:'',
+                resBody:''
+            },
         },
         mounted: function() {
             this.getDocumentAndMenus();
@@ -50,11 +59,28 @@
                 this.curMenuName = _menu.name;
                 this._activeMenu(_menu.name);
                 this._listDocumentPages(_menu);
-                console.log('123123')
             },
 
-            _gotoPage: function(){
-                console.log('_gotoPage')
+            _gotoPage: function(_page){
+               let _that  = this;
+               let _param = {
+                   params: {
+                       name: this.curMenuName,
+                       serviceCode:_page.serviceCode,
+                       resource:_page.resource
+                   }
+               };
+
+               //发送get请求
+              //Vue.http.get('/doc/api/pageContent', _param)
+               Vue.http.get('mock/pages.json', _param)
+              .then(function(res) {
+                       _that.content = res.data;
+                   },
+                   function (errInfo, error) {
+                       console.log('请求失败处理');
+                   }
+               );
             },
             _listDocumentPages: function (_menu) {
                 let _that  = this;

+ 13 - 5
service-user/src/test/java/com/java110/user/AppTest.java

@@ -1,10 +1,15 @@
 package com.java110.user;
 
+import com.java110.doc.annotation.Java110HeaderDoc;
+import com.java110.doc.annotation.Java110ParamsDoc;
+import com.java110.user.cmd.login.PcUserLoginCmd;
 import com.java110.utils.util.DateUtil;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
+import org.springframework.core.annotation.AnnotationUtils;
 
+import java.lang.annotation.Annotation;
 import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.Date;
@@ -35,12 +40,15 @@ public class AppTest
 
 
     public void testSplit(){
-        String a= "1-1-1-1001";
-        String[] as = a.split("-",3);
+        Class clazz = PcUserLoginCmd.class;
+
+
+        Java110ParamsDoc java110ParamsDoc = AnnotationUtils.findAnnotation(clazz,Java110ParamsDoc.class);
+
+        Java110HeaderDoc[] java110HeaderDocs = java110ParamsDoc.headers();
+
+        System.out.println(123);
 
-        for(String aa :as){
-            System.out.println(aa);
-        }
     }
 
     /**