// 查找接口方法对象 private static API FindApi(TypeAPI typeapi, string methodName) { foreach (var api in typeapi.Apis) { if (api.Name == methodName) { return(api); } } return(null); }
/// <summary> /// 构造接口清单页面 /// </summary> static string BuildApiListHtml(TypeAPI typeapi) { // 概述信息 StringBuilder sb = new StringBuilder(); sb.AppendLine(BuildCss()); sb.AppendLine("<h1>" + typeapi.Description + "</h1>"); foreach (HistoryAttribute history in typeapi.Histories) { sb.AppendFormat("<small>{0}, {1}, {2}</small><br/>", history.Date, history.User, history.Info); } // 接口清单 sb.AppendFormat("<br/>"); sb.AppendFormat("<table class='table table-sm table-hover'>"); sb.AppendFormat("<thead><tr>"); sb.AppendFormat("<td>{0}</td>", Resources.Name); sb.AppendFormat("<td>{0}</td>", Resources.Description); sb.AppendFormat("<td>{0}</td>", Resources.ReturnType); sb.AppendFormat("<td>{0}</td>", Resources.CacheSeconds); sb.AppendFormat("<td>{0}</td>", Resources.AuthIP); sb.AppendFormat("<td>{0}</td>", Resources.AuthToken); sb.AppendFormat("<td>{0}</td>", Resources.AuthLogin); sb.AppendFormat("<td>{0}</td>", Resources.AuthUser); sb.AppendFormat("<td>{0}</td>", Resources.AuthRole); sb.AppendFormat("<td>{0}</td>", Resources.AuthVerbs); sb.AppendFormat("<td>{0}</td>", Resources.Log); sb.AppendFormat("<td>{0}</td>", Resources.Status); sb.AppendFormat("<td>{0}</td>", Resources.Remark); sb.AppendFormat("</tr></thead>"); foreach (var api in typeapi.Apis) { sb.AppendFormat("<tr>"); sb.AppendFormat("<td><a target='_blank' href='{0}'>{1} </a></td>", api.Url, api.Name); sb.AppendFormat("<td>{0} </td>", api.Description); sb.AppendFormat("<td>{0} </td>", api.ReturnType); sb.AppendFormat("<td>{0} </td>", api.CacheDuration); sb.AppendFormat("<td>{0} </td>", api.AuthIP); sb.AppendFormat("<td>{0} </td>", api.AuthToken); sb.AppendFormat("<td>{0} </td>", api.AuthLogin); sb.AppendFormat("<td>{0} </td>", api.AuthUsers); sb.AppendFormat("<td>{0} </td>", api.AuthRoles); sb.AppendFormat("<td>{0} </td>", api.AuthVerbs); sb.AppendFormat("<td>{0} </td>", api.Log); sb.AppendFormat("<td>{0} </td>", api.Status); sb.AppendFormat("<td>{0} </td>", api.Remark); sb.AppendFormat("</tr>"); } sb.AppendLine("</table>"); return(sb.ToString()); }
//----------------------------------------------------------- // 生成接口页面 //----------------------------------------------------------- /// <summary> /// 构造接口清单页面 /// </summary> static string BuildApiListHtml(TypeAPI typeapi) { // 概述信息 StringBuilder sb = new StringBuilder(); sb.AppendLine("<h1>" + typeapi.Description + "</h1>"); foreach (HistoryAttribute history in typeapi.Histories) { sb.AppendFormat("<small>{0}, {1}, {2}</small><br/>", history.Date, history.User, history.Info); } // 接口清单 sb.AppendLine("<table border=1 style='border-collapse: collapse' width='100%' cellpadding='2' cellspacing='0'>"); sb.AppendLine(@"<tr> <td width='200'>接口名</td> <td width='200'>说明</td> <td width='70'>返回类型</td> <td width='70'>缓存(秒)</td> <td width='70'>校验IP</td> <td width='80'>校验安全码</td> <td width='70'>校验登录</td> <td width='70'>校验用户</td> <td width='70'>校验角色</td> <td width='70'>校验动作</td> <td width='70'>访问日志</td> <td width='70'>状态</td> <td>备注</td> </tr>"); foreach (var api in typeapi.Apis) { sb.AppendFormat("<tr>"); sb.AppendFormat("<td><a target='_blank' href='{0}'>{1} </a></td>", api.Url, api.Name); sb.AppendFormat("<td>{0} </td>", api.Description); sb.AppendFormat("<td>{0} </td>", api.ReturnType); sb.AppendFormat("<td>{0} </td>", api.CacheDuration); sb.AppendFormat("<td>{0} </td>", api.AuthIP); sb.AppendFormat("<td>{0} </td>", api.AuthSecurityCode); sb.AppendFormat("<td>{0} </td>", api.AuthLogin); sb.AppendFormat("<td>{0} </td>", api.AuthUsers); sb.AppendFormat("<td>{0} </td>", api.AuthRoles); sb.AppendFormat("<td>{0} </td>", api.AuthVerbs); sb.AppendFormat("<td>{0} </td>", api.Log); sb.AppendFormat("<td>{0} </td>", api.Status); sb.AppendFormat("<td>{0} </td>", api.Remark); sb.AppendFormat("</tr>"); } sb.AppendLine("</table>"); return(sb.ToString()); }
//----------------------------------------------------------- // 获取接口清单 //----------------------------------------------------------- // 获取接口清单 static TypeAPI GetTypeApi(Type type) { // 获取接口列表 var rootUrl = GetApiRootUrl(type); var typeapi = new TypeAPI(); var apis = new List <API>(); var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (MethodInfo method in methods) { HttpApiAttribute attr = ReflectHelper.GetHttpApiAttribute(method); if (attr != null) { var api = new API() { Name = method.Name, Description = attr.Description, ReturnType = ParseDataType(attr.Type, method.ReturnType).ToString(), CacheDuration = attr.CacheSeconds, AuthIP = attr.AuthIP, AuthToken = attr.AuthToken, AuthLogin = attr.AuthLogin, AuthUsers = attr.AuthUsers, AuthRoles = attr.AuthRoles, AuthVerbs = attr.AuthVerbs.IsEmpty() ? "" : attr.AuthVerbs.ToUpper(), Status = attr.Status, Log = attr.Log, Remark = attr.Remark, Example = attr.Example, Url = GetMethodDisplayUrl(rootUrl, method), UrlTest = GetMethodTestUrl(rootUrl, method, attr.AuthToken), Params = GetMethodParams(method, attr.AuthToken), Method = method, RType = attr.Type }; apis.Add(api); } } // typeapi.Apis = apis.OrderBy(t => t.Name).ToList(); typeapi.Description = ReflectHelper.GetDescription(type); typeapi.Histories = ReflectHelper.GetHistories(type); return(typeapi); }