From e6b190354191122069b1a0518f050d6504f7ec5e Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 11 二月 2026 15:29:39 +0800
Subject: [PATCH] 重构WMS服务并简化逻辑

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs |  188 +++++++++++++++++-----------------------------
 1 files changed, 71 insertions(+), 117 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs
index cea2a5f..293dbb5 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_UserService.cs
@@ -1,5 +1,7 @@
-锘縰sing WIDESEA_Core.Authorization;
+锘縰sing SqlSugar;
 using WIDESEA_Core;
+using WIDESEA_Core.Authorization;
+using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
 using WIDESEA_Core.Const;
 using WIDESEA_Core.Helper;
@@ -7,18 +9,8 @@
 using WIDESEA_ISystemService;
 using WIDESEA_Model;
 using WIDESEA_Model.Models;
-using WIDESEA_Core.BaseRepository;
-using System.Net;
-using WIDESEA_Core.Caches;
-using SqlSugar;
 using ICacheService = WIDESEA_Core.Caches.ICacheService;
-using HslCommunication.WebSocket;
-using System.Drawing.Drawing2D;
-using System.Linq;
-using MailKit.Search;
 using OrderByType = SqlSugar.OrderByType;
-using System.Drawing.Printing;
-//using WIDESEA_Core.HostedService;
 
 namespace WIDESEA_SystemService
 {
@@ -41,62 +33,47 @@
 
         public WebResponseContent Login(LoginInfo loginInfo)
         {
-            WebResponseContent content = new WebResponseContent();
             try
             {
-                //BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName);
-
-                string msg = string.Empty;
-
-                #region 涓存椂浣跨敤
                 try
                 {
                     loginInfo.Password = loginInfo.Password.EncryptDES(AppSecret.User);
                 }
-                catch
-                {
+                catch { }
 
-                }
-                #endregion
-
-                UserInfo user = BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password, x => new UserInfo { HeadImageUrl = x.HeadImageUrl, RoleId = x.RoleId, TenantId = x.TenantId, UserId = x.UserId, UserName = x.UserName, UserTrueName = x.UserTrueName });
-                if (user != null)
-                {
-                    object obj = _menuService.GetMenuActionList(user.RoleId);
-                    if (obj is not IEnumerable<object> list)
+                var user = BaseDal.QueryFirst(
+                    x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password,
+                    x => new UserInfo
                     {
-                        return WebResponseContent.Instance.Error("鏃犵櫥褰曟潈闄�");
-                    }
-                    if (!list.Any())
-                    {
-                        return WebResponseContent.Instance.Error("鏃犵櫥褰曟潈闄�");
-                    }
-
-                    string token = JwtHelper.IssueJwt(new TokenModelJwt()
-                    {
-                        UserId = user.UserId,
-                        RoleId = user.RoleId,
-                        UserName = user.UserName,
-                        TenantId = user.TenantId,
+                        HeadImageUrl = x.HeadImageUrl,
+                        RoleId = x.RoleId,
+                        TenantId = x.TenantId,
+                        UserId = x.UserId,
+                        UserName = x.UserName,
+                        UserTrueName = x.UserTrueName
                     });
-                    App.User.UpdateToke(token, user.UserId);
 
-                    //if (PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == user.UserId) == null)
-                    //    PermissionDataHostService.UserRoles.AddRange(PermissionDataHostService.GetUserRoles(Db, user.UserId));
+                if (user == null)
+                    return WebResponseContent.Instance.Error("璐﹀彿鎴栧瘑鐮侀敊璇�");
 
-                    content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName });
-                }
-                else
+                if (_menuService.GetMenuActionList(user.RoleId) is not IEnumerable<object> { } list || !list.Any())
+                    return WebResponseContent.Instance.Error("鏃犵櫥褰曟潈闄�");
+
+                var token = JwtHelper.IssueJwt(new TokenModelJwt
                 {
-                    content = WebResponseContent.Instance.Error("璐﹀彿鎴栧瘑鐮侀敊璇�");
-                }
+                    UserId = user.UserId,
+                    RoleId = user.RoleId,
+                    UserName = user.UserName,
+                    TenantId = user.TenantId,
+                });
+                App.User.UpdateToke(token, user.UserId);
+
+                return WebResponseContent.Instance.OK(data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName });
             }
             catch (Exception ex)
             {
-                content = WebResponseContent.Instance.Error(ex.Message);
+                return WebResponseContent.Instance.Error(ex.Message);
             }
-
-            return content;
         }
 
         public override WebResponseContent UpdateData(SaveModel saveModel)
@@ -114,53 +91,41 @@
 
         public override PageGridData<Sys_User> GetPageData(PageDataOptions options)
         {
-            int roleId = -1;
-            //鏍戝舰鑿滃崟浼犳煡璇㈣鑹蹭笅鎵�鏈夌敤鎴�
-            if (options.Value != null)
-            {
-                roleId = options.Value.ObjToInt();
-            }
+            int roleId = options.Value?.ObjToInt() ?? -1;
 
             if (roleId <= 0)
             {
                 if (App.User.IsHighestRole) return base.GetPageData(options);
                 roleId = App.User.RoleId;
             }
-            int totalCount = 0;
-            List<int> roleIds = _roleService.GetAllChildrenRoleId(roleId).Where(x => x != roleId).ToList();
-            ISugarQueryable<Sys_User> sugarQueryable = Db.Queryable<Sys_User>();
+
+            var roleIds = _roleService.GetAllChildrenRoleId(roleId).Where(x => x != roleId).ToList();
+            var sugarQueryable = Db.Queryable<Sys_User>();
             ValidatePageOptions(options, ref sugarQueryable);
 
-            Dictionary<string, OrderByType> orderbyDic = options.GetPageDataSort(TProperties);
-            List<OrderByModel> orderByModels = new List<OrderByModel>();
-            foreach (var item in orderbyDic)
-            {
-                OrderByModel orderByModel = new OrderByModel()
-                {
-                    FieldName = item.Key,
-                    OrderByType = item.Value
-                };
-                orderByModels.Add(orderByModel);
-            }
-            List<Sys_User> users = sugarQueryable.Where(x => roleIds.Contains(x.RoleId) || x.UserId == App.User.UserId).OrderBy(orderByModels).ToPageList(options.Page, options.Rows, ref totalCount);
+            var orderByModels = options.GetPageDataSort(TProperties)
+                .Select(item => new OrderByModel { FieldName = item.Key, OrderByType = item.Value })
+                .ToList();
+
+            int totalCount = 0;
+            var users = sugarQueryable
+                .Where(x => roleIds.Contains(x.RoleId) || x.UserId == App.User.UserId)
+                .OrderBy(orderByModels)
+                .ToPageList(options.Page, options.Rows, ref totalCount);
+
             return new PageGridData<Sys_User> { Rows = users, Total = totalCount };
         }
 
         public override WebResponseContent AddData(SaveModel saveModel)
         {
-            string pwd = "123456";
-            string uesrName = saveModel.MainData[nameof(Sys_User.UserName).FirstLetterToLower()].ToString();
-            saveModel.MainData[nameof(Sys_User.UserPwd).FirstLetterToLower()] = pwd.EncryptDES(AppSecret.User);
+            const string defaultPwd = "123456";
+            string userName = saveModel.MainData[nameof(Sys_User.UserName).FirstLetterToLower()].ToString();
+            saveModel.MainData[nameof(Sys_User.UserPwd).FirstLetterToLower()] = defaultPwd.EncryptDES(AppSecret.User);
 
-            WebResponseContent content = base.AddData(saveModel);
-            if (content.Status)
-            {
-                return WebResponseContent.Instance.OK($"鐢ㄦ埛鏂板缓鎴愬姛.甯愬彿{uesrName}瀵嗙爜{pwd}");
-            }
-            else
-            {
-                return content;
-            }
+            var content = base.AddData(saveModel);
+            return content.Status
+                ? WebResponseContent.Instance.OK($"鐢ㄦ埛鏂板缓鎴愬姛.甯愬彿{userName}瀵嗙爜{defaultPwd}")
+                : content;
         }
 
         /// <summary>
@@ -188,16 +153,13 @@
         /// <summary>
         /// 淇敼瀵嗙爜
         /// </summary>
-        /// <param name="parameters"></param>
-        /// <returns></returns>
         public WebResponseContent ModifyPwd(string oldPwd, string newPwd)
         {
-            WebResponseContent content = WebResponseContent.Instance;
-            oldPwd = oldPwd?.Trim();
-            newPwd = newPwd?.Trim();
-            string message = "";
             try
             {
+                oldPwd = oldPwd?.Trim();
+                newPwd = newPwd?.Trim();
+
                 if (string.IsNullOrEmpty(oldPwd)) return WebResponseContent.Instance.Error("鏃у瘑鐮佷笉鑳戒负绌�");
                 if (string.IsNullOrEmpty(newPwd)) return WebResponseContent.Instance.Error("鏂板瘑鐮佷笉鑳戒负绌�");
                 if (newPwd.Length < 6) return WebResponseContent.Instance.Error("瀵嗙爜涓嶈兘灏戜簬6浣�");
@@ -205,49 +167,42 @@
                 int userId = App.User.UserId;
                 string userCurrentPwd = BaseDal.QueryFirst(x => x.UserId == userId, s => s.UserPwd);
 
-                string _oldPwd = oldPwd.EncryptDES(AppSecret.User);
-                if (_oldPwd != userCurrentPwd) return WebResponseContent.Instance.Error("鏃у瘑鐮佷笉姝g‘");
+                string encryptedOldPwd = oldPwd.EncryptDES(AppSecret.User);
+                if (encryptedOldPwd != userCurrentPwd) return WebResponseContent.Instance.Error("鏃у瘑鐮佷笉姝g‘");
 
-                string _newPwd = newPwd.EncryptDES(AppSecret.User);
-                if (userCurrentPwd == _newPwd) return WebResponseContent.Instance.Error("鏂板瘑鐮佷笉鑳戒笌鏃у瘑鐮佺浉鍚�");
-
+                string encryptedNewPwd = newPwd.EncryptDES(AppSecret.User);
+                if (userCurrentPwd == encryptedNewPwd) return WebResponseContent.Instance.Error("鏂板瘑鐮佷笉鑳戒笌鏃у瘑鐮佺浉鍚�");
 
                 BaseDal.UpdateData(new Sys_User
                 {
                     UserId = userId,
-                    UserPwd = _newPwd,
+                    UserPwd = encryptedNewPwd,
                     LastModifyPwdDate = DateTime.Now
-                }, new List<string>
-                {
-                    nameof(Sys_User.LastModifyPwdDate),
-                    nameof(Sys_User.UserPwd)
-                });
+                }, new List<string> { nameof(Sys_User.LastModifyPwdDate), nameof(Sys_User.UserPwd) });
 
-                content = WebResponseContent.Instance.OK("瀵嗙爜淇敼鎴愬姛");
+                return WebResponseContent.Instance.OK("瀵嗙爜淇敼鎴愬姛");
             }
-            catch (Exception ex)
+            catch (Exception)
             {
-                message = ex.Message;
-                content = WebResponseContent.Instance.Error("鏈嶅姟鍣ㄤ簡鐐归棶棰�,璇风◢鍚庡啀璇�");
+                return WebResponseContent.Instance.Error("鏈嶅姟鍣ㄤ簡鐐归棶棰�,璇风◢鍚庡啀璇�");
             }
-            return content;
         }
         public WebResponseContent ModifyUserPwd(string password, string userName)
         {
-            WebResponseContent content = new WebResponseContent();
-            string message = "";
-            password = password?.Trim();
             try
             {
+                password = password?.Trim();
                 if (string.IsNullOrEmpty(password)) return WebResponseContent.Instance.Error("瀵嗙爜涓嶈兘涓虹┖");
-                //鑾峰彇鐢ㄦ埛
-                Sys_User user = BaseDal.QueryFirst(x => x.UserName == userName);
+
+                var user = BaseDal.QueryFirst(x => x.UserName == userName);
                 if (user == null) return WebResponseContent.Instance.Error("鐢ㄦ埛涓嶅瓨鍦�");
+
                 user.UserPwd = password.EncryptDES(AppSecret.User);
                 BaseDal.UpdateData(user);
+
                 if (App.User.UserId == user.UserId)
                 {
-                    string token = JwtHelper.IssueJwt(new TokenModelJwt()
+                    var token = JwtHelper.IssueJwt(new TokenModelJwt
                     {
                         UserId = user.UserId,
                         RoleId = user.RoleId,
@@ -256,14 +211,13 @@
                     });
                     _cacheService.AddOrUpdate(user.UserId.ToString(), token);
                 }
-                return content.OK("鏇存敼鎴愬姛");
+
+                return WebResponseContent.Instance.OK("鏇存敼鎴愬姛");
             }
-            catch (Exception ex)
+            catch (Exception)
             {
-                message = ex.Message;
-                content.Error("鏈嶅姟鍣ㄤ簡鐐归棶棰�,璇风◢鍚庡啀璇�");
+                return WebResponseContent.Instance.Error("鏈嶅姟鍣ㄤ簡鐐归棶棰�,璇风◢鍚庡啀璇�");
             }
-            return content;
         }
     }
 }

--
Gitblit v1.9.3