添加人脸

This commit is contained in:
tanglong 2025-10-30 13:00:35 +08:00
parent 11cd01c9e3
commit d9ef6a167e
15 changed files with 377 additions and 38 deletions

View File

@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YD_XinWei.Commons.Dto.Open
{
public class AddWeBankUserFaceDto
{
public int UserId { get; set; }
public IFormFile PicUrlA { get; set; }
}
}

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_XinWei.Commons")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9efa54fc9112aa3e7fc065c5055a05260858c013")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+11cd01c9e34d6d14a8b8b162a2061662baefd602")]
[assembly: System.Reflection.AssemblyProductAttribute("YD_XinWei.Commons")]
[assembly: System.Reflection.AssemblyTitleAttribute("YD_XinWei.Commons")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
969a4a6c36c831881df492a490d81a260eb7b441da8fcb9718db314cc018b26d
a9221cd8adc502c2d9936f3952d45bcfae80eabea5c75cf6e597e715edc85d39

View File

@ -227,5 +227,18 @@ namespace YD_XinWei.Api.Controllers
var res = await _xinWeiService.GetProjectInfo(orgId);
return res;
}
/// <summary>
/// 设备端人脸录入
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPut]
[Route("/basic/{apiVersion}/open/addWeBankUserFace")]
public async Task<bool> AddWeBankUserFace(IFormFile picUrlA, int userId)
{
var res = await _xinWeiService.AddWeBankUserFace(new AddWeBankUserFaceDto() { PicUrlA = picUrlA, UserId = userId });
return res;
}
}
}

View File

@ -912,5 +912,28 @@ namespace YD_XinWei.Api.Services.Impl
return res;
}
/// <summary>
/// 设备端人脸录入
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<bool> AddWeBankUserFace(AddWeBankUserFaceDto dto)
{
if (dto == null || dto.PicUrlA.Length == 0 || dto.UserId == 0)
return false;
var student = await _sportsContext.Student.Where(x => x.Id == dto.UserId).FirstOrDefaultAsync();
if (student == null)
return false;
var gradeId = await _sportsContext.Class.Where(x => x.Id == student.ClassId).Select(x => x.GradeId).FirstOrDefaultAsync();
var url = ALiYunOss.Upload(dto.PicUrlA, $"Upload/{student.SchoolCode}/Photo/Student/{gradeId}/{student.ClassId}/", student.StudentNo);
student.Photo = url;
_sportsContext.Student.Update(student);
return await _sportsContext.SaveChangesAsync() > 0;
}
}
}

View File

@ -127,5 +127,12 @@ namespace YD_XinWei.Api.Services.Interface
/// <param name="orgId"></param>
/// <returns></returns>
Task<List<ProjectVo>> GetProjectInfo(int orgId);
/// <summary>
/// 设备端人脸录入
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
Task<bool> AddWeBankUserFace(AddWeBankUserFaceDto dto);
}
}

View File

@ -0,0 +1,143 @@
using Aliyun.OSS;
using System.Net;
namespace YD_XinWei.Api.Utilities
{
/// <summary>
/// 阿里云Oss
/// </summary>
public class ALiYunOss
{
/// <summary>
/// 上传
/// </summary>
/// <param name="isIncludeExtension">是否包含文件后缀名</param>
/// <returns></returns>
public static string Upload(IFormFile file, string filePath, string fileName, bool isIncludeExtension = false)
{
if (file == null || file.Length == 0)
{
throw new ArgumentNullException("上传文件为空");
}
var url = string.Empty;
if (!isIncludeExtension)
{
// 拼接文件后缀名
var extension = Path.GetExtension(file.FileName);
filePath += fileName + extension;
}
else
{
filePath += fileName;
}
var stream = new MemoryStream();
file.CopyTo(stream);
stream.Position = 0;
// 创建OSSClient实例
var client = new OssClient(
AppSettings.ALiYunOSS.Endpoint,
AppSettings.ALiYunOSS.AccessKeyId,
AppSettings.ALiYunOSS.SecretAccessKey);
try
{
// 上传文件
var result = client.PutObject(AppSettings.YueDongServerOSS.BucketName, filePath, stream);
var uri = client.GeneratePresignedUri(AppSettings.YueDongServerOSS.BucketName, filePath);
url = "https://" + uri.Host + uri.LocalPath;
if (result.HttpStatusCode != HttpStatusCode.OK)
throw new Exception("上传失败");
}
catch (Exception ex)
{
throw new Exception("上传失败!");
}
finally
{
stream.Dispose();
}
return url;
}
/// <summary>
/// 获取指定前缀目录下的所有文件列表
/// </summary>
/// <param name="prefix">目录前缀,例如"student/"</param>
/// <returns>文件键列表</returns>
public static Dictionary<string, string> GetFileFromOss(string prefix)
{
var client = new OssClient(
AppSettings.ALiYunOSS.Endpoint,
AppSettings.ALiYunOSS.AccessKeyId,
AppSettings.ALiYunOSS.SecretAccessKey);
var listObjectsRequest = new ListObjectsRequest(AppSettings.YueDongServerOSS.BucketName)
{
Prefix = prefix
};
var result = client.ListObjects(listObjectsRequest);
Dictionary<string, string> imageDataDict = new Dictionary<string, string>();
foreach (var obj in result.ObjectSummaries)
{
// Fetch the object data from OSS
var objectData = client.GetObject(AppSettings.YueDongServerOSS.BucketName, obj.Key);
using (var memoryStream = new MemoryStream())
{
objectData.Content.CopyTo(memoryStream);
byte[] imageBytes = memoryStream.ToArray();
string base64Image = Convert.ToBase64String(imageBytes);
imageDataDict.Add(obj.Key, base64Image); // Store the file name and base64 data
}
}
return imageDataDict;
}
/// <summary>
/// 根据前缀删除指定路径下的所有文件
/// </summary>
/// <param name="prefix">目录前缀,例如"student/"</param>
public static void DeleteFilesByPrefix(string prefix)
{
var client = new OssClient(
AppSettings.ALiYunOSS.Endpoint,
AppSettings.ALiYunOSS.AccessKeyId,
AppSettings.ALiYunOSS.SecretAccessKey);
// 列出指定前缀的所有文件
var listObjectsRequest = new ListObjectsRequest(AppSettings.YueDongServerOSS.BucketName)
{
Prefix = prefix
};
var result = client.ListObjects(listObjectsRequest);
// 如果没有找到文件,直接返回
if (result.ObjectSummaries.Count() == 0)
{
return;
}
// 删除所有文件
foreach (var obj in result.ObjectSummaries)
{
try
{
// 删除文件
client.DeleteObject(AppSettings.YueDongServerOSS.BucketName, obj.Key);
}
catch (Exception ex)
{
throw new Exception("删除失败!");
}
}
}
}
}

View File

@ -1,4 +1,6 @@
namespace YD_XinWei.Api.Utilities
using Autofac.Core;
namespace YD_XinWei.Api.Utilities
{
public static class AppSettings
{
@ -13,6 +15,16 @@
public static string RedisConnectionString { get; set; }
public static WeChatConfig WeChat { get; set; }
/// <summary>
/// 阿里云OSS
/// </summary>
public static ALiYunOSS ALiYunOSS { get; set; }
/// <summary>
/// YueDongServerOSS
/// </summary>
public static YueDongServerOSS YueDongServerOSS { get; set; }
public static void Init(IConfiguration configuration)
{
Logging = configuration.GetSection("Logging").Get<LoggingConfig>();
@ -24,6 +36,10 @@
RedisConnectionString = configuration["RedisConnectionString"];
CorsUrls = configuration["CorsUrls"].Split(',');
WeChat = configuration.GetSection("WeChat").Get<WeChatConfig>();
ALiYunOSS = configuration.GetSection("ALiYunOSS").Get<ALiYunOSS>();
YueDongServerOSS = configuration.GetSection("YueDongServerOSS").Get<YueDongServerOSS>();
}
}
public class LoggingConfig
@ -59,6 +75,15 @@
public string Appid { get; set; }
public string Secret { get; set; }
}
public class ALiYunOSS
{
public string AccessKeyId { get; set; }
public string SecretAccessKey { get; set; }
public string Endpoint { get; set; }
}
public class YueDongServerOSS
{
public string BucketName { get; set; }
public string Prefix { get; set; }
}
}

View File

@ -38,5 +38,14 @@
"WeChat": {
"Appid": "xxx",
"Secret": "xxx"
},
"ALiYunOSS": {
"AccessKeyId": "LTAI5tDQAYU8PbXN7tcxW6Qw",
"SecretAccessKey": "oVpeH1PpDUnWTcBrUSSYkQNS4Fex8Y",
"Endpoint": "oss-cn-shanghai.aliyuncs.com"
},
"YueDongServerOSS": {
"BucketName": "yuedong-server",
"Prefix": "Upload"
}
}

View File

@ -30,6 +30,42 @@
}
]
},
{
"ContainingType": "YD_XinWei.Api.Controllers.XinWeiController",
"Method": "AddWeBankUserFace",
"RelativePath": "basic/{apiVersion}/open/addWeBankUserFace",
"HttpMethod": "PUT",
"IsController": true,
"Order": 0,
"Parameters": [
{
"Name": "picUrlA",
"Type": "Microsoft.AspNetCore.Http.IFormFile",
"IsRequired": false
},
{
"Name": "userId",
"Type": "System.Int32",
"IsRequired": false
},
{
"Name": "apiVersion",
"Type": "",
"IsRequired": true
}
],
"ReturnTypes": [
{
"Type": "System.Boolean",
"MediaTypes": [
"text/plain",
"application/json",
"text/json"
],
"StatusCode": 200
}
]
},
{
"ContainingType": "YD_XinWei.Api.Controllers.XinWeiController",
"Method": "GetFaceListOfDevicePage",

View File

@ -1216,6 +1216,74 @@
}
}
}
},
"/basic/{apiVersion}/open/addWeBankUserFace": {
"put": {
"tags": [
"XinWei"
],
"summary": "设备端人脸录入",
"parameters": [
{
"name": "userId",
"in": "query",
"schema": {
"type": "integer",
"format": "int32"
}
},
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"picUrlA": {
"type": "string",
"format": "binary"
}
}
},
"encoding": {
"picUrlA": {
"style": "form"
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"type": "boolean"
}
},
"application/json": {
"schema": {
"type": "boolean"
}
},
"text/json": {
"schema": {
"type": "boolean"
}
}
}
}
}
}
}
},
"components": {

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("YD_XinWei.Api")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9efa54fc9112aa3e7fc065c5055a05260858c013")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+11cd01c9e34d6d14a8b8b162a2061662baefd602")]
[assembly: System.Reflection.AssemblyProductAttribute("YD_XinWei.Api")]
[assembly: System.Reflection.AssemblyTitleAttribute("YD_XinWei.Api")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
d59d3583c895d140f4361bdf32eb75e79da767c3850e31f97b2bd9a44ed8184f
fba16e66043b8885ec0cd85a53aee89b6246d24a8a3a9bea59c28cea15ba55ec