简化使用

This commit is contained in:
2025-09-24 12:02:13 +08:00
parent dad5b8fb89
commit 4a198f1056
13 changed files with 103 additions and 375 deletions

View File

@@ -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);
}
}