简化使用
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Nancy;
|
||||
using Nancy.Bootstrapper;
|
||||
using Nancy.Conventions;
|
||||
using Nancy.Swagger.Annotations;
|
||||
using Nancy.Swagger.Services;
|
||||
using Nancy.TinyIoc;
|
||||
using Swagger.ObjectModel;
|
||||
@@ -18,6 +19,8 @@ namespace 电子展板.WebModule
|
||||
Url = "https://www.cnblogs.com/zjwno1"
|
||||
}, "http://www.cnblogs.com/zjwno1");
|
||||
base.ApplicationStartup(container, pipelines);
|
||||
//仅显示有特性的接口
|
||||
SwaggerAnnotationsConfig.ShowOnlyAnnotatedRoutes = true;
|
||||
}
|
||||
|
||||
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.ModelBinding;
|
||||
using Nancy.Swagger;
|
||||
using Nancy.Swagger.Annotations.Attributes;
|
||||
using Swagger.ObjectModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@@ -19,8 +22,14 @@ namespace 电子展板.WebModule
|
||||
{
|
||||
public class HomeModule : NancyModule
|
||||
{
|
||||
public HomeModule()
|
||||
public HomeModule(ISwaggerModelCatalog modelCatalog)
|
||||
{
|
||||
|
||||
//涉及到哪些Model
|
||||
modelCatalog.AddModel<MyConfig>();
|
||||
modelCatalog.AddModel<RetValue<string>>();
|
||||
|
||||
|
||||
//默认页面
|
||||
Get("", Index);
|
||||
Get("/", Index);
|
||||
@@ -33,26 +42,51 @@ namespace 电子展板.WebModule
|
||||
//获得上传的文件
|
||||
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");
|
||||
}
|
||||
//默认页面
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 默认页面
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <returns></returns>
|
||||
private object Index(dynamic dynamic)
|
||||
{
|
||||
return Resouce("Views/Index.html");
|
||||
}
|
||||
//全局静态资源
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 全局静态资源
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <returns></returns>
|
||||
private object StaticResource(dynamic dynamic)
|
||||
{
|
||||
string name = dynamic.name;
|
||||
return Resouce(name);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获得配置
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <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)
|
||||
{
|
||||
//保留JSON KEY首字母大写,这里就不用 Response.AsJson();
|
||||
@@ -70,7 +104,7 @@ namespace 电子展板.WebModule
|
||||
private object GetUploadFiles(dynamic dynamic)
|
||||
{
|
||||
string id = dynamic.id;
|
||||
string contentType = System.Web.MimeMapping.GetMimeMapping(id);
|
||||
string contentType = MimeTypes.GetMimeType(id);
|
||||
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
|
||||
}
|
||||
|
||||
@@ -79,29 +113,36 @@ namespace 电子展板.WebModule
|
||||
/// </summary>
|
||||
/// <param name="dynamic"></param>
|
||||
/// <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/");
|
||||
if (!Directory.Exists(uploadPath))
|
||||
{
|
||||
Directory.CreateDirectory(uploadPath);
|
||||
}
|
||||
List<string> filePathList = new List<string>();
|
||||
foreach (var file in Request.Files)
|
||||
|
||||
string ext = Path.GetExtension(file.Name);
|
||||
string fileName = UUID.StrSnowId + ext;
|
||||
var filePath = uploadPath + fileName;
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
string ext = Path.GetExtension(file.Name);
|
||||
string fileName = UUID.StrSnowId + ext;
|
||||
var filePath = uploadPath + fileName;
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
file.Value.CopyTo(fileStream);
|
||||
}
|
||||
filePathList.Add($"/Upload/{fileName}");
|
||||
file.Value.CopyTo(fileStream);
|
||||
}
|
||||
RetValue result = new RetValue { state = 1, message = "上传成功", data = filePathList.GetStrArray() };
|
||||
RetValue<string> result = new RetValue<string> { state = 1, message = "上传成功", data = $"/upload/{fileName}" };
|
||||
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)
|
||||
{
|
||||
MyConfig config = this.Bind<MyConfig>();
|
||||
@@ -119,13 +160,13 @@ namespace 电子展板.WebModule
|
||||
GlobalVariable.SaveConfig();
|
||||
//保存并修改界面
|
||||
EventBus.Instance.Publish("save", "");
|
||||
RetValue result = new RetValue { state = 1, message = "保存成功" };
|
||||
RetValue<string> result = new RetValue<string> { state = 1, message = "保存成功" };
|
||||
return Response.AsJson(result);
|
||||
}
|
||||
private object Resouce(string 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user