From 52cb8e9d1363585733dbe9c0855b64e9877d3261 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 07 九月 2023 11:33:51 +0800 Subject: [PATCH] 添加出图接口~ --- MoonExp/App_Start/WebApiConfig.cs | 30 ++++++- MoonExp/Web.config | 4 MoonExp/MoonExp.csproj | 10 +- MoonExp/Controllers/ExportController.cs | 54 +++++++++++++ MoonExp/export.html | 101 +++++++++++++++++++++++++ MoonExp/cs/NoBufferPolicySelector.cs | 27 ++++++ 6 files changed, 217 insertions(+), 9 deletions(-) diff --git a/MoonExp/App_Start/WebApiConfig.cs b/MoonExp/App_Start/WebApiConfig.cs index 64b208b..e243fcc 100644 --- a/MoonExp/App_Start/WebApiConfig.cs +++ b/MoonExp/App_Start/WebApiConfig.cs @@ -1,7 +1,10 @@ -锘縰sing System; +锘縰sing MoonExp.cs; +using Newtonsoft.Json.Converters; +using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; +using System.Web.Http.Hosting; namespace MoonExp { @@ -9,16 +12,35 @@ { public static void Register(HttpConfiguration config) { - // Web API configuration and services - // Web API routes config.MapHttpAttributeRoutes(); + //config.Routes.MapHttpRoute( + // name: "TerraApi", + // routeTemplate: "{controller}/3d/terrain", + // defaults: new { controller = "Terra" } + //); + + // {controller}/{id} config.Routes.MapHttpRoute( name: "DefaultApi", - routeTemplate: "api/{controller}/{id}", + routeTemplate: "{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); + + // 娉ㄥ唽鎵╁睍涓绘満缂撳瓨Policy + GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector()); + + // 灞忚斀杩斿洖XML鏍煎紡 + GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); + + // 瑙e喅webapi鍦� multipart/form-data 鏃犳硶鏄犲皠瀹炰綋绫伙紝HttpContext.Request.Files.Count + //GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter()); + + // 淇敼鏃ユ湡鏍煎紡 + IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); + timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; + GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(timeConverter); } } } diff --git a/MoonExp/Controllers/ExportController.cs b/MoonExp/Controllers/ExportController.cs new file mode 100644 index 0000000..502503a --- /dev/null +++ b/MoonExp/Controllers/ExportController.cs @@ -0,0 +1,54 @@ +锘縰sing MoonExp.cs; +using MoonExp.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace MoonExp.Controllers +{ + public class ExportController : ApiController + { + [HttpGet] + public string Test() + { + return DateTime.Now.ToString("yyyyy-MM-dd HH:mm:ss"); + } + + [HttpPost] + public ResponseMsg<string> Start([FromBody]ExportArgs args) + { + try + { + if (args == null || + string.IsNullOrWhiteSpace(args.layers) || + string.IsNullOrWhiteSpace(args.token) || + (args.xmin == 0 && args.ymin == 0 && args.xmax == 0 && args.ymax == 0)) + { + return ResponseMsg<string>.fail("鍙傛暟涓嶅畬鏁�"); + } + + if (!ExportUtil.VerifyToken(args.token)) + { + return ResponseMsg<string>.fail("浠ょ墝鏃犳晥"); + } + + string err = null; + string imgPath = ExportUtil.Generate(args, ref err); + if (string.IsNullOrEmpty(imgPath)) + { + return ResponseMsg<string>.fail(null == err ? "鐢熸垚澶辫触" : err); + } + + return ResponseMsg<string>.success("鎴愬姛", imgPath); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return ResponseMsg<string>.fail(ex.Message); + } + } + } +} diff --git a/MoonExp/MoonExp.csproj b/MoonExp/MoonExp.csproj index 2de8452..d991491 100644 --- a/MoonExp/MoonExp.csproj +++ b/MoonExp/MoonExp.csproj @@ -14,7 +14,7 @@ <RootNamespace>MoonExp</RootNamespace> <AssemblyName>MoonExp</AssemblyName> <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> - <UseIISExpress>true</UseIISExpress> + <UseIISExpress>false</UseIISExpress> <IISExpressSSLPort /> <IISExpressAnonymousAuthentication /> <IISExpressWindowsAuthentication /> @@ -81,13 +81,16 @@ </Reference> </ItemGroup> <ItemGroup> + <Content Include="export.html" /> <Content Include="Global.asax" /> <Content Include="Web.config" /> </ItemGroup> <ItemGroup> <Compile Include="App_Start\WebApiConfig.cs" /> + <Compile Include="Controllers\ExportController.cs" /> <Compile Include="cs\ExportUtil.cs" /> <Compile Include="cs\LogOut.cs" /> + <Compile Include="cs\NoBufferPolicySelector.cs" /> <Compile Include="cs\PostgreHelper.cs" /> <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> @@ -104,7 +107,6 @@ </ItemGroup> <ItemGroup> <Folder Include="App_Data\" /> - <Folder Include="Controllers\" /> </ItemGroup> <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> @@ -119,9 +121,9 @@ <WebProjectProperties> <UseIIS>True</UseIIS> <AutoAssignPort>True</AutoAssignPort> - <DevelopmentServerPort>0</DevelopmentServerPort> + <DevelopmentServerPort>1062</DevelopmentServerPort> <DevelopmentServerVPath>/</DevelopmentServerVPath> - <IISUrl>http://localhost:1380/</IISUrl> + <IISUrl>http://localhost/MoonExp</IISUrl> <NTLMAuthentication>False</NTLMAuthentication> <UseCustomServer>False</UseCustomServer> <CustomServerUrl> diff --git a/MoonExp/Web.config b/MoonExp/Web.config index 544389a..9c1b819 100644 --- a/MoonExp/Web.config +++ b/MoonExp/Web.config @@ -10,7 +10,9 @@ </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.2" /> - <httpRuntime targetFramework="4.5.2" /> + <!-- maxRequestLength鏄笂浼犳枃浠舵椂鏈�澶ize(鍗曚綅鏄疜B,榛樿鍊兼槸4096kb)锛宔xecutionTimeout鏄姹俿ession(鍗曚綅鏄,榛樿鍊兼槸110s) --> + <httpRuntime maxRequestLength="2147483647" executionTimeout="8640000"/> + <pages controlRenderingCompatibilityVersion="4.0"/> </system.web> <system.webServer> <security> diff --git a/MoonExp/cs/NoBufferPolicySelector.cs b/MoonExp/cs/NoBufferPolicySelector.cs new file mode 100644 index 0000000..6492dfd --- /dev/null +++ b/MoonExp/cs/NoBufferPolicySelector.cs @@ -0,0 +1,27 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Web; +using System.Web.Http.WebHost; + +namespace MoonExp.cs +{ + public class NoBufferPolicySelector : WebHostBufferPolicySelector + { + public override bool UseBufferedInputStream(object hostContext) + { + var context = hostContext as HttpContextBase; + + if (context != null) + { + if (context.Request.HttpMethod == HttpMethod.Post.ToString() && context.Request.ContentLength > 102400) + { + return false; + } + } + + return true; + } + } +} diff --git a/MoonExp/export.html b/MoonExp/export.html new file mode 100644 index 0000000..5dfb91a --- /dev/null +++ b/MoonExp/export.html @@ -0,0 +1,101 @@ +锘�<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>MoonExp</title> + <meta http-equiv="Expires" content="0" /> + <meta http-equiv="Cache" content="no-cache" /> + <meta http-equiv="Pragma" content="no-cache" /> + <meta http-equiv="Cache-control" content="no-cache" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <script src="js/jquery.1.12.4.js"></script> + <script> + var token = "384d7382-ea48-400b-bc92-58cb2b28bcbc"; + + $(function () { + $("#token").html(token); + }); + + // Ajax + function ajax(url, type, data, dataType, contentType, fn) { + $.ajax({ + url: url, + type: type, + data: data, + dataType: dataType || "json", // html銆乯son銆乯sonp銆乻cript銆乼ext + contentType: contentType || "application/json", // "application/x-www-form-urlencoded" + success: function (data) { + fn(data); + }, + error: function (e) { + console.error(e); + fn(); + } + }); + } + + // 鍑哄浘-1 + function exportMap1() { + var page = $("#selectPage").find("option:selected").text(); + var data = { + token: token, + title: "绠¢亾鍩虹澶ф暟鎹钩鍙帮紙DW1-01锛�", + pageSize: page, + province: "娌冲寳鐪�", + scale: "1锛�5涓�", + resolution: "5绫�", + date: "2022.11.09", + layers: "m_hydraulic_protection,m_marker,m_pipeline,m_sitepoint,m_valvehousepoint,m_crossing,楂樺痉鐭㈤噺", + rotation: 0, + xmin: 103.394023, + ymin: 30.690466, + ymax: 37.871048, + xmax: 112.534648 + }; + + ajax("Export/Start", "POST", JSON.stringify(data), null, null, function (rs) { + alert("code = " + rs.code + ", msg = " + rs.msg + ", result = " + rs.result); + console.log(rs); + }); + } + + // 鍑哄浘-2 + function exportMap2() { + var page = $("#selectPage").find("option:selected").text(); + var data = { + token: token, + title: "绠¢亾鍩虹澶ф暟鎹钩鍙帮紙DW1-02锛�", + pageSize: page, + province: "灞变笢鐪�", + scale: "1锛�1涓�", + resolution: "2绫�", + date: "2022.11.10", + layers: "bs_project,th_strategic_channel,m_hydraulic_protection,m_marker,m_pipeline,m_sitepoint,m_valvehousepoint,m_crossing,楂樺痉褰卞儚", + rotation: 0, + xmin: 103.394023, + ymin: 30.690466, + ymax: 37.871048, + xmax: 112.534648 + }; + + ajax("Export/Start", "POST", JSON.stringify(data), null, null, function (rs) { + alert("code = " + rs.code + ", msg = " + rs.msg + ", result = " + rs.result); + console.log(rs); + }); + } + </script> +</head> +<body> + <h2>Toekn锛�<span id="token" style="font-size: 14px;"></span></h2> + <select id="selectPage"> + <option>A0</option> + <option>A1</option> + <option>A2</option> + <option>A3</option> + <option selected="selected">A4</option> + </select> + <input type="button" value="鍑哄浘-1" onclick="exportMap1()" /> + <input type="button" value="鍑哄浘-2" onclick="exportMap2()" /> + <br /><br /> +</body> +</html> -- Gitblit v1.9.3