简化使用
This commit is contained in:
@@ -1,80 +1,80 @@
|
|||||||
using System;
|
using Nancy.Swagger.Annotations.Attributes;
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using 电子展板.Base;
|
using 电子展板.Base;
|
||||||
using 电子展板.Utility.Swagger;
|
|
||||||
|
|
||||||
namespace 电子展板.Models
|
namespace 电子展板.Models
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置类
|
/// 配置类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SwaggerClassComment("配置项")]
|
[Model("配置项")]
|
||||||
public class MyConfig : ModelBase
|
public class MyConfig : ModelBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 时间
|
/// 时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("时间")]
|
[Comment("时间")]
|
||||||
[SwaggerComment("时间", true)]
|
[ModelProperty(Description = "时间", Required = true)]
|
||||||
public string Time { get; set; }
|
public string Time { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 时间字体大小
|
/// 时间字体大小
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("时间字体大小")]
|
[Comment("时间字体大小")]
|
||||||
[SwaggerComment("时间字体大小", true, 15, 60)]
|
[ModelProperty(Description = "时间字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int TimeSize { get; set; }
|
public int TimeSize { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 负责人
|
/// 负责人
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("负责人")]
|
[Comment("负责人")]
|
||||||
[SwaggerComment("负责人", true)]
|
[ModelProperty(Description = "负责人", Required = true)]
|
||||||
public string Charger { get; set; }
|
public string Charger { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 负责人字体大小
|
/// 负责人字体大小
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("负责人字体大小")]
|
[Comment("负责人字体大小")]
|
||||||
[SwaggerComment("负责人字体大小", true, 15, 60)]
|
[ModelProperty(Description = "负责人字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int ChargerSize { get; set; }
|
public int ChargerSize { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 风险等级
|
/// 风险等级
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("风险等级")]
|
[Comment("风险等级")]
|
||||||
[SwaggerComment("风险等级", true)]
|
[ModelProperty(Description = "风险等级", Required = true)]
|
||||||
public string RiskLevel { get; set; }
|
public string RiskLevel { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 风险等级字体大小
|
/// 风险等级字体大小
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("风险等级字体大小")]
|
[Comment("风险等级字体大小")]
|
||||||
[SwaggerComment("风险等级字体大小", true, 15, 60)]
|
[ModelProperty(Description = "风险等级字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int RiskLevelSize { get; set; }
|
public int RiskLevelSize { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 工作内容
|
/// 工作内容
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("工作内容")]
|
[Comment("工作内容")]
|
||||||
[SwaggerComment("工作内容", true)]
|
[ModelProperty(Description = "工作内容", Required = true)]
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 工作内容字体大小
|
/// 工作内容字体大小
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("工作内容字体大小")]
|
[Comment("工作内容字体大小")]
|
||||||
[SwaggerComment("工作内容字体大小", true, 15, 60)]
|
[ModelProperty(Description = "工作内容字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int ContentSize { get; set; }
|
public int ContentSize { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 危险点
|
/// 危险点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("危险点")]
|
[Comment("危险点")]
|
||||||
[SwaggerComment("危险点", true)]
|
[ModelProperty(Description = "危险点", Required = true)]
|
||||||
public string DangerPoint { get; set; }
|
public string DangerPoint { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 危险点
|
/// 危险点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("危险点字体大小")]
|
[Comment("危险点字体大小")]
|
||||||
[SwaggerComment("危险点字体大小", true, 15, 60)]
|
[ModelProperty(Description = "危险点字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int DangerPointSize { get; set; }
|
public int DangerPointSize { get; set; }
|
||||||
|
|
||||||
|
|
||||||
@@ -82,40 +82,41 @@ namespace 电子展板.Models
|
|||||||
/// 重点措施
|
/// 重点措施
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Comment("重点措施")]
|
[Comment("重点措施")]
|
||||||
[SwaggerComment("重点措施", true)]
|
[ModelProperty(Description = "重点措施", Required = true)]
|
||||||
public string Measures { get; set; }
|
public string Measures { get; set; }
|
||||||
|
|
||||||
[Comment("重点措施字体大小")]
|
[Comment("重点措施字体大小")]
|
||||||
|
[ModelProperty(Description = "重点措施字体大小", Required = true)]
|
||||||
public int MeasuresSize { get; set; }
|
public int MeasuresSize { get; set; }
|
||||||
|
|
||||||
[Comment("党员1图片路径")]
|
[Comment("党员1图片路径")]
|
||||||
[SwaggerComment("党员1图片路径", true)]
|
[ModelProperty(Description = "党员1图片路径", Required = true)]
|
||||||
public string CPCMember1Path { get; set; }
|
public string CPCMember1Path { get; set; }
|
||||||
[Comment("党员1姓名")]
|
[Comment("党员1姓名")]
|
||||||
[SwaggerComment("党员1姓名", true)]
|
[ModelProperty(Description = "党员1姓名", Required = true)]
|
||||||
public string CPCMember1Name { get; set; }
|
public string CPCMember1Name { get; set; }
|
||||||
[Comment("党员2图片路径")]
|
[Comment("党员2图片路径")]
|
||||||
[SwaggerComment("党员2图片路径", true)]
|
[ModelProperty(Description = "党员2图片路径", Required = true)]
|
||||||
public string CPCMember2Path { get; set; }
|
public string CPCMember2Path { get; set; }
|
||||||
[Comment("党员2姓名")]
|
[Comment("党员2姓名")]
|
||||||
[SwaggerComment("党员2姓名", true)]
|
[ModelProperty(Description = "党员2姓名", Required = true)]
|
||||||
public string CPCMember2Name { get; set; }
|
public string CPCMember2Name { get; set; }
|
||||||
[Comment("党员3图片路径")]
|
[Comment("党员3图片路径")]
|
||||||
[SwaggerComment("党员3图片路径", true)]
|
[ModelProperty(Description = "党员3图片路径", Required = true)]
|
||||||
public string CPCMember3Path { get; set; }
|
public string CPCMember3Path { get; set; }
|
||||||
|
|
||||||
[Comment("党员3姓名")]
|
[Comment("党员3姓名")]
|
||||||
[SwaggerComment("党员3姓名", true)]
|
[ModelProperty(Description = "党员3姓名", Required = true)]
|
||||||
public string CPCMember3Name { get; set; }
|
public string CPCMember3Name { get; set; }
|
||||||
[Comment("党员4图片路径")]
|
[Comment("党员4图片路径")]
|
||||||
[SwaggerComment("党员4图片路径", true)]
|
[ModelProperty(Description = "党员4图片路径", Required = true)]
|
||||||
public string CPCMember4Path { get; set; }
|
public string CPCMember4Path { get; set; }
|
||||||
[Comment("党员4姓名")]
|
[Comment("党员4姓名")]
|
||||||
[SwaggerComment("党员4姓名", true)]
|
[ModelProperty(Description = "党员4姓名", Required = true)]
|
||||||
public string CPCMember4Name { get; set; }
|
public string CPCMember4Name { get; set; }
|
||||||
|
|
||||||
[Comment("党员字体大小")]
|
[Comment("党员字体大小")]
|
||||||
[SwaggerComment("党员字体大小", true, 15, 60)]
|
[ModelProperty(Description = "党员字体大小", Required = true, Minimum = 15, Maximum = 60)]
|
||||||
public int CPCMemberSize { get; set; }
|
public int CPCMemberSize { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转换成JSON字符串
|
/// 转换成JSON字符串
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
using System;
|
using Nancy.Swagger.Annotations.Attributes;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using 电子展板.Utility.Swagger;
|
|
||||||
|
|
||||||
namespace 电子展板.Models
|
namespace 电子展板.Models
|
||||||
{
|
{
|
||||||
[SwaggerClassComment("返回数据")]
|
[Model("返回数据")]
|
||||||
public class RetValue
|
public class RetValue<T>
|
||||||
{
|
{
|
||||||
[SwaggerComment("返回状态")]
|
[ModelProperty("返回状态")]
|
||||||
public int state { get; set; }
|
public int state { get; set; }
|
||||||
[SwaggerComment("返回消息")]
|
[ModelProperty("返回消息")]
|
||||||
public string message { get; set; }
|
public string message { get; set; }
|
||||||
[SwaggerComment("返回数据")]
|
|
||||||
public object data { get; set; }
|
[ModelProperty("返回数据")]
|
||||||
|
public T data { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace 电子展板.Utility
|
|
||||||
{
|
|
||||||
public class ContentTypeUtils
|
|
||||||
{
|
|
||||||
public static string GetContentType(string fileName)
|
|
||||||
{
|
|
||||||
return System.Web.MimeMapping.GetMimeMapping(fileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace 电子展板.Utility.Swagger
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
|
||||||
public class SwaggerClassCommentAttribute : Attribute
|
|
||||||
{
|
|
||||||
public string Comment { get; set; }
|
|
||||||
public SwaggerClassCommentAttribute(string comment)
|
|
||||||
{
|
|
||||||
Comment = comment;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace 电子展板.Utility.Swagger
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
|
||||||
public class SwaggerCommentAttribute : Attribute
|
|
||||||
{
|
|
||||||
public string Comment { get; set; }
|
|
||||||
public bool? IsRequired { get; set; }
|
|
||||||
|
|
||||||
public long? Min { get; set; }
|
|
||||||
public long? Max { get; set; }
|
|
||||||
public SwaggerCommentAttribute()
|
|
||||||
{
|
|
||||||
Comment = "";
|
|
||||||
}
|
|
||||||
public SwaggerCommentAttribute(string comment)
|
|
||||||
{
|
|
||||||
Comment = comment;
|
|
||||||
}
|
|
||||||
public SwaggerCommentAttribute(string comment, bool isRequired)
|
|
||||||
{
|
|
||||||
Comment = comment;
|
|
||||||
IsRequired = isRequired;
|
|
||||||
}
|
|
||||||
public SwaggerCommentAttribute(string comment, bool isRequired, long min, long max)
|
|
||||||
{
|
|
||||||
Comment = comment;
|
|
||||||
IsRequired = isRequired;
|
|
||||||
Min = min;
|
|
||||||
Max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
using Nancy.Swagger;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace 电子展板.Utility.Swagger
|
|
||||||
{
|
|
||||||
public static class SwaggerModelDataUtils
|
|
||||||
{
|
|
||||||
public static SwaggerModelData GetModelData<T>()
|
|
||||||
{
|
|
||||||
return SwaggerModelData.ForType<T>(with =>
|
|
||||||
{
|
|
||||||
Type type = typeof(T);
|
|
||||||
SwaggerClassCommentAttribute classComment = type.GetCustomAttribute<SwaggerClassCommentAttribute>();
|
|
||||||
with.Description(classComment != null ? classComment.Comment : type.Name);
|
|
||||||
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
|
|
||||||
foreach (PropertyInfo propertyInfo in propertyInfos)
|
|
||||||
{
|
|
||||||
SwaggerModelPropertyData ss = with.Data.Properties.Where(x => x.Name == propertyInfo.Name).FirstOrDefault();
|
|
||||||
SwaggerCommentAttribute attribute = propertyInfo.GetCustomAttribute<SwaggerCommentAttribute>();
|
|
||||||
if (attribute == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!string.IsNullOrEmpty(attribute.Comment))
|
|
||||||
ss.Description = attribute.Comment;
|
|
||||||
if (attribute.IsRequired != null)
|
|
||||||
ss.Required = attribute.IsRequired.Value;
|
|
||||||
if (attribute.Min != null)
|
|
||||||
ss.Minimum = attribute.Min.Value;
|
|
||||||
if (attribute.Max != null)
|
|
||||||
ss.Maximum = attribute.Max.Value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
using Nancy;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace 电子展板.Utility.Swagger
|
|
||||||
{
|
|
||||||
public class SwaggerModule : NancyModule
|
|
||||||
{
|
|
||||||
public SwaggerModule()
|
|
||||||
{
|
|
||||||
//全局静态资源
|
|
||||||
Get("/swagger", RedirectToIndex);
|
|
||||||
Get("/swagger/index.html", Index);
|
|
||||||
Get("/swagger/{name*}", StaticResource);
|
|
||||||
}
|
|
||||||
|
|
||||||
private object RedirectToIndex(dynamic dynamic)
|
|
||||||
{
|
|
||||||
return Response.AsRedirect("/swagger/index.html");
|
|
||||||
}
|
|
||||||
|
|
||||||
private object Index(dynamic dynamic)
|
|
||||||
{
|
|
||||||
var url = $"{Request.Url.BasePath}/api-docs";
|
|
||||||
string packUri = $"pack://application:,,,/Assets/SwaggerUI/index.html";
|
|
||||||
string contentType = System.Web.MimeMapping.GetMimeMapping(packUri);
|
|
||||||
byte[] bytes = null;
|
|
||||||
using (var stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream)
|
|
||||||
{
|
|
||||||
bytes = new byte[stream.Length];
|
|
||||||
stream.Read(bytes, 0, bytes.Length);
|
|
||||||
}
|
|
||||||
string html = Encoding.UTF8.GetString(bytes);
|
|
||||||
html = html.Replace("@url", url);
|
|
||||||
return Response.AsText(html, contentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
private object StaticResource(dynamic dynamic)
|
|
||||||
{
|
|
||||||
string name = dynamic.name;
|
|
||||||
return Resouce(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
private object Resouce(string resourcePath)
|
|
||||||
{
|
|
||||||
if (resourcePath == "")
|
|
||||||
resourcePath = "index.html";
|
|
||||||
string packUri = $"pack://application:,,,/Assets/SwaggerUI/{resourcePath}";
|
|
||||||
string contentType = System.Web.MimeMapping.GetMimeMapping(packUri);
|
|
||||||
return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using Nancy;
|
using Nancy;
|
||||||
using Nancy.Bootstrapper;
|
using Nancy.Bootstrapper;
|
||||||
using Nancy.Conventions;
|
using Nancy.Conventions;
|
||||||
|
using Nancy.Swagger.Annotations;
|
||||||
using Nancy.Swagger.Services;
|
using Nancy.Swagger.Services;
|
||||||
using Nancy.TinyIoc;
|
using Nancy.TinyIoc;
|
||||||
using Swagger.ObjectModel;
|
using Swagger.ObjectModel;
|
||||||
@@ -18,6 +19,8 @@ namespace 电子展板.WebModule
|
|||||||
Url = "https://www.cnblogs.com/zjwno1"
|
Url = "https://www.cnblogs.com/zjwno1"
|
||||||
}, "http://www.cnblogs.com/zjwno1");
|
}, "http://www.cnblogs.com/zjwno1");
|
||||||
base.ApplicationStartup(container, pipelines);
|
base.ApplicationStartup(container, pipelines);
|
||||||
|
//仅显示有特性的接口
|
||||||
|
SwaggerAnnotationsConfig.ShowOnlyAnnotatedRoutes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ConfigureConventions(NancyConventions nancyConventions)
|
protected override void ConfigureConventions(NancyConventions nancyConventions)
|
||||||
|
|||||||
@@ -1,104 +0,0 @@
|
|||||||
using Nancy.Metadata.Modules;
|
|
||||||
using Nancy.Swagger;
|
|
||||||
using Swagger.ObjectModel;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using 电子展板.Models;
|
|
||||||
using 电子展板.Utility.Core;
|
|
||||||
|
|
||||||
namespace 电子展板.WebModule
|
|
||||||
{
|
|
||||||
public class HomeMetadataModule : MetadataModule<PathItem>
|
|
||||||
{
|
|
||||||
public HomeMetadataModule(ISwaggerModelCatalog modelCatalog)
|
|
||||||
{
|
|
||||||
//添加模型
|
|
||||||
modelCatalog.AddModels(typeof(MyConfig));
|
|
||||||
Describe["getConfig"] = description => description.AsSwagger(
|
|
||||||
with => with.Operation(
|
|
||||||
op =>
|
|
||||||
{
|
|
||||||
//设置操作id
|
|
||||||
op.OperationId("getConfig");
|
|
||||||
//设置tag
|
|
||||||
op.Tag("getConfig");
|
|
||||||
//备注
|
|
||||||
op.Summary("获得当前配置");
|
|
||||||
//参数配置
|
|
||||||
//op.BodyParameter(p =>
|
|
||||||
// p.Description("配置项")
|
|
||||||
// .Name("Config")
|
|
||||||
// .Schema<MyConfig>()
|
|
||||||
//);
|
|
||||||
//返回值配置
|
|
||||||
op.Response(r =>
|
|
||||||
r.Description("返回数据")
|
|
||||||
//.Example("application/json", JsonHelper.ToJson(GlobalVariable.Config))
|
|
||||||
.Schema<MyConfig>()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Describe["uploadImage"] = description => description.AsSwagger(
|
|
||||||
with => with.Operation(
|
|
||||||
op =>
|
|
||||||
{
|
|
||||||
//设置操作id
|
|
||||||
op.OperationId("uploadImage");
|
|
||||||
//设置tag
|
|
||||||
op.Tag("uploadImage");
|
|
||||||
//备注
|
|
||||||
op.Summary("上传照片");
|
|
||||||
//文件上传
|
|
||||||
op.ConsumeMimeType("multipart/form-data");
|
|
||||||
//参数配置
|
|
||||||
op.Parameter(p =>
|
|
||||||
p.In(ParameterIn.Form)
|
|
||||||
.Name("file")//参数名
|
|
||||||
.Type("file")//文件类型
|
|
||||||
.Description("上传文件")
|
|
||||||
.IsRequired()
|
|
||||||
);
|
|
||||||
//返回值配置
|
|
||||||
op.Response(r =>
|
|
||||||
r.Description("返回数据")
|
|
||||||
.Example("application/json", new RetValue { state = 1, message = "上传成功", data = "/upload/123.png" }.ToJson())
|
|
||||||
.Schema<RetValue>()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Describe["save"] = description => description.AsSwagger(
|
|
||||||
with => with.Operation(
|
|
||||||
op =>
|
|
||||||
{
|
|
||||||
//设置操作id
|
|
||||||
op.OperationId("save");
|
|
||||||
//设置tag
|
|
||||||
op.Tag("save");
|
|
||||||
//备注
|
|
||||||
op.Summary("保存配置");
|
|
||||||
//参数配置
|
|
||||||
op.BodyParameter(p =>
|
|
||||||
p.Description("配置项")
|
|
||||||
.Name("Config")
|
|
||||||
.Schema<MyConfig>()
|
|
||||||
);
|
|
||||||
//返回值配置
|
|
||||||
op.Response(r =>
|
|
||||||
r.Description("返回数据")
|
|
||||||
.Example("application/json", new RetValue { state = 1, message = "保存成功" }.ToJson())
|
|
||||||
.Schema<RetValue>()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
using Nancy;
|
using Nancy;
|
||||||
using Nancy.ModelBinding;
|
using Nancy.ModelBinding;
|
||||||
|
using Nancy.Swagger;
|
||||||
|
using Nancy.Swagger.Annotations.Attributes;
|
||||||
|
using Swagger.ObjectModel;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -19,8 +22,14 @@ namespace 电子展板.WebModule
|
|||||||
{
|
{
|
||||||
public class HomeModule : NancyModule
|
public class HomeModule : NancyModule
|
||||||
{
|
{
|
||||||
public HomeModule()
|
public HomeModule(ISwaggerModelCatalog modelCatalog)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//涉及到哪些Model
|
||||||
|
modelCatalog.AddModel<MyConfig>();
|
||||||
|
modelCatalog.AddModel<RetValue<string>>();
|
||||||
|
|
||||||
|
|
||||||
//默认页面
|
//默认页面
|
||||||
Get("", Index);
|
Get("", Index);
|
||||||
Get("/", Index);
|
Get("/", Index);
|
||||||
@@ -33,26 +42,51 @@ namespace 电子展板.WebModule
|
|||||||
//获得上传的文件
|
//获得上传的文件
|
||||||
Get("/upload/{id}", GetUploadFiles);
|
Get("/upload/{id}", GetUploadFiles);
|
||||||
//上传图片
|
//上传图片
|
||||||
Post("/uploadImage", UploadImage, null, "uploadImage");
|
Post("/uploadImage", dynamic =>
|
||||||
|
{
|
||||||
|
IEnumerable<HttpFile> files = Request.Files;
|
||||||
|
if (files == null || files.Count() == 0)
|
||||||
|
return UploadImage(null);
|
||||||
|
return UploadImage(files.FirstOrDefault());
|
||||||
|
}, null, "uploadImage");
|
||||||
//保存配置
|
//保存配置
|
||||||
Post("/save", SaveConfig, null, "save");
|
Post("/save", SaveConfig, null, "save");
|
||||||
}
|
}
|
||||||
//默认页面
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 默认页面
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dynamic"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private object Index(dynamic dynamic)
|
private object Index(dynamic dynamic)
|
||||||
{
|
{
|
||||||
return Resouce("Views/Index.html");
|
return Resouce("Views/Index.html");
|
||||||
}
|
}
|
||||||
//全局静态资源
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 全局静态资源
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dynamic"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private object StaticResource(dynamic dynamic)
|
private object StaticResource(dynamic dynamic)
|
||||||
{
|
{
|
||||||
string name = dynamic.name;
|
string name = dynamic.name;
|
||||||
return Resouce(name);
|
return Resouce(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得配置
|
/// 获得配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dynamic"></param>
|
/// <param name="dynamic"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
[Route("getConfig")]//Name
|
||||||
|
[Route(HttpMethod.Get, "/getConfig")]//Method And Path
|
||||||
|
[Route(Summary = "获得配置数据")]
|
||||||
|
[SwaggerResponse(HttpStatusCode.OK, typeof(MyConfig))]
|
||||||
|
[Route(Tags = new[] { "获得配置数据" })]
|
||||||
private object GetConfig(dynamic dynamic)
|
private object GetConfig(dynamic dynamic)
|
||||||
{
|
{
|
||||||
//保留JSON KEY首字母大写,这里就不用 Response.AsJson();
|
//保留JSON KEY首字母大写,这里就不用 Response.AsJson();
|
||||||
@@ -70,7 +104,7 @@ namespace 电子展板.WebModule
|
|||||||
private object GetUploadFiles(dynamic dynamic)
|
private object GetUploadFiles(dynamic dynamic)
|
||||||
{
|
{
|
||||||
string id = dynamic.id;
|
string id = dynamic.id;
|
||||||
string contentType = System.Web.MimeMapping.GetMimeMapping(id);
|
string contentType = MimeTypes.GetMimeType(id);
|
||||||
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
|
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,16 +113,19 @@ namespace 电子展板.WebModule
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dynamic"></param>
|
/// <param name="dynamic"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private object UploadImage(dynamic dynamic)
|
[Route("uploadImage")]//Name
|
||||||
|
[Route(HttpMethod.Post, "/uploadImage")]//Method And Path
|
||||||
|
[Route(Summary = "上传图片")]
|
||||||
|
[SwaggerResponse(HttpStatusCode.OK, typeof(RetValue<string>))]
|
||||||
|
[Route(Tags = new[] { "上传图片" })]
|
||||||
|
private object UploadImage([RouteParam(ParamIn = ParameterIn.Form, Name = "file", Description = "图片文件", ParamType = typeof(SwaggerFile), Required = true)] HttpFile file)
|
||||||
{
|
{
|
||||||
string uploadPath = MyEnvironment.Root("/Upload/");
|
string uploadPath = MyEnvironment.Root("/Upload/");
|
||||||
if (!Directory.Exists(uploadPath))
|
if (!Directory.Exists(uploadPath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(uploadPath);
|
Directory.CreateDirectory(uploadPath);
|
||||||
}
|
}
|
||||||
List<string> filePathList = new List<string>();
|
|
||||||
foreach (var file in Request.Files)
|
|
||||||
{
|
|
||||||
string ext = Path.GetExtension(file.Name);
|
string ext = Path.GetExtension(file.Name);
|
||||||
string fileName = UUID.StrSnowId + ext;
|
string fileName = UUID.StrSnowId + ext;
|
||||||
var filePath = uploadPath + fileName;
|
var filePath = uploadPath + fileName;
|
||||||
@@ -96,12 +133,16 @@ namespace 电子展板.WebModule
|
|||||||
{
|
{
|
||||||
file.Value.CopyTo(fileStream);
|
file.Value.CopyTo(fileStream);
|
||||||
}
|
}
|
||||||
filePathList.Add($"/Upload/{fileName}");
|
RetValue<string> result = new RetValue<string> { state = 1, message = "上传成功", data = $"/upload/{fileName}" };
|
||||||
}
|
|
||||||
RetValue result = new RetValue { state = 1, message = "上传成功", data = filePathList.GetStrArray() };
|
|
||||||
return Response.AsJson(result);
|
return Response.AsJson(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Route("save")]//Name
|
||||||
|
[Route(HttpMethod.Post, "/save")]//Method And Path
|
||||||
|
[Route(Summary = "保存配置")]
|
||||||
|
[SwaggerResponse(HttpStatusCode.OK, typeof(RetValue<string>))]
|
||||||
|
[Route(Tags = new[] { "保存配置" })]
|
||||||
private object SaveConfig(dynamic dynamic)
|
private object SaveConfig(dynamic dynamic)
|
||||||
{
|
{
|
||||||
MyConfig config = this.Bind<MyConfig>();
|
MyConfig config = this.Bind<MyConfig>();
|
||||||
@@ -119,13 +160,13 @@ namespace 电子展板.WebModule
|
|||||||
GlobalVariable.SaveConfig();
|
GlobalVariable.SaveConfig();
|
||||||
//保存并修改界面
|
//保存并修改界面
|
||||||
EventBus.Instance.Publish("save", "");
|
EventBus.Instance.Publish("save", "");
|
||||||
RetValue result = new RetValue { state = 1, message = "保存成功" };
|
RetValue<string> result = new RetValue<string> { state = 1, message = "保存成功" };
|
||||||
return Response.AsJson(result);
|
return Response.AsJson(result);
|
||||||
}
|
}
|
||||||
private object Resouce(string resourcePath)
|
private object Resouce(string resourcePath)
|
||||||
{
|
{
|
||||||
string packUri = $"pack://application:,,,/Assets/{resourcePath}";
|
string packUri = $"pack://application:,,,/Assets/{resourcePath}";
|
||||||
string contentType = System.Web.MimeMapping.GetMimeMapping(packUri);
|
string contentType = MimeTypes.GetMimeType(packUri);
|
||||||
return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType);
|
return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
using Nancy.Swagger;
|
|
||||||
using Nancy.Swagger.Services;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using 电子展板.Models;
|
|
||||||
using 电子展板.Utility.Swagger;
|
|
||||||
|
|
||||||
namespace 电子展板.WebModule.ModelMetadata
|
|
||||||
{
|
|
||||||
public class MyConfigModelDataProvider : ISwaggerModelDataProvider
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 使用自定义特性注解完成配置
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public SwaggerModelData GetModelData()
|
|
||||||
{
|
|
||||||
return SwaggerModelDataUtils.GetModelData<MyConfig>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
using Nancy.Swagger;
|
|
||||||
using Nancy.Swagger.Services;
|
|
||||||
using 电子展板.Models;
|
|
||||||
using 电子展板.Utility.Swagger;
|
|
||||||
|
|
||||||
namespace 电子展板.WebModule.ModelMetadata
|
|
||||||
{
|
|
||||||
public class RetValueDataProvider : ISwaggerModelDataProvider
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 使用自定义特性注解完成配置
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public SwaggerModelData GetModelData()
|
|
||||||
{
|
|
||||||
return SwaggerModelDataUtils.GetModelData<RetValue>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -20,10 +20,6 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>-->
|
</PackageReference>-->
|
||||||
<PackageReference Include="HandyControl" Version="3.5.1" />
|
|
||||||
<PackageReference Include="Nancy.Swagger.Alyce" Version="2.2.76" />
|
|
||||||
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
|
|
||||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -35,9 +31,12 @@
|
|||||||
<!-- 日志框架 -->
|
<!-- 日志框架 -->
|
||||||
<PackageReference Include="NLog" Version="6.0.2" />
|
<PackageReference Include="NLog" Version="6.0.2" />
|
||||||
<!-- WEB 自宿主 -->
|
<!-- WEB 自宿主 -->
|
||||||
<Reference Include="System.Web" />
|
<PackageReference Include="ZJW.NancySwagger" Version="2.0.0" />
|
||||||
<PackageReference Include="Nancy" Version="2.0.0" />
|
|
||||||
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
|
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
|
||||||
|
<!-- 界面UI-->
|
||||||
|
<PackageReference Include="HandyControl" Version="3.5.1" />
|
||||||
|
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="Upload\0.jpg">
|
<Content Include="Upload\0.jpg">
|
||||||
|
|||||||
Reference in New Issue
Block a user