From b8510ca5d1e56ef78cd27c45b6bd65a2c13f6b27 Mon Sep 17 00:00:00 2001 From: yanjinhui <3306209981@qq.com> Date: 星期一, 17 三月 2025 15:12:34 +0800 Subject: [PATCH] 代码优化 --- 项目代码/WIDESEAWCS_Server 正式/WIDESEAWCS_SystemServices/Sys_UserService.cs | 67 ++++++++++++++++++++++++++++++++- 1 files changed, 65 insertions(+), 2 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs" index 8871a36..7c4034d 100644 --- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs" +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WIDESEAWCS_Server \346\255\243\345\274\217/WIDESEAWCS_SystemServices/Sys_UserService.cs" @@ -12,6 +12,10 @@ using System.Net; using WIDESEAWCS_Core.Caches; using WIDESEA_ISerialPortRepository; +using Microsoft.AspNetCore.Http; +using SqlSugar; +using ICacheService = WIDESEAWCS_Core.Caches.ICacheService; +using Microsoft.AspNetCore.Builder; namespace WIDESEAWCS_SystemServices { @@ -199,8 +203,8 @@ nameof(Sys_User.LastModifyPwdDate), nameof(Sys_User.UserPwd) }); - content = WebResponseContent.Instance.OK("瀵嗙爜淇敼鎴愬姛"); + //content = WebResponseContent.Instance.OK("瀵嗙爜淇敼鎴愬姛",userId); } catch (Exception ex) { @@ -208,6 +212,7 @@ content = WebResponseContent.Instance.Error("鏈嶅姟鍣ㄤ簡鐐归棶棰�,璇风◢鍚庡啀璇�"); } return content; + } public WebResponseContent GetUerType() @@ -241,7 +246,7 @@ } - public WebResponseContent PermissionView(int userId, string group) + public WebResponseContent PermissionView(int userId, string group) { try { @@ -292,5 +297,63 @@ } } + + + public WebResponseContent UploaDavatar(List<IFormFile> files, int userId) + { + if (files == null || files.Count == 0) + return new WebResponseContent { Status = false, Message = "璇蜂笂浼犳枃浠�" }; + + // 1. 鎸囧畾鍥哄畾鐨勫瓨鍌ㄧ洰褰� + //string baseDirectory = @"E:\缇庡瀷\NiuJuKongZhi\椤圭洰浠g爜\WIDESEAWCS_Server 姝e紡\WIDESEAWCS_Server\wwwroot\image\"; + string baseDirectory = @"wwwroot\image\"; + + // 2. 鐢熸垚瀛愮洰褰曪紝閬垮厤鏂囦欢鍐茬獊 + string subDirectory = $"{DateTime.Now:yyMMddHHmmss}_{new Random().Next(1000, 9999)}"; + string filePath = Path.Combine(baseDirectory, subDirectory); + + //3. 纭繚鐩綍瀛樺湪 + if (!Directory.Exists(filePath)) + Directory.CreateDirectory(filePath); + + try + { + string uploadedFilePath = ""; + for (int i = 0; i < files.Count; i++) + { + // 4. 鐢熸垚鍞竴鏂囦欢鍚嶏紙闃叉閲嶅悕瑕嗙洊锛� + string fileName = $"{Guid.NewGuid()}{Path.GetExtension(files[i].FileName)}"; + string fullFilePath = Path.Combine(filePath, fileName); + + // 5. 淇濆瓨鏂囦欢鍒版寚瀹氳矾寰� + using (var stream = new FileStream(fullFilePath, FileMode.Create)) + { + files[i].CopyTo(stream); + } + + // 6. 璁板綍鏂囦欢璺緞锛堝瓨鍏ユ暟鎹簱鐨勭浉瀵硅矾寰勶級 + uploadedFilePath = Path.Combine("/image", subDirectory, fileName).Replace("\\", "/"); + break; // 鍙鐞嗙涓�涓枃浠� + } + + //7. 鏇存柊鐢ㄦ埛澶村儚璺緞鍒版暟鎹簱 + var user = BaseDal.QueryData(u => u.User_Id == userId).FirstOrDefault(); + if (user != null) + { + user.HeadImageUrl = uploadedFilePath; + UpdateData(user); + } + else + { + return new WebResponseContent { Status = false, Message = "鐢ㄦ埛涓嶅瓨鍦�" }; + } + + return new WebResponseContent { Status = true, Message = "鏂囦欢涓婁紶鎴愬姛", Data = uploadedFilePath }; + } + catch (Exception ex) + { + return new WebResponseContent { Status = false, Message = "涓婁紶鏂囦欢澶辫触锛�" + ex.Message }; + } + } } } -- Gitblit v1.9.3