155 lines
5.2 KiB
C#
155 lines
5.2 KiB
C#
using Nancy;
|
||
using Nancy.ModelBinding;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using 电子展板.Base;
|
||
using 电子展板.Utility;
|
||
using 电子展板.Utility.Core;
|
||
using 电子展板.Utility.Extension;
|
||
using 电子展板.Utility.Logs;
|
||
using 电子展板.Utility.Other;
|
||
|
||
namespace 电子展板
|
||
{
|
||
public class WebModule : NancyModule
|
||
{
|
||
public WebModule()
|
||
{
|
||
//默认页面
|
||
Get("", Index);
|
||
Get("/", Index);
|
||
Get("/index.html", Index);
|
||
//全局静态资源
|
||
Get("/static/{name*}", StaticResource);
|
||
|
||
//获得配置
|
||
Get("/getConfig", GetConfig);
|
||
//获得上传的文件
|
||
Get("/upload/{id}", GetUploadFiles);
|
||
//上传图片
|
||
Post("/uploadImage", UploadImage);
|
||
//保存配置
|
||
Post("/save", SaveConfig);
|
||
}
|
||
//默认页面
|
||
private object Index(dynamic dynamic)
|
||
{
|
||
return Resouce("Views/Index.html");
|
||
}
|
||
//全局静态资源
|
||
private object StaticResource(dynamic dynamic)
|
||
{
|
||
string name = dynamic.name;
|
||
return Resouce(name);
|
||
}
|
||
/// <summary>
|
||
/// 获得配置
|
||
/// </summary>
|
||
/// <param name="dynamic"></param>
|
||
/// <returns></returns>
|
||
private object GetConfig(dynamic dynamic)
|
||
{
|
||
//保留JSON KEY首字母大写,这里就不用 Response.AsJson();
|
||
//AsJson会将首字母小写
|
||
return Response.AsText(JsonHelper.ToJson(GlobalVariable.Config), "application/json", Encoding.UTF8);
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 获得上传的文件
|
||
/// </summary>
|
||
/// <param name="dynamic"></param>
|
||
/// <returns></returns>
|
||
private object GetUploadFiles(dynamic dynamic)
|
||
{
|
||
string id = dynamic.id;
|
||
string contentType = System.Web.MimeMapping.GetMimeMapping(id);
|
||
return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上传图片
|
||
/// </summary>
|
||
/// <param name="dynamic"></param>
|
||
/// <returns></returns>
|
||
private object UploadImage(dynamic dynamic)
|
||
{
|
||
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))
|
||
{
|
||
file.Value.CopyTo(fileStream);
|
||
}
|
||
filePathList.Add($"/Upload/{fileName}");
|
||
}
|
||
var result = new { state = 1, message = "上传成功", data = filePathList.GetStrArray() };
|
||
return Response.AsJson(result);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 保存配置
|
||
/// </summary>
|
||
/// <param name="dynamic"></param>
|
||
/// <returns></returns>
|
||
private object SaveConfig(dynamic dynamic)
|
||
{
|
||
MyConfig config = this.Bind<MyConfig>();
|
||
//string str = Request.Body.AsString();
|
||
|
||
//MyConfig config = JsonHelper.ToObject<MyConfig>(str);
|
||
LogHelper.Instance.Info($"用户保存了配置:{JsonHelper.ToJson(config)}");
|
||
//保存
|
||
PropertyInfo[] propertyInfos = typeof(MyConfig).GetProperties();
|
||
foreach (PropertyInfo propertyInfo in propertyInfos)
|
||
{
|
||
object value = propertyInfo.GetValue(config);
|
||
propertyInfo.SetValue(GlobalVariable.Config, value);
|
||
}
|
||
GlobalVariable.SaveConfig();
|
||
//保存并修改界面
|
||
EventBus.Instance.Publish("save", "");
|
||
var result = new { 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);
|
||
return Response.FromStream(System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream, contentType);
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
//public class ApplicationBootstrapper : DefaultNancyBootstrapper
|
||
//{
|
||
// protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
|
||
// {
|
||
// SwaggerMetadataProvider.SetInfo("API", "v1.0", "电子展板", new Swagger.ObjectModel.Contact
|
||
// {
|
||
// EmailAddress = "18862253202@qq.com",
|
||
// Name = "大师兄法号随缘",
|
||
// Url = "https://www.cnblogs.com/zjwno1"
|
||
// });
|
||
|
||
// base.ApplicationStartup(container, pipelines);
|
||
// }
|
||
//}
|
||
}
|