Former-commit-id: 3e136de29850f80a9c59a36410c1eafd5aecfcd4
TangShanKaiPing
wanggang 6 years ago
parent d3d90cf505
commit 38f2f907ac

@ -78,6 +78,7 @@ namespace Infrastructure.Web.Mvc
this.ToEntity(model, entity);
this._repo.Add(entity);
this._repo.SaveChanges();
this.OnAdded(entity);
return RedirectTo();
}
catch (Exception ex)
@ -90,6 +91,10 @@ namespace Infrastructure.Web.Mvc
return View(model);
}
public virtual void OnAdded(TEntity entity)
{
}
public virtual IActionResult Edit(Guid id)
{
var query = this._repo.ReadOnlyTable();
@ -115,6 +120,7 @@ namespace Infrastructure.Web.Mvc
entity.From(model);
this.ToEntity(model, entity);
this._repo.SaveChanges();
this.OnUpdeted(entity);
return RedirectTo();
}
catch (Exception ex)
@ -127,6 +133,10 @@ namespace Infrastructure.Web.Mvc
return View(model);
}
public virtual void OnUpdeted(TEntity entity)
{
}
public virtual IActionResult Remove(List<Guid> list)
{
try
@ -177,6 +187,7 @@ namespace Infrastructure.Web.Mvc
var entity = query.FirstOrDefault(o => o.Id == id);
this._repo.Delete(entity);
this._repo.SaveChanges();
this.OnDeleted(entity);
}
return RedirectTo();
}
@ -187,6 +198,10 @@ namespace Infrastructure.Web.Mvc
}
}
public virtual void OnDeleted(TEntity entity)
{
}
public virtual IQueryable<TEntity> Include(IQueryable<TEntity> query)
{
return query;

@ -9,6 +9,7 @@ using Infrastructure.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UserCenter.Services;
namespace UserCenter.Areas.Admin.Controllers
{
@ -19,12 +20,14 @@ namespace UserCenter.Areas.Admin.Controllers
private readonly IRepository<User> _userRepo;
private readonly IEncryptionService _encrypitonService;
private readonly AjaxController _ajax;
private readonly FaceRecognitionService _frs;
public UserController(IRepository<User> userRepo, IEncryptionService encrypitonService, AjaxController ajax) : base(userRepo)
public UserController(IRepository<User> userRepo, IEncryptionService encrypitonService, AjaxController ajax, FaceRecognitionService frs) : base(userRepo)
{
this._userRepo = userRepo;
this._encrypitonService = encrypitonService;
this._ajax = ajax;
this._frs = frs;
}
public override IQueryable<User> Include(IQueryable<User> query)
@ -67,5 +70,20 @@ namespace UserCenter.Areas.Admin.Controllers
{
model.UserName = entity.UserName;
}
public override void OnAdded(User entity)
{
this._frs.AddFace(entity.UserName, entity.FaceImage);
}
public override void OnUpdeted(User entity)
{
this._frs.UpdateFace(entity.UserName, entity.FaceImage);
}
public override void OnDeleted(User entity)
{
this._frs.RemoveFace(entity.UserName);
}
}
}

@ -1,6 +1,7 @@
using Application.Domain.Entities;
using FaceRecognitionDotNet;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
@ -31,20 +32,38 @@ namespace UserCenter.Services
{
if (!string.IsNullOrEmpty(user.FaceImage))
{
var faceImagePath = Path.Combine(this._env.WebRootPath, user.FaceImage.TrimStart('/'));
using (var img = FaceRecognition.LoadImageFile(faceImagePath))
try
{
var faceEncoding = this._faceRecognition.FaceEncodings(img).ToArray().FirstOrDefault();
if (faceEncoding != null)
{
_faces.TryAdd(user.UserName, faceEncoding);
}
var faceEndoding = this.GetFaceImageEncoding(user.FaceImage);
this._faces.TryAdd(user.UserName, faceEndoding);
}
catch (Exception ex)
{
ex.PrintStack(ex.Message);
}
}
}
}
}
private FaceEncoding GetFaceImageEncoding(string faceImage)
{
var faceImagePath = Path.Combine(this._env.WebRootPath, faceImage.TrimStart('/'));
using (var img = FaceRecognition.LoadImageFile(faceImagePath))
{
var faceEncodings = this._faceRecognition.FaceEncodings(img).ToArray();
if (faceEncodings.Length == 0)
{
throw new Exception($"cann't find face");
}
else if (faceEncodings.Length > 1)
{
throw new Exception($"find too many faces");
}
return faceEncodings.FirstOrDefault();
}
}
public string FindFace(System.Drawing.Bitmap bitmap)
{
var userName = "";
@ -72,6 +91,23 @@ namespace UserCenter.Services
return userName;
}
public void AddFace(string userName, string faceImage)
{
this._faces.TryAdd(userName, this.GetFaceImageEncoding(faceImage));
}
public void UpdateFace(string userName, string faceImage)
{
this.RemoveFace(userName);
this.AddFace(userName, faceImage);
}
public void RemoveFace(string userName)
{
this._faces.TryRemove(userName, out FaceEncoding face);
face.Dispose();
}
private byte[] ToManaged(System.Drawing.Bitmap bitmap)
{
var format = bitmap.PixelFormat;

@ -155,11 +155,26 @@ namespace UserCenter
SecurityStamp = securityStam,
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
PasswordConfirmed = true,
Email = "test@test.com",
Email = "admin@test.com",
EmailConfirmed = true,
PhoneNumber = "13000000000",
PhoneNumberConfirmed = true,
NickName = "管理员",
FaceImage = "/face/admin.jpg",
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
});
dbContext.Set<User>().Add(new User
{
UserName = "tester",
SecurityStamp = securityStam,
PasswordHash = encryptionService.CreatePasswordHash("123456", securityStam),
PasswordConfirmed = true,
Email = "tester@test.com",
EmailConfirmed = true,
PhoneNumber = "13000000001",
PhoneNumberConfirmed = true,
NickName = "测试员",
FaceImage = "/face/tester.jpg",
UserRoles = new List<UserRole> { new UserRole { Role = adminRole } }
});
dbContext.SaveChanges();

@ -18,4 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\upload\" />
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

Loading…
Cancel
Save