移动系统liao
2025-02-08 479b6cfc60113f692f6f9146bcd7b9231a32b0e8
cylsg/EzUpFile/EzFileUploadService.cs
@@ -3,14 +3,21 @@
using Aliyun.OSS.Util;
using cylsg.utility;
using cylsg.utility.Extend;
using EzTencentCloud;
using Furion;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Buffers.Text;
using System.Drawing;
using System.Globalization;
using System.Security.Policy;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloud.Teo.V20220901.Models;
using Task = System.Threading.Tasks.Task;
namespace EzUpFile
{
    /// <summary>
@@ -21,11 +28,13 @@
        private readonly HttpRequest? _request;
        private readonly ISqlSugarClient _sqlSugarClient;
        public EzFileUploadService(IHttpContextAccessor httpContext, ISqlSugarClient sqlSugarClient)
        private readonly ITencentCloudService _tcs;
        public EzFileUploadService(IHttpContextAccessor httpContext, ISqlSugarClient sqlSugarClient,ITencentCloudService tencentCloudService)
        {
            _request = httpContext.HttpContext?.Request ?? null;
            _sqlSugarClient = sqlSugarClient;
            _tcs= tencentCloudService;
        }
        /// <summary>
@@ -275,8 +284,248 @@
            return true;
        }
        #endregion
        #region 识别上传
        public async Task<(IDCardOCRResponse, string)> UpIdCord(string PageName = "FRONT")
        {
            try
            {
                var maxSize = 1024 * 1024 * 5; //上传大小5M
                var FileData = _request?.Form?.Files["file"];
                if (FileData.Length > maxSize)
                {
                    throw Oops.Oh(" 上传文件不可超出5M");
                }
                //处理图形
                //  var FileData = Request.Form.Files[0];
                Image oimage = Image.FromStream(FileData.OpenReadStream());
                if (oimage == null)
                {
                    throw Oops.Oh(" 上传失败");
                }
                MemoryStream ms = new MemoryStream();
                if (oimage.Width > 1200)
                {
                    if (oimage.Width > oimage.Height)
                        oimage.GetThumbnailImage(1200, 800, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    else
                        oimage.GetThumbnailImage(800, 1200, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                    oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.Position = 0;
                var arr = ms.ToArray();
                string img64 = Convert.ToBase64String(arr);
                IDCardOCRResponse idcordinfo = null;
                string url = "";
                try
                {
                    idcordinfo = _tcs.IdCord(img64, PageName == "FRONT");
                    url = await UploadFilesFByBase64(_tcs.GetIdCordImg());
                    idcordinfo.AdvancedInfo = null;
                    return (idcordinfo, url);
                }
                catch (Exception e)
                {
                    throw Oops.Oh(e.Message+"腾讯云,或者阿里云操作错误");
                }
            }
            catch (Exception e)
            {
                throw Oops.Oh(e.Message);
            }
        }
        public async Task<(BizLicenseOCRResponse, string)> UpBizLicense()
        {
            try
            {
                var maxSize = 1024 * 1024 * 5; //上传大小5M
                var FileData = _request?.Form?.Files["file"];
                if (FileData.Length > maxSize)
                {
                    throw Oops.Oh(" 上传文件不可超出5M");
                }
                //处理图形
                //  var FileData = Request.Form.Files[0];
                Image oimage = Image.FromStream(FileData.OpenReadStream());
                if (oimage == null)
                {
                    throw Oops.Oh(" 上传失败");
                }
                MemoryStream ms = new MemoryStream();
                if (oimage.Width > 1200)
                {
                    if (oimage.Width > oimage.Height)
                        oimage.GetThumbnailImage(1200, 800, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    else
                        oimage.GetThumbnailImage(800, 1200, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                    oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.Position = 0;
                var arr = ms.ToArray();
                string img64 = Convert.ToBase64String(arr);
                BizLicenseOCRResponse info = null;
                string url = "";
                try
                {
                    info = _tcs.BizLicenseOCR(img64);
                    url = await UploadFilesFByBase64(img64);
                    return (info, url);
                }
                catch (Exception e)
                {
                    throw Oops.Oh(e.Message);
                }
            }
            catch (Exception e)
            {
                throw Oops.Oh(e.Message);
            }
        }
        public async Task<(bool,string)> IaiAddPerso(string CoredID,string Name,int PersonGender)
        {
            try
            {
                var maxSize = 1024 * 1024 * 5; //上传大小5M
                var FileData = _request?.Form?.Files["file"];
                if (FileData.Length > maxSize)
                {
                    throw Oops.Oh(" 上传文件不可超出500K");
                }
                //处理图形
                //  var FileData = Request.Form.Files[0];
                Image oimage = Image.FromStream(FileData.OpenReadStream());
                if (oimage == null)
                {
                    throw Oops.Oh(" 上传失败");
                }
                MemoryStream ms = new MemoryStream();
                if (oimage.Width > 600)
                {
                    if (oimage.Width > oimage.Height)
                        oimage.GetThumbnailImage(600, 400, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    else
                        oimage.GetThumbnailImage(400, 600, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                    oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.Position = 0;
                var arr = ms.ToArray();
                string img64 = Convert.ToBase64String(arr);
                bool  info = false;
                string url = "";
                try
                {
                    info =  _tcs.IaiAddPerso(img64, CoredID, Name, PersonGender);
                    url = await UploadFilesFByBase64(img64);
                    return (info, url);
                }
                catch (Exception e)
                {
                    throw Oops.Oh(e.Message);
                }
            }
            catch (Exception e)
            {
                throw Oops.Oh(e.Message);
            }
        }
/// <inheritdoc/>
        public async Task<(bool, string)> IaiAddPerso(string imgBase64, string CoredID, string Name, int PersonGender)
        {
            if (string.IsNullOrEmpty(imgBase64))
            {
                throw Oops.Oh("没有内容");
            }
            //检查上传大小
            if (!CommonHelper.CheckBase64Size(imgBase64, 5))
            {
                throw Oops.Oh("上传文件大小超过限制,最大允许上传" + "5" + "M");
            }
            imgBase64 = imgBase64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
            bool info = false;
            string url = "";
            try
            {
                info = _tcs.IaiAddPerso(imgBase64, CoredID, Name, PersonGender);
                url = await UploadFilesFByBase64(imgBase64);
                return (info, url);
            }
            catch (Exception e)
            {
                throw Oops.Oh(e.Message);
            }
        }
        #endregion
    }
}