diff --git a/电子展板/Base/GlobalVariable.cs b/电子展板/Base/GlobalVariable.cs index 782724a..67d9f75 100644 --- a/电子展板/Base/GlobalVariable.cs +++ b/电子展板/Base/GlobalVariable.cs @@ -1,4 +1,5 @@ -using System; +using Nancy.Hosting.Self; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -11,6 +12,8 @@ namespace 电子展板.Base { public class GlobalVariable { + public static NancyHost WebServer; + private static MyConfig _config; /// /// 配置数据 diff --git a/电子展板/Controller/BaseController.cs b/电子展板/Controller/BaseController.cs deleted file mode 100644 index dac3e5f..0000000 --- a/电子展板/Controller/BaseController.cs +++ /dev/null @@ -1,207 +0,0 @@ -using JinianNet.JNTemplate; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Pipes; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Runtime.InteropServices.ComTypes; -using System.Text; -using System.Web; -using System.Web.Http; -using System.Web.Http.Results; -using System.Xml.Linq; -using 电子展板.Utility.Core; -using 电子展板.Utility.Extension; -using 电子展板.Utility.ResponseModels; - - -namespace 电子展板.Controller -{ - - public class BaseController : ApiController - { - protected ActionResult Content(string content) - { - return new ActionResult { Content = new StringContent(content, Encoding.UTF8, "text/plain") }; - } - - protected ActionResult ContentType(string content, string contentType) - { - return new ActionResult { Content = new StringContent(content, Encoding.UTF8, contentType) }; - } - - public new ActionResult Redirect(string url) - { - ActionResult resp = new ActionResult(HttpStatusCode.Moved); - string originalString = Request.RequestUri.OriginalString; - string pathAndQuery = Request.RequestUri.PathAndQuery; - string baseUrl = originalString.Substring(0, originalString.IndexOf(pathAndQuery)); - resp.Headers.Location = new Uri(baseUrl + url); - return resp; - } - - protected ActionResult File(string virtualPath, string contentType) - { - return File(virtualPath, contentType, "HelloWorld"); - } - - protected ActionResult File(string virtualPath, string contentType, string fileDownloadName) - { - string physicalPath = MyEnvironment.WebRootPath(virtualPath); - return PhysicalFile(physicalPath, contentType, fileDownloadName); - } - - protected ActionResult PhysicalFile(string physicalPath, string contentType) - { - return PhysicalFile(physicalPath, contentType, "HelloWorld"); - } - - protected ActionResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName) - { - return File(System.IO.File.OpenRead(physicalPath), contentType, fileDownloadName); - } - - - protected ActionResult File(byte[] fileContents, string contentType) - { - return File(fileContents, contentType, "HelloWorld"); - } - - - protected ActionResult File(byte[] fileContents, string contentType, string fileDownloadName) - { - ActionResult response = new ActionResult(HttpStatusCode.OK); - response.Content = new ByteArrayContent(fileContents); - response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); - response.Content.Headers.ContentLength = fileContents.Length; - response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") - { - FileName = HttpUtility.UrlEncode(fileDownloadName) - }; - response.Headers.Add("Access-Control-Expose-Headers", "FileName"); - response.Headers.Add("FileName", HttpUtility.UrlEncode(fileDownloadName)); - return response; - } - - protected ActionResult File(Stream fileStream, string contentType) - { - return File(fileStream, contentType, "HelloWorld"); - } - - protected ActionResult File(Stream fileStream, string contentType, string fileDownloadName) - { - ActionResult response = new ActionResult(HttpStatusCode.OK); - response.Content = new StreamContent(fileStream); - response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); - response.Content.Headers.ContentLength = fileStream.Length; - response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") - { - FileName = HttpUtility.UrlEncode(fileDownloadName) - }; - response.Headers.Add("Access-Control-Expose-Headers", "FileName"); - response.Headers.Add("FileName", HttpUtility.UrlEncode(fileDownloadName)); - return response; - } - #region 快捷方法 - protected ActionResult Success(string message = "恭喜您,操作成功。", object data = null) - { - return Content(new AjaxResult(ResultType.Success, message, data).ToJson()); - } - protected ActionResult Error(string message = "对不起,操作失败。", object data = null) - { - return Content(new AjaxResult(ResultType.Error, message, data).ToJson()); - } - protected ActionResult Warning(string message, object data = null) - { - return Content(new AjaxResult(ResultType.Warning, message, data).ToJson()); - } - protected ActionResult Info(string message, object data = null) - { - return Content(new AjaxResult(ResultType.Info, message, data).ToJson()); - } - - - public ActionResult Json(object obj) - { - return new ActionResult { Content = new StringContent(obj.ToJson(), Encoding.UTF8, "application/json") }; - } - - - - public ActionResult Text(string str) - { - //return Content(str, "text/plain"); - return new ActionResult { Content = new System.Net.Http.StringContent(str, Encoding.UTF8, "text/plain") }; - - } - - public ActionResult HtmlStr(string str) - { - return new ActionResult { Content = new System.Net.Http.StringContent(str, Encoding.UTF8, "text/html") }; - - } - - public ActionResult Html(string view) - { - return Html(view, null); - } - public ActionResult Resouce(string viewName, string contentType) - { - string packUri = $"pack://application:,,,/Assets{viewName}"; - byte[] bytes = null; - using (Stream stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream) - { - bytes = new byte[stream.Length]; - stream.Read(bytes, 0, bytes.Length); - } - ActionResult response = new ActionResult(HttpStatusCode.OK); - response.Content = new ByteArrayContent(bytes); - response.Content.Headers.ContentEncoding.Add("utf-8"); - response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType); - response.Content.Headers.ContentLength = bytes.Length; - return response; - } - - - public ActionResult Html(string view, Dictionary dict) - { - if (!view.StartsWith("/")) - view = "/" + view; - string path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "Views"; - - - var template = Engine.LoadTemplate(path + view + ".html"); - - - if (dict != null) - { - foreach (KeyValuePair keyValue in dict) - { - template.Set(keyValue.Key, keyValue.Value); - } - } - var result = template.Render(); - // return Content(result, "text/html"); - return new ActionResult { Content = new System.Net.Http.StringContent(result, Encoding.UTF8, "text/html") }; - - } - #endregion - } -} - -namespace Microsoft.AspNetCore.Mvc -{ - public class ActionResult : System.Net.Http.HttpResponseMessage - { - public ActionResult() : base(HttpStatusCode.OK) - { - - } - public ActionResult(HttpStatusCode statusCode) : base(statusCode) - { - } - } -} \ No newline at end of file diff --git a/电子展板/Controller/IndexController.cs b/电子展板/Controller/IndexController.cs deleted file mode 100644 index 1706630..0000000 --- a/电子展板/Controller/IndexController.cs +++ /dev/null @@ -1,105 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Net.Http; -using System.Reflection; -using System.Web.Http; -using 电子展板.Base; -using 电子展板.Utility; -using 电子展板.Utility.Core; -using 电子展板.Utility.Extension; -using 电子展板.Utility.Logs; -using 电子展板.Utility.Other; -using 电子展板.Utility.Web; - -namespace 电子展板.Controller -{ - public class IndexController : BaseController - { - /// - /// 主界面 - /// - /// - [HttpGet, Route(""), Route("index.html")] - public ActionResult Index() - { - return Resouce("/Views/Index.html", "text/html"); - } - - /// - /// 获得配置信息 - /// - /// - [HttpGet, Route("getConfig")] - public ActionResult GetConfig() - { - return Json(GlobalVariable.Config); - } - - - /// - /// 上传图片。 - /// - /// - [HttpPost, Route("uploadImage")] - public ActionResult UploadImage() - { - if (!Request.Content.IsMimeMultipartContent()) - { - throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); - } - string path = MyEnvironment.Root("/Upload"); - var provider = new WithExtensionMultipartFormDataStreamProvider(path, UUID.StrSnowId); - var fileData = Request.Content.ReadAsMultipartAsync(provider).Result; - if (fileData.FileData.Count == 0) - { - return Error(); - } - var file = fileData.FileData[0]; - string virtualPath = "/Upload/" + Path.GetFileName(file.LocalFileName); - return Success("上传成功", virtualPath); - } - - - /// - /// 图形显示 - /// - /// - /// - [HttpGet, Route("Upload/{id}")] - public ActionResult GetUploadFile([FromUri] string id) - { - string filePath = MyEnvironment.Root($"/Upload/{id}"); - string contentType = System.Web.MimeMapping.GetMimeMapping(filePath); - return PhysicalFile(filePath, contentType, id); - } - - - /// - /// 保存配置文件 - /// - /// - /// - [HttpPost, Route("save")] - public ActionResult Save([FromBody] MyConfig config) - { - 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", ""); - return Success(); - } - - - - } -} diff --git a/电子展板/MainWindow.xaml.cs b/电子展板/MainWindow.xaml.cs index 2eda3d4..dc98c8c 100644 --- a/电子展板/MainWindow.xaml.cs +++ b/电子展板/MainWindow.xaml.cs @@ -1,6 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -21,6 +19,6 @@ namespace 电子展板 public MainWindow() { InitializeComponent(); - } + } } } \ No newline at end of file diff --git a/电子展板/Utility/ResponseModels/AjaxResult.cs b/电子展板/Utility/ResponseModels/AjaxResult.cs deleted file mode 100644 index d77306e..0000000 --- a/电子展板/Utility/ResponseModels/AjaxResult.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace 电子展板.Utility.ResponseModels -{ - /// - /// 通用AJAX请求响应数据格式模型。 - /// - public class AjaxResult - { - public AjaxResult(ResultType state, string message, object data = null) - { - this.state = state; - this.message = message; - this.data = data; - } - /// - /// 结果类型。 - /// - public ResultType state { get; set; } - /// - /// 消息内容。 - /// - public string message { get; set; } - /// - /// 返回数据。 - /// - public object data { get; set; } - } - - /// - /// 结果类型枚举。 - /// - public enum ResultType - { - /// - /// 警告。 - /// - Warning = 0, - - /// - /// 成功。 - /// - Success = 1, - - /// - /// 异常。 - /// - Error = 2, - - /// - /// 消息。 - /// - Info = 6 - } -} \ No newline at end of file diff --git a/电子展板/Utility/ResponseModels/LayNavbar.cs b/电子展板/Utility/ResponseModels/LayNavbar.cs deleted file mode 100644 index dc519cf..0000000 --- a/电子展板/Utility/ResponseModels/LayNavbar.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace 电子展板.Utility.ResponseModels -{ - - /// - /// 菜单视图模型。 - /// - public class LayNavbar - { - /// - /// 标题 - /// - public string title { get; set; } - /// - /// 图标 - /// - public string icon { get; set; } - /// - /// 是否展开 - /// - public bool spread { get; set; } - /// - /// 子级菜单集合 - /// - public List children { get; set; } - } - - /// - /// 子级菜单模型。 - /// - public class LayChildNavbar - { - /// - /// 标题 - /// - public string title { get; set; } - /// - /// 图标 - /// - public string icon { get; set; } - /// - /// 链接 - /// - public string href { get; set; } - } -} diff --git a/电子展板/Utility/ResponseModels/LayPadding.cs b/电子展板/Utility/ResponseModels/LayPadding.cs deleted file mode 100644 index 2551849..0000000 --- a/电子展板/Utility/ResponseModels/LayPadding.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace 电子展板.Utility.ResponseModels -{ - /// - /// Laytpl + Laypage 分页模型。 - /// - /// - public class LayPadding where TEntity : class - { - public int code { get; set; } - - /// - /// 获取结果。 - /// - public bool result { get; set; } - - /// - /// 备注信息。 - /// - public string msg { get; set; } - - /// - /// 数据列表。 - /// - public List list { get; set; } - - public string backgroundImage { get; set; } - /// - /// 记录条数。 - /// - public long count { get; set; } - } -} \ No newline at end of file diff --git a/电子展板/Utility/ResponseModels/LayPaddingDataTable.cs b/电子展板/Utility/ResponseModels/LayPaddingDataTable.cs deleted file mode 100644 index 5f33ba3..0000000 --- a/电子展板/Utility/ResponseModels/LayPaddingDataTable.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace 电子展板.Utility.ResponseModels -{ - public class LayPaddingDataTable - { - public int code { get; set; } - - /// - /// 获取结果。 - /// - public bool result { get; set; } - - /// - /// 备注信息。 - /// - public string msg { get; set; } - - /// - /// 数据列表。 - /// - public DataTable list { get; set; } - - public string backgroundImage { get; set; } - /// - /// 记录条数。 - /// - public long count { get; set; } - } -} diff --git a/电子展板/Utility/ResponseModels/RetStr.cs b/电子展板/Utility/ResponseModels/RetStr.cs deleted file mode 100644 index 4cdcca4..0000000 --- a/电子展板/Utility/ResponseModels/RetStr.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace 电子展板.Utility.ResponseModels -{ - public class RetStr - { - public string Data { get; set; } - } -} diff --git a/电子展板/Utility/ResponseModels/TreeSelect.cs b/电子展板/Utility/ResponseModels/TreeSelect.cs deleted file mode 100644 index a83f1b7..0000000 --- a/电子展板/Utility/ResponseModels/TreeSelect.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Web; -using Newtonsoft.Json; - -namespace 电子展板.Utility.ResponseModels -{ - /// - /// Select2树形下拉列表模型。 - /// - public class TreeSelect - { - public string id { get; set; } - public string text { get; set; } - public string parentId { get; set; } - public object data { get; set; } - } - - public static class TreeSelectHelper - { - public static string ToTreeSelectJson(this List data) - { - StringBuilder sb = new StringBuilder(); - sb.Append("["); - sb.Append(ToTreeSelectJson(data, "0", "")); - sb.Append("]"); - return sb.ToString(); - } - private static string ToTreeSelectJson(List data, string parentId, string blank) - { - StringBuilder sb = new StringBuilder(); - var childList = data.FindAll(t => t.parentId == parentId); - - var tabline = ""; - if (parentId != "0") - { - tabline = "  "; - } - if (childList.Count > 0) - { - tabline = tabline + blank; - } - foreach (TreeSelect entity in childList) - { - entity.text = tabline + entity.text; - string strJson = JsonConvert.SerializeObject(entity); - sb.Append(strJson); - sb.Append(ToTreeSelectJson(data, entity.id, tabline)); - } - return sb.ToString().Replace("}{", "},{"); - } - } -} \ No newline at end of file diff --git a/电子展板/Utility/ResponseModels/ZTreeNode.cs b/电子展板/Utility/ResponseModels/ZTreeNode.cs deleted file mode 100644 index 1ba7004..0000000 --- a/电子展板/Utility/ResponseModels/ZTreeNode.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace 电子展板.Utility.ResponseModels -{ - /// - /// zTree单层节点数据模型。 - /// - public class ZTreeNode - { - /// - /// 节点ID。 - /// - public string id { get; set; } - /// - /// 父节点ID。 - /// - public string pId { get; set; } - /// - /// 节点名称。 - /// - public string name { get; set; } - /// - /// 是否展开。 - /// - public bool open { get; set; } - /// - /// 是否选中。 - /// - public bool @checked { get; set; } - } -} \ No newline at end of file diff --git a/电子展板/Utility/Web/WithExtensionMultipartFormDataStreamProvider.cs b/电子展板/Utility/Web/WithExtensionMultipartFormDataStreamProvider.cs deleted file mode 100644 index 4039260..0000000 --- a/电子展板/Utility/Web/WithExtensionMultipartFormDataStreamProvider.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; - -namespace 电子展板.Utility.Web -{ - - public class WithExtensionMultipartFormDataStreamProvider : MultipartFormDataStreamProvider - { - public string guid { get; set; } - public string OriginalName { get; set; } - public string Ext { get; set; } - - public WithExtensionMultipartFormDataStreamProvider(string rootPath, string guidStr) : base(rootPath) - { - guid = guidStr; - } - - public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers) - { - OriginalName = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? GetValidFileName(headers.ContentDisposition.FileName) : ""; - Ext = !string.IsNullOrWhiteSpace(OriginalName) ? Path.GetExtension(GetValidFileName(OriginalName)) : ""; - return guid + Ext; - } - - private string GetValidFileName(string filePath) - { - char[] invalids = System.IO.Path.GetInvalidFileNameChars(); - return string.Join("_", filePath.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.'); - } - - } -} diff --git a/电子展板/ViewModels/MainWindow2ViewModel.cs b/电子展板/ViewModels/MainWindow2ViewModel.cs index f522309..900ef17 100644 --- a/电子展板/ViewModels/MainWindow2ViewModel.cs +++ b/电子展板/ViewModels/MainWindow2ViewModel.cs @@ -122,7 +122,10 @@ namespace 电子展板.ViewModels /// public void WindowClosing(CancelEventArgs args) { - WebServer.Stop(); + + GlobalVariable.WebServer?.Stop(); + GlobalVariable.WebServer?.Dispose(); + GlobalVariable.WebServer = null; Screen[] screens = Screen.AllScreens; if (screens.Length > 1) { diff --git a/电子展板/ViewModels/MainWindowViewModel.cs b/电子展板/ViewModels/MainWindowViewModel.cs index 6b2ea6b..00fc239 100644 --- a/电子展板/ViewModels/MainWindowViewModel.cs +++ b/电子展板/ViewModels/MainWindowViewModel.cs @@ -1,4 +1,7 @@ -using System; +using Nancy; +using Nancy.Hosting.Self; +using Nancy.Json; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -30,12 +33,15 @@ namespace 电子展板.ViewModels this.window.Top = (int)(Screen.AllScreens[screenIndex].Bounds.Top / GetDpiScaleX()); this.window.Width = (int)(Screen.AllScreens[screenIndex].Bounds.Width / GetDpiScaleX()); this.window.Height = (int)(Screen.AllScreens[screenIndex].Bounds.Height / GetDpiScaleX()); - WebServer.Start(); + GlobalVariable.WebServer = new NancyHost(new Uri("http://localhost:80")); + GlobalVariable.WebServer.Start(); } private void WindowClosing(CancelEventArgs args) { - WebServer.Stop(); + GlobalVariable.WebServer?.Stop(); + GlobalVariable.WebServer?.Dispose(); + GlobalVariable.WebServer = null; Environment.Exit(0); } public double GetDpiScaleX() diff --git a/电子展板/WebModule.cs b/电子展板/WebModule.cs new file mode 100644 index 0000000..56b98ab --- /dev/null +++ b/电子展板/WebModule.cs @@ -0,0 +1,140 @@ +using Nancy; +using Nancy.Extensions; +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); + } + /// + /// 获得配置 + /// + /// + /// + private object GetConfig(dynamic dynamic) + { + //保留JSON KEY首字母大写,这里就不用 Response.AsJson(); + //AsJson会将首字母小写 + return Response.AsText(JsonHelper.ToJson(GlobalVariable.Config), "application/json", Encoding.UTF8); + } + + + + /// + /// 获得上传的文件 + /// + /// + /// + private object GetUploadFiles(dynamic dynamic) + { + string id = dynamic.id; + string contentType = System.Web.MimeMapping.GetMimeMapping(id); + return Response.AsFile(MyEnvironment.Root("/Upload/" + id), contentType); + } + + /// + /// 上传图片 + /// + /// + /// + private object UploadImage(dynamic dynamic) + { + string uploadPath = MyEnvironment.Root("/Upload/"); + if (!Directory.Exists(uploadPath)) + { + Directory.CreateDirectory(uploadPath); + } + List filePathList = new List(); + 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); + } + + + /// + /// 保存配置 + /// + /// + /// + private object SaveConfig(dynamic dynamic) + { + MyConfig config = this.Bind(); + //string str = Request.Body.AsString(); + + //MyConfig config = JsonHelper.ToObject(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); + } + + + + } +} diff --git a/电子展板/WebServer.cs b/电子展板/WebServer.cs deleted file mode 100644 index cbd4210..0000000 --- a/电子展板/WebServer.cs +++ /dev/null @@ -1,126 +0,0 @@ -using Beginor.Owin.StaticFile; -using JinianNet.JNTemplate; -using Microsoft.Extensions.Configuration; -using Microsoft.Owin; -using Microsoft.Owin.Hosting; -using Owin; -using System; -using System.Collections.Generic; -using System.Configuration; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Formatting; -using System.Net.Http.Headers; -using System.Net.Mime; -using System.Text; -using System.Text.Json.Nodes; -using System.Threading; -using System.Threading.Tasks; -using System.Web.Http; -using 电子展板.Utility.Core; -using 电子展板.Utility.Extension; -using 电子展板.Utility.Logs; -using static System.Net.Mime.MediaTypeNames; -using static System.Windows.Forms.VisualStyles.VisualStyleElement; - -namespace 电子展板 -{ - public class WebServer - { - private static IDisposable host; - public static void Start() - { - LogHelper.Instance.Info("正在开启Web服务"); - StartOptions startOptions = new StartOptions(); - startOptions.Urls.Add($"http://*:80/"); - host = WebApp.Start(startOptions); - } - - public static void Stop() - { - if (host == null) - return; - LogHelper.Instance.Info("正在停止Web服务"); - host.Dispose(); - host = null; - } - } - - - /// - /// Web启动类 - /// - public class Startup - { - private HttpConfiguration _config; - /// - /// 配置 - /// - /// - /// - public void Configuration(IAppBuilder app) - { - _config = new HttpConfiguration(); - _config.MapHttpAttributeRoutes(); - _config.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); - _config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json")); - //全局拦截 - app.Use("/static", "/Assets"); - app.UseWebApi(_config); - //配置模板引擎 - Engine.Configure(c => - { - c.TagPrefix = "${"; - c.TagSuffix = "}"; - c.TagFlag = '^'; - }); - } - } - - public class ResouceStaticRoute : OwinMiddleware - { - private string route; - private string resources; - public ResouceStaticRoute(OwinMiddleware next, string route, string resources) : base(next) - { - this.route = route; - this.resources = resources; - } - - public override async Task Invoke(IOwinContext context) - { - try - { - string method = context.Request.Method;//GET POST - string path = context.Request.Path.ToString(); - if (method.ToUpper() == "GET" && path.StartsWith(route)) - { - string resourePath = path.Replace(route, ""); - string packUri = $"pack://application:,,,{resources}{resourePath}"; - byte[] bytes = null; - using (Stream stream = System.Windows.Application.GetResourceStream(new Uri(packUri, UriKind.RelativeOrAbsolute)).Stream) - { - bytes = new byte[stream.Length]; - stream.Read(bytes, 0, bytes.Length); - } - - context.Response.ContentType = System.Web.MimeMapping.GetMimeMapping(path); - context.Response.Write(bytes, 0, bytes.Length); - await Task.FromResult(0); - } - await Next.Invoke(context); - } - catch (InvalidCastException ex) - { - await Next.Invoke(context); - } - catch (Exception ex) - { - await Next.Invoke(context); - } - } - } -} - diff --git a/电子展板/电子展板.csproj b/电子展板/电子展板.csproj index 2650c14..7cfc60f 100644 --- a/电子展板/电子展板.csproj +++ b/电子展板/电子展板.csproj @@ -31,25 +31,16 @@ - - - - - - - - - - - - + + + - - PreserveNewest - + + PreserveNewest +