sync with OpenAuth.Core

This commit is contained in:
yubaolee 2020-12-20 23:14:09 +08:00
parent 7217e7a924
commit 7540baa322
25 changed files with 347 additions and 169 deletions

View File

@ -2,76 +2,49 @@
// Copyright (c) 2019 openauth.me. All rights reserved.
// </copyright>
// <author>www.cnblogs.com/yubaolee</author>
// <date>2019-03-07</date>
// <summary>生成缩略图</summary>
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace Infrastructure.Helpers
{
public class ImgHelper
{
//MakeThumbnail(path, tpath, 120, 90, "H");
/// <summary>
/// 根据已有图片生成缩略图
/// <para>用法MakeThumbnail(path, tpath, 120, 90, "H");</para>
/// </summary>
/// <param name="originalImagePath">源图片路径</param>
/// <param name="thumbnailPath">缩略图保存路径</param>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">缩略模式H:指定高度宽度按比例处理W指定宽度高度按比例处理HW按参数指定的高度和宽度</param>
public static void MakeThumbnail(string originalImagePath,
string thumbnailPath,
int width = 120, int height = 90, string mode = "H")
{
//Image originalImage = Image.FromFile(originalImagePath);
//int towidth = width;
//int toheight = height;
//int x = 0;
//int y = 0;
//int ow = originalImage.Width;
//int oh = originalImage.Height;
//switch (mode)
//{
// case "HW"://指定高宽缩放(可能变形)
// break;
// case "W"://指定宽,高按比例
// toheight = originalImage.Height * width / originalImage.Width;
// break;
// case "H"://指定高,宽按比例
// towidth = originalImage.Width * height / originalImage.Height;
// break;
// case "Cut"://指定高宽裁减(不变形)
// if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
// {
// oh = originalImage.Height;
// ow = originalImage.Height * towidth / toheight;
// y = 0;
// x = (originalImage.Width - ow) / 2;
// }
// else
// {
// ow = originalImage.Width;
// oh = originalImage.Width * height / towidth;
// x = 0;
// y = (originalImage.Height - oh) / 2;
// }
// break;
// default:
// break;
//}
//MediaTypeNames.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//g.Clear(Color.Transparent);
//g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
// new Rectangle(x, y, ow, oh),
// GraphicsUnit.Pixel);
//try
//{
// bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
//}
//catch (System.Exception e)
//{
// throw e;
//}
//finally
//{
// originalImage.Dispose();
// bitmap.Dispose();
// g.Dispose();
//}
using (var originalImage = Image.Load(originalImagePath))
{
int towidth = width; //缩略图宽度
int toheight = height; //缩略图高度
switch (mode)
{
case "HW": //指定高宽缩放(可能变形)
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
originalImage.Mutate(x => x.Resize(towidth, toheight));
originalImage.Save(thumbnailPath);
}
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -18,6 +18,7 @@
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
</ItemGroup>
<ItemGroup>

View File

@ -1,19 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Helpers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
{
/// <summary>
/// 文件
/// 文件管理
/// </summary>
public class FileApp : BaseApp<UploadFile>
{
@ -22,7 +27,8 @@ namespace OpenAuth.App
private string _dbFilePath; //数据库中的文件路径
private string _dbThumbnail; //数据库中的缩略图路径
public FileApp( IOptions<AppSetting> setOptions, IUnitWork unitWork, IRepository<UploadFile> repository, ILogger<FileApp> logger, IAuth auth)
public FileApp(IOptions<AppSetting> setOptions, IUnitWork unitWork, IRepository<UploadFile> repository,
ILogger<FileApp> logger, IAuth auth)
: base(unitWork, repository, auth)
{
_logger = logger;
@ -33,6 +39,30 @@ namespace OpenAuth.App
}
}
/// <summary>
/// 加载附件列表
/// </summary>
public async Task<TableData> Load(QueryFileListReq request)
{
var result = new TableData();
var objs = UnitWork.Find<UploadFile>(null);
if (!string.IsNullOrEmpty(request.key))
{
objs = objs.Where(u => u.FileName.Contains(request.key) || u.FilePath.Contains(request.key));
}
result.data = objs.OrderByDescending(u => u.CreateTime)
.Skip((request.page - 1) * request.limit)
.Take(request.limit);
result.count = objs.Count();
return result;
}
/// <summary>
/// 批量添加附件
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
public List<UploadFile> Add(IFormFileCollection files)
{
var result = new List<UploadFile>();
@ -55,20 +85,22 @@ namespace OpenAuth.App
{
_logger.LogWarning("收到新文件为空");
}
if (file != null && file.Length > 0 && file.Length < 10485760)
{
using (var binaryReader = new BinaryReader(file.OpenReadStream()))
{
var fileName = Path.GetFileName(file.FileName);
var data = binaryReader.ReadBytes((int) file.Length);
UploadFile(fileName, data);
SaveFile(fileName, data);
var filedb = new UploadFile
{
FilePath = _dbFilePath,
Thumbnail = _dbThumbnail,
FileName = fileName,
FileSize = file.Length,
FileSize = file.Length.ToInt(),
CreateUserName = _auth.GetUserName(),
FileType = Path.GetExtension(fileName),
Extension = Path.GetExtension(fileName)
};
@ -82,7 +114,32 @@ namespace OpenAuth.App
}
}
private void UploadFile(string fileName, byte[] fileBuffers)
/// <summary>
/// 删除附件
/// </summary>
/// <param name="ids"></param>
public override void Delete(string[] ids)
{
var files = base.Repository.Find(u => ids.Contains(u.Id)).ToList();
for (int i = 0; i < files.Count(); i++)
{
var uploadPath = Path.Combine(_filePath, files[i].FilePath);
FileHelper.FileDel(uploadPath);
if (!string.IsNullOrEmpty(files[i].Thumbnail))
{
FileHelper.FileDel(Path.Combine(_filePath, files[i].Thumbnail));
}
Repository.Delete(u =>u.Id == files[i].Id);
}
}
/// <summary>
/// 存储文件,如果是图片文件则生成缩略图
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileBuffers"></param>
/// <exception cref="Exception"></exception>
private void SaveFile(string fileName, byte[] fileBuffers)
{
string folder = DateTime.Now.ToString("yyyyMMdd");
@ -114,7 +171,8 @@ namespace OpenAuth.App
fs.Close();
//生成缩略图
if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") || ext.Contains(".gif"))
if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") ||
ext.Contains(".gif"))
{
string thumbnailName = GenerateId.GenerateOrderNumber() + ext;
ImgHelper.MakeThumbnail(Path.Combine(uploadPath, newName), Path.Combine(uploadPath, thumbnailName));

View File

@ -62,6 +62,9 @@ namespace OpenAuth.App
return _revelanceApp.Get(Define.ROLEDATAPROPERTY, roleId, moduleCode);
}
/// <summary>
/// 根据某角色ID获取可访问某模块的菜单项
/// </summary>
public IEnumerable<ModuleElement> LoadMenusForRole(string moduleId, string roleId)
{
var elementIds = _revelanceApp.Get(Define.ROLEELEMENT, true, roleId);
@ -90,6 +93,10 @@ namespace OpenAuth.App
}
/// <summary>
/// 新增菜单
/// <para>当前登录用户的所有角色会自动分配菜单</para>
/// </summary>
public void AddMenu(ModuleElement model)
{
var loginContext = _auth.GetCurrentUser();

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -0,0 +1,7 @@
namespace OpenAuth.App.Request
{
public class QueryFileListReq : PageReq
{
//todo:添加自己的请求字段
}
}

View File

@ -72,14 +72,16 @@ namespace OpenAuth.App
{
foreach (var value in sameVals)
{
Repository.Delete(u => u.Key == key && u.FirstId == sameVals.Key && u.SecondId == value);
UnitWork.Delete<Relevance>(u => u.Key == key && u.FirstId == sameVals.Key && u.SecondId == value);
}
}
UnitWork.Save();
}
public void DeleteBy(string key, params string[] firstIds)
{
Repository.Delete(u => firstIds.Contains(u.FirstId) && u.Key == key);
UnitWork.Delete<Relevance>(u => firstIds.Contains(u.FirstId) && u.Key == key);
UnitWork.Save();
}
@ -160,11 +162,12 @@ namespace OpenAuth.App
{
foreach (var property in request.Properties)
{
Repository.Delete(u => u.Key == Define.ROLEDATAPROPERTY
UnitWork.Delete<Relevance>(u => u.Key == Define.ROLEDATAPROPERTY
&& u.FirstId == request.RoleId
&& u.SecondId == request.ModuleCode
&& u.ThirdId == property);
}
UnitWork.Save();
}
}
@ -173,6 +176,8 @@ namespace OpenAuth.App
/// </summary>
/// <param name="request"></param>
public void AssignRoleUsers(AssignRoleUsers request)
{
UnitWork.ExecuteWithTransaction(() =>
{
//删除以前的所有用户
UnitWork.Delete<Relevance>(u => u.SecondId == request.RoleId && u.Key == Define.USERROLE);
@ -186,6 +191,7 @@ namespace OpenAuth.App
OperateTime = DateTime.Now
}).ToArray());
UnitWork.Save();
});
}
/// <summary>
@ -193,6 +199,8 @@ namespace OpenAuth.App
/// </summary>
/// <param name="request"></param>
public void AssignOrgUsers(AssignOrgUsers request)
{
UnitWork.ExecuteWithTransaction(() =>
{
//删除以前的所有用户
UnitWork.Delete<Relevance>(u => u.SecondId == request.OrgId && u.Key == Define.USERORG);
@ -206,6 +214,7 @@ namespace OpenAuth.App
OperateTime = DateTime.Now
}).ToArray());
UnitWork.Save();
});
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using Castle.Core.Logging;
using Infrastructure;
using Infrastructure.Cache;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using OpenAuth.App.Request;
using OpenAuth.App.SSO;
namespace OpenAuth.App.Test
{
class TestFileApp :TestBase
{
public override ServiceCollection GetService()
{
var services = new ServiceCollection();
var cachemock = new Mock<ICacheContext>();
cachemock.Setup(x => x.Get<UserAuthSession>("tokentest")).Returns(new UserAuthSession { Account = "System" });
services.AddScoped(x => cachemock.Object);
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
services.AddScoped(x => httpContextAccessorMock.Object);
var logMock = new Mock<ILogger<FileApp>>();
services.AddScoped(x => logMock.Object);
return services;
}
[Test]
public void TestLoad()
{
var app = _autofacServiceProvider.GetService<FileApp>();
var result = app.Load(new QueryFileListReq()
{
page = 1,
limit = 10
});
Console.WriteLine(JsonHelper.Instance.Serialize(result.Result));
}
}
}

View File

@ -119,6 +119,9 @@ namespace OpenAuth.App
throw new Exception("请为用户分配机构");
User requser = request;
requser.CreateId = _auth.GetCurrentUser().User.Id;
UnitWork.ExecuteWithTransaction(() =>
{
if (string.IsNullOrEmpty(request.Id))
{
if (UnitWork.Any<User>(u => u.Account == request.Account))
@ -160,6 +163,8 @@ namespace OpenAuth.App
_revelanceApp.DeleteBy(Define.USERORG, requser.Id);
_revelanceApp.Assign(Define.USERORG, orgIds.ToLookup(u => requser.Id));
});
}
/// <summary>
@ -167,11 +172,15 @@ namespace OpenAuth.App
/// </summary>
/// <param name="ids"></param>
public override void Delete(string[] ids)
{
UnitWork.ExecuteWithTransaction(() =>
{
UnitWork.Delete<Relevance>(u =>(u.Key == Define.USERROLE || u.Key == Define.USERORG)
&& ids.Contains(u.FirstId));
UnitWork.Delete<User>(u => ids.Contains(u.Id));
UnitWork.Save();
});
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -59,9 +60,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryDataPrivilegeRuleListReq request)
public async Task<string> Load([FromQuery]QueryDataPrivilegeRuleListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -136,9 +137,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryFlowInstanceListReq request)
public async Task<string> Load([FromQuery]QueryFlowInstanceListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
/// <summary>

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -86,9 +87,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryFlowSchemeListReq request)
public async Task<string> Load([FromQuery]QueryFlowSchemeListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -87,9 +88,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryFormListReq request)
public async Task<string> Load([FromQuery]QueryFormListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -106,9 +107,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryOpenJobListReq request)
public async Task<string> Load([FromQuery]QueryOpenJobListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -54,9 +55,10 @@ namespace OpenAuth.Mvc.Controllers
}
public string Load([FromQuery]QueryResourcesReq request)
public async Task<string> Load([FromQuery]QueryResourcesReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -60,9 +61,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QuerySysLogListReq request)
public async Task<string> Load([FromQuery]QuerySysLogListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -60,9 +61,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QuerySysMessageListReq request)
public async Task<string> Load([FromQuery]QuerySysMessageListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
@ -59,9 +60,10 @@ namespace OpenAuth.Mvc.Controllers
/// <summary>
/// 加载列表
/// </summary>
public string Load([FromQuery]QueryWmsInboundOrderTblListReq request)
public async Task<string> Load([FromQuery]QueryWmsInboundOrderTblListReq request)
{
return JsonHelper.Instance.Serialize(_app.Load(request));
var objs = await _app.Load(request);
return JsonHelper.Instance.Serialize(objs);
}
[HttpPost]

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseRazorBuildServer>false</UseRazorBuildServer>
</PropertyGroup>

View File

@ -51,7 +51,7 @@ namespace OpenAuth.Repository.Domain
/// <summary>
/// 文件大小
/// </summary>
public long FileSize { get; set; }
public int? FileSize { get; set; }
/// <summary>
/// 扩展名称
/// </summary>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
namespace OpenAuth.WebApi.Controllers
@ -25,8 +28,41 @@ namespace OpenAuth.WebApi.Controllers
_app = app;
}
/// <summary>
/// 加载附件列表
/// </summary>
[HttpGet]
public async Task<TableData> Load([FromQuery]QueryFileListReq request)
{
return await _app.Load(request);
}
/// <summary>
/// 删除附件
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[HttpPost]
public Response Delete([FromBody]string[] ids)
{
var result = new Response();
try
{
_app.Delete(ids);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 批量上传文件接口
/// <para>客户端文本框需设置name='files'</para>
/// </summary>
/// <param name="files"></param>
/// <returns>服务器存储的文件信息</returns>

View File

@ -187,7 +187,10 @@ namespace OpenAuth.WebApi.Controllers
#endregion
//添加或修改
/// <summary>
/// 新增菜单
/// <para>当前登录用户的所有角色会自动分配菜单</para>
/// </summary>
[HttpPost]
public Response<ModuleElement> AddMenu(ModuleElement obj)
{
@ -206,7 +209,9 @@ namespace OpenAuth.WebApi.Controllers
return result;
}
//添加或修改
/// <summary>
/// 修改菜单属性
/// </summary>
[HttpPost]
public Response UpdateMenu(ModuleElement obj)
{
@ -226,6 +231,9 @@ namespace OpenAuth.WebApi.Controllers
}
/// <summary>
/// 删除菜单
/// </summary>
[HttpPost]
public Response DeleteMenu([FromBody]string[] ids)
{

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">