22
33
44import  com .hsjfans .github .config .Config ;
5- import  com .hsjfans .github .model .ApiTree ;
6- import  com .hsjfans .github .model .ControllerClass ;
7- import  com .hsjfans .github .model .ControllerMethod ;
8- import  com .hsjfans .github .model .RequestParam ;
5+ import  com .hsjfans .github .model .*;
96import  com .hsjfans .github .util .CollectionUtil ;
107import  com .hsjfans .github .util .FileUtil ;
118import  com .hsjfans .github .util .StringUtil ;
129
10+ import  java .util .List ;
11+ 1312
1413/** 
1514 * 
@@ -26,6 +25,51 @@ public class HtmlGenerator extends AbstractGenerator {
2625 private  static  final  String  urlTpl  = "api-url.html" ;
2726 private  static  final  String  extraTpl  = "js.html" ;
2827
28+  private  static  final  String  Request_Params_Table_No_head  = " \n  <tr>\n "  +
29+  " <td>%s</td>\n "  +
30+  " <td>%s</td>\n "  +
31+  " <td>%s</td>\n "  +
32+  " <td>%s</td>\n "  +
33+  " <td>%s</td>\n "  +
34+  " <td>%s</td>\n "  +
35+  " </tr> " ;
36+  private  static  final  String  Union_No_head  = " \n  <tr>\n "  +
37+  " <td>%s</td>\n "  +
38+  " <td>%s</td>\n "  +
39+  " </tr> " ;
40+ 41+  private  static  final  String  Request_Params_Table_head  = "<table class=\" table\" >\n "  +
42+  " <tr align=\" center\" >\n "  +
43+  " <th>名称</th>\n "  +
44+  " <th>类型</th>\n "  +
45+  " <th>取值</th>\n "  +
46+  " <th>必需</th>\n "  +
47+  " <th>模糊</th>\n "  +
48+  " <th>说明</th>\n "  +
49+  " </tr>\n "  +
50+  " ${requestParams}\n "  +
51+  " </table>" ;
52+ 53+  private  static  final  String  Response_Return_Table_Head  = "<table class=\" table\" >\n "  +
54+  " <!--<caption><h4></h4></caption>-->\n "  +
55+  " <tr align=\" center\" >\n "  +
56+  " <th>名称</th>\n "  +
57+  " <th>类型</th>\n "  +
58+  " <th>取值</th>\n "  +
59+  " <th>说明</th>\n "  +
60+  " </tr>\n "  +
61+  " ${responses}\n "  +
62+  " </table>" ;
63+ 64+ 65+  private  static  final  String  Response_Return_Table_No_Head  =
66+  " <tr>\n "  +
67+  " <td>%s</td>\n "  +
68+  " <td>%s</td>\n "  +
69+  " <td>%s</td>\n "  +
70+  " <td>%s</td>\n "  +
71+  " </tr>" ;
72+ 2973
3074 @ Override 
3175 public  void  from (ApiTree  apiTree , Config  config ) {
@@ -94,41 +138,71 @@ protected void buildApiDoc(ControllerClass controllerClass, ControllerMethod con
94138 method  = method .replace ("${api-url-description}" ,controllerMethod .getDescription ());
95139 method  = method .replace ("${methods}" , CollectionUtil .requestMethodsToString (controllerMethod .getMethods ()));
96140 method  = method .replace ("${author}" ,controllerMethod .getAuthor ());
141+  method  = method .replace ("${requestParams}" , generateRequestParams (controllerMethod .getParams ()));
142+  method  = method .replace ("${responses}" , generateResponseReturn (controllerMethod .getResponseReturn ()));
143+ 144+ 145+  FileUtil .to (this .config .getOutPath ()+controllerClass .getName ()+"_" +controllerMethod .getName ()+".html" ,method );
146+  }
97147
148+ 149+  private  String  generateRequestParams (List <RequestParam > requestParams ){
98150 StringBuilder  params  = new  StringBuilder ();
99-  controllerMethod .getParams ().forEach (requestParam ->{
100-  if (requestParam .getParams ()==null ){
101-  params .append (String .format (" \n  <tr>\n "  +
102-  " <td>%s</td>\n "  +
103-  " <td>%s</td>\n "  +
104-  " <td>%s</td>\n "  +
105-  " <td>%s</td>\n "  +
106-  " <td>%s</td>\n "  +
107-  " <td>%s</td>\n "  +
108-  " </tr> " ,requestParam .getName (),requestParam .getType (),
151+  requestParams .forEach (requestParam ->{
152+  if (requestParam .getParams ()!=null &&requestParam .getParams ().size ()>0 ){
153+  params .append (String .format (Union_No_head ,
154+  requestParam .getName (),
155+  Request_Params_Table_head .replace ("${requestParams}" ,generateRequestParams (requestParam .getParams ()))
156+  ));
157+  }else  {
158+  params .append (String .format (Request_Params_Table_No_head ,requestParam .getName (),requestParam .getType (),
109159 StringUtil .enumToStrs (requestParam .getEnumValues ()),requestParam .isNecessary (),requestParam .isFuzzy (),
110160 requestParam .getDescription ()));
111-  }else  {
112-  // todo 
113161 }
114162 });
115-  method  = method .replace ("${requestParams}" , params .toString ());
116163
117-  StringBuilder  responses  = new  StringBuilder ();
118-  responses .append (String .format (
119-  " <tr>\n "  +
120-  " <td>%s</td>\n "  +
121-  " <td>%s</td>\n "  +
122-  " <td>%s</td>\n "  +
123-  " </tr>" 
124-  ,controllerMethod .getResponseReturn ().getName (),
125-  controllerMethod .getResponseReturn ().getType (),
126-  controllerMethod .getResponseReturn ().getDescription ()));
164+  return  params .toString ();
165+ 166+  }
127167
128-  method  =  method . replace ( "${responses}" ,  responses . toString ()); 
168+  private String generateResponseReturn ( ResponseReturn responseReturn ){ 
129169
170+  StringBuilder  responses  = new  StringBuilder ();
130171
131-  FileUtil .to (this .config .getOutPath ()+controllerClass .getName ()+"_" +controllerMethod .getName ()+".html" ,method );
172+  if (responseReturn .getReturnItem ()!=null &&responseReturn .getReturnItem ().size ()>0 ){
173+  responses .append (String .format (Union_No_head ,
174+  responseReturn .getName (),
175+  Request_Params_Table_head .replace ("${requestParams}" ,generateRequestParams (responseReturn .getReturnItem ()))
176+  ));
177+ // responses.append(Response_Return_Table_Head.replace("${responses}",generateResponseItems(responseReturn.getReturnItem()))); 
178+  }else  {
179+  responses .append (String .format (Response_Return_Table_No_Head ,
180+  responseReturn .getName (),
181+  responseReturn .getType (),
182+  StringUtil .enumToStrs (responseReturn .getEnumValues ()),
183+  responseReturn .getDescription ()));
184+  }
185+  return  responses .toString ();
186+  }
187+ 188+  private  String  generateResponseItems (List <RequestParam > requestParams ){
189+  StringBuilder  responses  = new  StringBuilder ();
190+  requestParams .forEach (requestParam  -> {
191+  if (requestParam .getParams ()!=null &&requestParam .getParams ().size ()>0 ){
192+  responses .append (String .format (Union_No_head ,
193+  requestParam .getName (),
194+  Request_Params_Table_head .replace ("${requestParams}" ,generateRequestParams (requestParam .getParams ()))
195+  ));
196+  responses .append (Response_Return_Table_Head .replace ("${responses}" ,generateResponseItems (requestParam .getParams ())));
197+  }else  {
198+  responses .append (String .format (Response_Return_Table_No_Head ,
199+  requestParam .getName (),
200+  requestParam .getType (),
201+  StringUtil .enumToStrs (requestParam .getEnumValues ()),
202+  requestParam .getDescription ()));
203+  }
204+  });
205+  return  responses .toString ();
132206 }
133207
134208 @ Override 
0 commit comments