完善各层
This commit is contained in:
BIN
Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db
Normal file
BIN
Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db
Normal file
Binary file not shown.
@@ -5,6 +5,10 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -6,5 +6,8 @@
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"SqliteConn": {
|
||||
"Url": "Filename=YIDB.db"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace CC.ElectronicCommerce.Core
|
||||
namespace Yi.Framework.Core
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using CC.ElectronicCommerce.Common.IOCOptions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System;
|
||||
@@ -7,6 +6,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
|
||||
namespace CC.ElectronicCommerce.Core
|
||||
{
|
||||
|
||||
@@ -4,4 +4,35 @@
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Consul" Version="1.6.10.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
|
||||
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Common\Yi.Framework.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Bcl.AsyncInterfaces">
|
||||
<HintPath>Library\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Common">
|
||||
<HintPath>Library\ServiceStack.Common.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Interfaces">
|
||||
<HintPath>Library\ServiceStack.Interfaces.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Redis">
|
||||
<HintPath>Library\ServiceStack.Redis.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Text">
|
||||
<HintPath>Library\ServiceStack.Text.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
77
Yi.Framework/Yi.Framework.Interface/IBaseService.cs
Normal file
77
Yi.Framework/Yi.Framework.Interface/IBaseService.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Interface
|
||||
{
|
||||
public interface IBaseService<T> where T : class, new()
|
||||
{
|
||||
#region
|
||||
//通过id得到实体
|
||||
#endregion
|
||||
Task<T> GetEntityById(int id);
|
||||
|
||||
#region
|
||||
//得到全部实体
|
||||
#endregion
|
||||
Task<IEnumerable<T>> GetAllEntitiesAsync();
|
||||
|
||||
#region
|
||||
//通过表达式得到实体
|
||||
#endregion
|
||||
Task<IEnumerable<T>> GetEntitiesAsync(Expression<Func<T, bool>> whereLambda);
|
||||
|
||||
#region
|
||||
//通过表达式得到实体,分页版本
|
||||
#endregion
|
||||
Task<int> GetCountAsync(Expression<Func<T, bool>> whereLambda);
|
||||
|
||||
#region
|
||||
//通过表达式统计数量
|
||||
#endregion
|
||||
IQueryable<IGrouping<S, T>> GetGroup<S>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> groupByLambda);
|
||||
|
||||
#region
|
||||
//通过表达式分组
|
||||
#endregion
|
||||
Task<Tuple<IEnumerable<T>, int>> GetPageEntities<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc);
|
||||
|
||||
#region
|
||||
//添加实体
|
||||
#endregion
|
||||
Task<bool> AddAsync(T entity);
|
||||
|
||||
#region
|
||||
//添加多个实体
|
||||
#endregion
|
||||
Task<bool> AddAsync(IEnumerable<T> entities);
|
||||
|
||||
#region
|
||||
//更新实体
|
||||
#endregion
|
||||
Task<bool> UpdateAsync(T entity);
|
||||
|
||||
#region
|
||||
//更新实体部分属性
|
||||
#endregion
|
||||
Task<bool> DeleteAsync(T entity);
|
||||
|
||||
#region
|
||||
//删除实体
|
||||
#endregion
|
||||
Task<bool> DeleteAsync(int id);
|
||||
|
||||
#region
|
||||
//通过id删除实体
|
||||
#endregion
|
||||
Task<bool> DeleteAsync(IEnumerable<int> ids);
|
||||
|
||||
#region
|
||||
//通过id列表删除多个实体
|
||||
#endregion
|
||||
Task<bool> DeleteAsync(Expression<Func<T, bool>> where);
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,12 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
using Yi.Framework.Model.Models;
|
||||
|
||||
namespace Yi.Framework.Model.Models
|
||||
namespace Yi.Framework.Model
|
||||
{
|
||||
//Add-Migration yi-1
|
||||
//Update-Database yi-1
|
||||
public class DataContext : DbContext
|
||||
{
|
||||
private readonly IOptionsMonitor<SqliteOptions> _optionsMonitor;
|
||||
@@ -31,6 +34,6 @@ namespace Yi.Framework.Model.Models
|
||||
optionsBuilder.UseSqlite(_connStr);
|
||||
}
|
||||
}
|
||||
//public virtual DbSet<TbBrand> TbBrand { get; set; }
|
||||
}
|
||||
public virtual DbSet<user> user { get; set; }
|
||||
}
|
||||
}
|
||||
39
Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs
generated
Normal file
39
Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs
generated
Normal file
@@ -0,0 +1,39 @@
|
||||
// <auto-generated />
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Yi.Framework.Model;
|
||||
|
||||
namespace Yi.Framework.Model.Migrations
|
||||
{
|
||||
[DbContext(typeof(DataContext))]
|
||||
[Migration("20211010110842_yi-1")]
|
||||
partial class yi1
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.10");
|
||||
|
||||
modelBuilder.Entity("Yi.Framework.Model.Models.user", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("age")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.ToTable("user");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Yi.Framework.Model.Migrations
|
||||
{
|
||||
public partial class yi1 : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "user",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
name = table.Column<string>(type: "TEXT", nullable: true),
|
||||
age = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_user", x => x.id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "user");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// <auto-generated />
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Yi.Framework.Model;
|
||||
|
||||
namespace Yi.Framework.Model.Migrations
|
||||
{
|
||||
[DbContext(typeof(DataContext))]
|
||||
partial class DataContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.10");
|
||||
|
||||
modelBuilder.Entity("Yi.Framework.Model.Models.user", b =>
|
||||
{
|
||||
b.Property<int>("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("age")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("name")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("id");
|
||||
|
||||
b.ToTable("user");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Yi.Framework/Yi.Framework.Model/Models/user.cs
Normal file
17
Yi.Framework/Yi.Framework.Model/Models/user.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Model.Models
|
||||
{
|
||||
public class user
|
||||
{
|
||||
[Key]
|
||||
public int id { get; set; }
|
||||
public string name { get; set; }
|
||||
public int age { get; set; }
|
||||
}
|
||||
}
|
||||
119
Yi.Framework/Yi.Framework.Service/BaseService.cs
Normal file
119
Yi.Framework/Yi.Framework.Service/BaseService.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Interface;
|
||||
|
||||
namespace Yi.Framework.Service
|
||||
{
|
||||
public class CCBaseServer<T> : IBaseService<T> where T : class, new()
|
||||
{
|
||||
public DbContext _Db;
|
||||
public CCBaseServer(DbContext Db)
|
||||
{
|
||||
_Db = Db;
|
||||
}
|
||||
|
||||
public async Task<T> GetEntityById(int id)
|
||||
{
|
||||
return await _Db.Set<T>().FindAsync(id);
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<T>> GetAllEntitiesAsync()
|
||||
{
|
||||
return await _Db.Set<T>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> GetEntitiesAsync(Expression<Func<T, bool>> whereLambda)
|
||||
{
|
||||
return await _Db.Set<T>().Where(whereLambda).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetCountAsync(Expression<Func<T, bool>> whereLambda) //统计数量
|
||||
{
|
||||
return await _Db.Set<T>().CountAsync(whereLambda);
|
||||
}
|
||||
|
||||
public IQueryable<IGrouping<S, T>> GetGroup<S>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> groupByLambda) //分组
|
||||
{
|
||||
return _Db.Set<T>().Where(whereLambda).GroupBy(groupByLambda).AsQueryable();
|
||||
}
|
||||
|
||||
public async Task<Tuple<IEnumerable<T>, int>> GetPageEntities<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
|
||||
{
|
||||
int total = await GetCountAsync(whereLambda);
|
||||
|
||||
IEnumerable<T> pageData;
|
||||
if (isAsc)
|
||||
{
|
||||
pageData = await _Db.Set<T>().Where(whereLambda)
|
||||
.OrderBy<T, S>(orderByLambda)
|
||||
.Skip(pageSize * (pageIndex - 1))
|
||||
.Take(pageSize).ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
pageData = await _Db.Set<T>().Where(whereLambda)
|
||||
.OrderByDescending<T, S>(orderByLambda)
|
||||
.Skip(pageSize * (pageIndex - 1))
|
||||
.Take(pageSize).ToListAsync();
|
||||
}
|
||||
|
||||
return Tuple.Create<IEnumerable<T>, int>(pageData, total);
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(T entity)
|
||||
{
|
||||
_Db.Set<T>().Add(entity);
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> AddAsync(IEnumerable<T> entities)
|
||||
{
|
||||
_Db.Set<T>().AddRange(entities);
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(T entity)
|
||||
{
|
||||
_Db.Set<T>().Update(entity);
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(T entity)
|
||||
{
|
||||
_Db.Set<T>().Remove(entity);
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id)
|
||||
{
|
||||
_Db.Set<T>().Remove(await GetEntityById(id));
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(IEnumerable<int> ids)
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
_Db.Set<T>().RemoveRange(await GetEntityById(id));
|
||||
}
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
public async Task<bool> DeleteAsync(Expression<Func<T, bool>> where)
|
||||
{
|
||||
IEnumerable<T> entities = await GetEntitiesAsync(where);
|
||||
if (entities != null)
|
||||
{
|
||||
_Db.Set<T>().RemoveRange(entities);
|
||||
|
||||
return await _Db.SaveChangesAsync() > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,9 @@
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Interface\Yi.Framework.Interface.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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