mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-01 22:56:36 +08:00
完善各层
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
using CC.ElectronicCommerce.Model;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
//using Yi.Framework.Model;
|
||||
//using Microsoft.AspNetCore.Authentication;
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Security.Claims;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore
|
||||
{
|
||||
public static class CommonExtend
|
||||
{
|
||||
public static bool IsAjaxRequest(this HttpRequest request)
|
||||
{
|
||||
string header = request.Headers["X-Requested-With"];
|
||||
return "XMLHttpRequest".Equals(header);
|
||||
}
|
||||
//namespace CC.ElectronicCommerce.WebCore
|
||||
//{
|
||||
// public static class CommonExtend
|
||||
// {
|
||||
// public static bool IsAjaxRequest(this HttpRequest request)
|
||||
// {
|
||||
// string header = request.Headers["X-Requested-With"];
|
||||
// return "XMLHttpRequest".Equals(header);
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public static UserInfo GetCurrentUserInfo(this HttpContext httpContext)
|
||||
{
|
||||
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
return new UserInfo()
|
||||
{
|
||||
id = long.Parse(claimlist.FirstOrDefault(u => u.Type == "id").Value),
|
||||
username = claimlist.FirstOrDefault(u => u.Type == "username").Value ?? "匿名"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 基于HttpContext,当前鉴权方式解析,获取用户信息
|
||||
// /// </summary>
|
||||
// /// <param name="httpContext"></param>
|
||||
// /// <returns></returns>
|
||||
// public static UserInfo GetCurrentUserInfo(this HttpContext httpContext)
|
||||
// {
|
||||
// IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
// return new UserInfo()
|
||||
// {
|
||||
// id = long.Parse(claimlist.FirstOrDefault(u => u.Type == "id").Value),
|
||||
// username = claimlist.FirstOrDefault(u => u.Type == "username").Value ?? "匿名"
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
using CC.ElectronicCommerce.Core;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
@@ -6,7 +6,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
using Yi.Framework.Common.Models;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.FilterExtend
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -1,156 +1,156 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
//using Microsoft.AspNetCore.Builder;
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.IO;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持在返回HTML时,将返回的Stream保存到指定目录
|
||||
/// </summary>
|
||||
public class StaticPageMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private string _directoryPath = @"D:/cc-ec/";
|
||||
private bool _supportDelete = false;
|
||||
private bool _supportWarmup = false;
|
||||
//namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 支持在返回HTML时,将返回的Stream保存到指定目录
|
||||
// /// </summary>
|
||||
// public class StaticPageMiddleware
|
||||
// {
|
||||
// private readonly RequestDelegate _next;
|
||||
// private string _directoryPath = @"D:/cc-ec/";
|
||||
// private bool _supportDelete = false;
|
||||
// private bool _supportWarmup = false;
|
||||
|
||||
public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup)
|
||||
{
|
||||
this._next = next;
|
||||
this._directoryPath = directoryPath;
|
||||
this._supportDelete = supportDelete;
|
||||
this._supportWarmup = supportWarmup;
|
||||
}
|
||||
// public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup)
|
||||
// {
|
||||
// this._next = next;
|
||||
// this._directoryPath = directoryPath;
|
||||
// this._supportDelete = supportDelete;
|
||||
// this._supportWarmup = supportWarmup;
|
||||
// }
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.DeleteHmtl(context.Request.Path.Value);
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.ClearDirectory(10);//考虑数据量
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (!context.Request.IsAjaxRequest())
|
||||
{
|
||||
Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}");
|
||||
#region context.Response.Body
|
||||
var originalStream = context.Response.Body;
|
||||
using (var copyStream = new MemoryStream())
|
||||
{
|
||||
context.Response.Body = copyStream;
|
||||
await _next(context);
|
||||
// public async Task InvokeAsync(HttpContext context)
|
||||
// {
|
||||
// if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"]))
|
||||
// {
|
||||
// this.DeleteHmtl(context.Request.Path.Value);
|
||||
// context.Response.StatusCode = 200;
|
||||
// }
|
||||
// else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"]))
|
||||
// {
|
||||
// this.ClearDirectory(10);//考虑数据量
|
||||
// context.Response.StatusCode = 200;
|
||||
// }
|
||||
// else if (!context.Request.IsAjaxRequest())
|
||||
// {
|
||||
// Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}");
|
||||
// #region context.Response.Body
|
||||
// var originalStream = context.Response.Body;
|
||||
// using (var copyStream = new MemoryStream())
|
||||
// {
|
||||
// context.Response.Body = copyStream;
|
||||
// await _next(context);
|
||||
|
||||
copyStream.Position = 0;
|
||||
var reader = new StreamReader(copyStream);
|
||||
var content = await reader.ReadToEndAsync();
|
||||
string url = context.Request.Path.Value;
|
||||
// copyStream.Position = 0;
|
||||
// var reader = new StreamReader(copyStream);
|
||||
// var content = await reader.ReadToEndAsync();
|
||||
// string url = context.Request.Path.Value;
|
||||
|
||||
this.SaveHmtl(url, content);
|
||||
// this.SaveHmtl(url, content);
|
||||
|
||||
copyStream.Position = 0;
|
||||
await copyStream.CopyToAsync(originalStream);
|
||||
context.Response.Body = originalStream;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
// copyStream.Position = 0;
|
||||
// await copyStream.CopyToAsync(originalStream);
|
||||
// context.Response.Body = originalStream;
|
||||
// }
|
||||
// #endregion
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// await _next(context);
|
||||
// }
|
||||
// }
|
||||
|
||||
private void SaveHmtl(string url, string html)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine($"Response: {html}");
|
||||
if (string.IsNullOrWhiteSpace(html))
|
||||
return;
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
// private void SaveHmtl(string url, string html)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// //Console.WriteLine($"Response: {html}");
|
||||
// if (string.IsNullOrWhiteSpace(html))
|
||||
// return;
|
||||
// if (!url.EndsWith(".html"))
|
||||
// return;
|
||||
|
||||
if (Directory.Exists(_directoryPath) == false)
|
||||
Directory.CreateDirectory(_directoryPath);
|
||||
// if (Directory.Exists(_directoryPath) == false)
|
||||
// Directory.CreateDirectory(_directoryPath);
|
||||
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.WriteAllText(totalPath, html);//直接覆盖
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
// var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
// File.WriteAllText(totalPath, html);//直接覆盖
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine(ex.Message);
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 删除某个页面
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="index"></param>
|
||||
private void DeleteHmtl(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.Delete(totalPath);//直接删除
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Delete {url} 异常,{ex.Message}");
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 删除某个页面
|
||||
// /// </summary>
|
||||
// /// <param name="url"></param>
|
||||
// /// <param name="index"></param>
|
||||
// private void DeleteHmtl(string url)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// if (!url.EndsWith(".html"))
|
||||
// return;
|
||||
// var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
// File.Delete(totalPath);//直接删除
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"Delete {url} 异常,{ex.Message}");
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 清理文件,支持重试
|
||||
/// </summary>
|
||||
/// <param name="index">最多重试次数</param>
|
||||
private void ClearDirectory(int index)
|
||||
{
|
||||
if (index > 0)//简陋版---重试index次
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(_directoryPath);
|
||||
foreach (var file in files)
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ClearDirectory failed {ex.Message}");
|
||||
ClearDirectory(index--);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 清理文件,支持重试
|
||||
// /// </summary>
|
||||
// /// <param name="index">最多重试次数</param>
|
||||
// private void ClearDirectory(int index)
|
||||
// {
|
||||
// if (index > 0)//简陋版---重试index次
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var files = Directory.GetFiles(_directoryPath);
|
||||
// foreach (var file in files)
|
||||
// {
|
||||
// File.Delete(file);
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"ClearDirectory failed {ex.Message}");
|
||||
// ClearDirectory(index--);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 扩展中间件
|
||||
/// </summary>
|
||||
public static class StaticPageMiddlewareExtensions
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="directoryPath">文件写入地址,文件夹目录</param>
|
||||
/// <param name="supportDelete">是否支持删除</param>
|
||||
/// <param name="supportClear">是否支持全量删除</param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear)
|
||||
{
|
||||
return app.UseMiddleware<StaticPageMiddleware>(directoryPath, supportDelete, supportClear);
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// <summary>
|
||||
// /// 扩展中间件
|
||||
// /// </summary>
|
||||
// public static class StaticPageMiddlewareExtensions
|
||||
// {
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// /// <param name="app"></param>
|
||||
// /// <param name="directoryPath">文件写入地址,文件夹目录</param>
|
||||
// /// <param name="supportDelete">是否支持删除</param>
|
||||
// /// <param name="supportClear">是否支持全量删除</param>
|
||||
// /// <returns></returns>
|
||||
// public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear)
|
||||
// {
|
||||
// return app.UseMiddleware<StaticPageMiddleware>(directoryPath, supportDelete, supportClear);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -5,7 +5,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user