yanjinhui
2025-03-17 b8510ca5d1e56ef78cd27c45b6bd65a2c13f6b27
ÏîÄ¿´úÂë/WIDESEAWCS_Server Õýʽ/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()
@@ -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\项目代码\WIDESEAWCS_Server æ­£å¼\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 };
            }
        }
    }
}