fix 数据库连接切换
This commit is contained in:
@@ -27,6 +27,8 @@ public class GainfoService : IGainfoService
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly IDevicePermissionService _devicePermissionService;
|
||||
private readonly RedisCache _redisCache;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<GainfoService> _logger;
|
||||
|
||||
public GainfoService(IGainfoStore gainfoStore,
|
||||
@@ -34,6 +36,8 @@ public class GainfoService : IGainfoService
|
||||
ICurrentUser currentUser,
|
||||
IDevicePermissionService devicePermissionService,
|
||||
RedisCache redisCache,
|
||||
IServiceProvider serviceProvider,
|
||||
IConfiguration configuration,
|
||||
ILogger<GainfoService> logger)
|
||||
{
|
||||
_gainfoStore = gainfoStore;
|
||||
@@ -41,6 +45,8 @@ public class GainfoService : IGainfoService
|
||||
_currentUser = currentUser;
|
||||
_devicePermissionService = devicePermissionService;
|
||||
_redisCache = redisCache;
|
||||
_serviceProvider = serviceProvider;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -168,51 +174,38 @@ public class GainfoService : IGainfoService
|
||||
|
||||
public async Task<IActionResult> GetFaceAsync(string token)
|
||||
{
|
||||
return await Scoped.Request.Value.CreateAsync<IActionResult>(async (sp) =>
|
||||
var timeLimitedDataProtector = _serviceProvider.GetService<IDataProtectionProvider>()
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
|
||||
try
|
||||
{
|
||||
var timeLimitedDataProtector = sp.GetService<IDataProtectionProvider>()
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
var password = timeLimitedDataProtector.Unprotect(token);
|
||||
var userCode = password.Split(':')[1];
|
||||
|
||||
try
|
||||
var faceCache = await _redisCache.HashGetAsync("faces", userCode);
|
||||
if (faceCache.HasValue)
|
||||
{
|
||||
var password = timeLimitedDataProtector.Unprotect(token);
|
||||
var userCode = password.Split(':')[1];
|
||||
|
||||
var faceCache = await _redisCache.HashGetAsync("faces", userCode);
|
||||
if (faceCache.HasValue)
|
||||
{
|
||||
return new FileContentResult(Convert.FromBase64String(faceCache), "image/png");
|
||||
}
|
||||
|
||||
var configuration = sp.GetService<IConfiguration>();
|
||||
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
|
||||
if (faceConnectionString.IsEmpty())
|
||||
{
|
||||
_logger.LogWarning("暂不支持获取人脸");
|
||||
return new NotFoundResult();
|
||||
}
|
||||
|
||||
//todo 切换连接字符串
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == userCode).SingleAsync();
|
||||
|
||||
if (face == null)
|
||||
{
|
||||
_logger.LogWarning("学工号:{userCode}不存在人脸照片", userCode);
|
||||
return new NotFoundResult();
|
||||
}
|
||||
|
||||
await _redisCache.HashSetAsync("faces", userCode, face.Face);
|
||||
return new FileContentResult(Convert.FromBase64String(face.Face), "image/png");
|
||||
return new FileContentResult(Convert.FromBase64String(faceCache), "image/png");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
var dbContext = _dbContext.ChangeDb("face");
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == userCode).SingleAsync();
|
||||
|
||||
if (face == null)
|
||||
{
|
||||
_logger.LogError(ex, "获取人脸失败:" + ex.Message);
|
||||
_logger.LogWarning("学工号:{userCode}不存在人脸照片", userCode);
|
||||
return new NotFoundResult();
|
||||
}
|
||||
});
|
||||
|
||||
await _redisCache.HashSetAsync("faces", userCode, face.Face);
|
||||
return new FileContentResult(Convert.FromBase64String(face.Face), "image/png");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取人脸失败:{message}", ex.Message);
|
||||
return new NotFoundResult();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UploadFaceAsync(GainfoUploadFaceRequest request)
|
||||
@@ -222,18 +215,48 @@ public class GainfoService : IGainfoService
|
||||
|
||||
gainfo.ThrowIfNull("人员档案信息不存在");
|
||||
|
||||
await Scoped.Request.Value.CreateAsync(async (sp) =>
|
||||
var dbContext = _dbContext.ChangeDb("face");
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
|
||||
|
||||
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
|
||||
|
||||
await _redisCache.HashSetAsync("faces", gainfo.Code, request.Face);
|
||||
if (face == null)
|
||||
{
|
||||
var configuration = sp.GetService<IConfiguration>();
|
||||
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
|
||||
if (faceConnectionString.IsEmpty())
|
||||
face = new GainfoFaceEntity
|
||||
{
|
||||
throw new UserFriendlyException("暂不支持人脸上传");
|
||||
UserCode = gainfo.Code,
|
||||
Face = request.Face
|
||||
};
|
||||
|
||||
await dbContext.Insertable(face).ExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (face.Face == request.Face)
|
||||
{
|
||||
_logger.LogWarning("相同的人脸照片更新忽略");
|
||||
return;
|
||||
}
|
||||
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
//todo 切换连接字符串
|
||||
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
|
||||
face.Face = request.Face;
|
||||
await dbContext.Updateable(face).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
//生成任务
|
||||
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType);
|
||||
}
|
||||
|
||||
public async Task UploadFacesAsync(List<GainfoUploadFaceRequest> requests)
|
||||
{
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var gainfo = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).Where(x => x.No == request.UserNo).FirstAsync();
|
||||
gainfo ??= await _gainfoStore.Queryable(GainfoStatusEnumType.External).Where(x => x.No == request.UserNo).FirstAsync();
|
||||
|
||||
gainfo.ThrowIfNull("人员档案信息不存在");
|
||||
|
||||
var dbContext = _dbContext.ChangeDb("face");
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
|
||||
|
||||
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
|
||||
@@ -247,7 +270,7 @@ public class GainfoService : IGainfoService
|
||||
Face = request.Face
|
||||
};
|
||||
|
||||
await dbContext.InsertAsync(face);
|
||||
await dbContext.Insertable(face).ExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -258,61 +281,11 @@ public class GainfoService : IGainfoService
|
||||
}
|
||||
|
||||
face.Face = request.Face;
|
||||
await dbContext.UpdateAsync(face);
|
||||
await dbContext.Updateable(face).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
//生成任务
|
||||
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task UploadFacesAsync(List<GainfoUploadFaceRequest> requests)
|
||||
{
|
||||
foreach (var request in requests)
|
||||
{
|
||||
var gainfo = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).Where(x => x.No == request.UserNo).FirstAsync();
|
||||
gainfo ??= await _gainfoStore.Queryable(GainfoStatusEnumType.External).Where(x => x.No == request.UserNo).FirstAsync();
|
||||
|
||||
gainfo.ThrowIfNull("人员档案信息不存在");
|
||||
|
||||
await Scoped.Request.Value.CreateAsync(async (sp) =>
|
||||
{
|
||||
var configuration = sp.GetService<IConfiguration>();
|
||||
var faceConnectionString = configuration.GetSection("SqlSugarOptions:FaceConnectionString").Value;
|
||||
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
//todo 切换连接字符串
|
||||
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
|
||||
|
||||
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
|
||||
|
||||
await _redisCache.HashSetAsync("faces", gainfo.Code, request.Face);
|
||||
if (face == null)
|
||||
{
|
||||
face = new GainfoFaceEntity
|
||||
{
|
||||
UserCode = gainfo.Code,
|
||||
Face = request.Face
|
||||
};
|
||||
|
||||
await dbContext.InsertAsync(face);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (face.Face == request.Face)
|
||||
{
|
||||
_logger.LogWarning("相同的人脸照片更新忽略");
|
||||
return;
|
||||
}
|
||||
|
||||
face.Face = request.Face;
|
||||
await dbContext.UpdateAsync(face);
|
||||
}
|
||||
|
||||
//生成任务
|
||||
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,39 +294,25 @@ public class GainfoService : IGainfoService
|
||||
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode).SingleAsync();
|
||||
gainfo.ThrowIfNull($"{userCode}人员信息不存在");
|
||||
|
||||
return await Scoped.Request.Value.CreateAsync<string>(async (sp) =>
|
||||
{
|
||||
var configuration = sp.GetService<IConfiguration>();
|
||||
var dbContext = _dbContext.ChangeDb("face");
|
||||
|
||||
var faceConnectionString = configuration.GetSection($"{nameof(SqlSugarOptions)}:FaceConnectionString").Value;
|
||||
if (faceConnectionString.IsEmpty())
|
||||
{
|
||||
throw new UserFriendlyException("暂不支持获取人脸");
|
||||
}
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.SingleAsync();
|
||||
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
//todo 切换连接字符串
|
||||
dbContext.Client.CurrentConnectionConfig.ConnectionString = faceConnectionString;
|
||||
face.ThrowIfNull("人脸照片尚未录入");
|
||||
|
||||
var face = await dbContext.Queryable<GainfoFaceEntity>()
|
||||
.Where(x => x.UserCode == userCode)
|
||||
.SingleAsync();
|
||||
var timeLimitedDataProtector = _serviceProvider.GetService<IDataProtectionProvider>()
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
|
||||
face.ThrowIfNull("人脸照片尚未录入");
|
||||
var faceUrl = _configuration.GetSection("FaceUrlOptions:Url").Value;
|
||||
var limit = _configuration.GetSection("FaceUrlOptions:Limit").Get<int>();
|
||||
|
||||
var timeLimitedDataProtector = sp.GetService<IDataProtectionProvider>()
|
||||
.CreateProtector("down:face")
|
||||
.ToTimeLimitedDataProtector();
|
||||
var key = $"face:{userCode}";
|
||||
var token = timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(limit));
|
||||
|
||||
var faceUrl = configuration.GetSection("FaceUrlOptions:Url").Value;
|
||||
var limit = configuration.GetSection("FaceUrlOptions:Limit").Get<int>();
|
||||
var dataProtectionProvider = sp.GetService<IDataProtectionProvider>();
|
||||
|
||||
var key = $"face:{userCode}";
|
||||
var token = timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(limit));
|
||||
|
||||
return faceUrl + token;
|
||||
});
|
||||
return faceUrl + token;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>> GetFaceByUserCodesAsync(GetFaceByUserCodesRequest request)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Kurisu.AspNetCore.Startup;
|
||||
using Kurisu.AspNetCore.Utils;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Startup;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
@@ -23,8 +24,8 @@ public class Startup : DefaultStartup
|
||||
base.ConfigureServices(services);
|
||||
services.AddRedis();
|
||||
|
||||
//todo
|
||||
var redis = ConnectionMultiplexer.Connect($"192.168.199.30:30115,defaultDatabase:1,password=dlhis123");
|
||||
var reidConnection = Configuration.GetSection($"{nameof(RedisOptions)}:{nameof(RedisOptions.ConnectionString)}").Value;
|
||||
var redis = ConnectionMultiplexer.Connect(reidConnection);
|
||||
services.AddDataProtection()
|
||||
.SetApplicationName("scheduled")
|
||||
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
|
||||
|
||||
@@ -48,7 +48,6 @@ public class UserService : IUserService
|
||||
|
||||
public async Task<List<UserResponse>> GetUsersAsync()
|
||||
{
|
||||
//todo tenant
|
||||
var users = await _dbContext.Queryable<UserEntity>().Select<UserResponse>().ToListAsync();
|
||||
return users;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user