xiazhengtongxue
2026-03-31 2ec78835e454cecb95e48a2be2cb0b8393674ec5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using SqlSugar;
using WIDESEA_Core;
using WIDESEA_Core.Authorization;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Const;
using WIDESEA_Core.Helper;
using WIDESEA_Core.HttpContextUser;
using WIDESEA_ISystemService;
using WIDESEA_Model;
using WIDESEA_Model.Models;
using ICacheService = WIDESEA_Core.Caches.ICacheService;
using OrderByType = SqlSugar.OrderByType;
 
namespace WIDESEA_SystemService
{
    public class Sys_UserService : ServiceBase<Sys_User, IRepository<Sys_User>>, ISys_UserService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly ICacheService _cacheService;
        private readonly ISys_MenuService _menuService;
        private readonly ISys_RoleService _roleService;
 
        public IRepository<Sys_User> Repository => BaseDal;
 
        public Sys_UserService(IRepository<Sys_User> repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService roleService) : base(repository)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _cacheService = cacheService;
            _menuService = menuService;
            _roleService = roleService;
        }
 
        public WebResponseContent Login(LoginInfo loginInfo)
        {
            try
            {
                try
                {
                    loginInfo.Password = loginInfo.Password.EncryptDES(AppSecret.User);
                }
                catch { }
 
                var 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)
                    return WebResponseContent.Instance.Error("账号或密码错误");
 
                if (_menuService.GetMenuActionList(user.RoleId) is not IEnumerable<object> { } list || !list.Any())
                    return WebResponseContent.Instance.Error("无登录权限");
 
                var token = JwtHelper.IssueJwt(new TokenModelJwt
                {
                    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)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            UpdateIgnoreColOnExecute = x =>
            {
                return new List<string>
                {
                    nameof(Sys_User.UserPwd),
                    nameof(Sys_User.TenantId)
                };
            };
            return base.UpdateData(saveModel);
        }
 
        public override PageGridData<Sys_User> GetPageData(PageDataOptions options)
        {
            int roleId = options.Value?.ObjToInt() ?? -1;
 
            if (roleId <= 0)
            {
                if (App.User.IsHighestRole) return base.GetPageData(options);
                roleId = App.User.RoleId;
            }
 
            var roleIds = _roleService.GetAllChildrenRoleId(roleId).Where(x => x != roleId).ToList();
            var sugarQueryable = Db.Queryable<Sys_User>();
            ValidatePageOptions(options, ref sugarQueryable);
 
            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)
        {
            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);
 
            var content = base.AddData(saveModel);
            return content.Status
                ? WebResponseContent.Instance.OK($"用户新建成功.帐号{userName}密码{defaultPwd}")
                : content;
        }
 
        /// <summary>
        /// 个人中心获取当前用户信息
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetCurrentUserInfo()
        {
            var data = BaseDal.QueryFirst(x => x.UserId == App.User.UserId, s => new
            {
                s.UserName,
                s.UserTrueName,
                s.Address,
                s.PhoneNo,
                s.Email,
                s.Remark,
                s.Gender,
                s.RoleName,
                s.HeadImageUrl,
                s.CreateDate
            });
            return WebResponseContent.Instance.OK(null, data);
        }
 
        /// <summary>
        /// 修改密码
        /// </summary>
        public WebResponseContent ModifyPwd(string oldPwd, string newPwd)
        {
            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位");
 
                int userId = App.User.UserId;
                string userCurrentPwd = BaseDal.QueryFirst(x => x.UserId == userId, s => s.UserPwd);
 
                string encryptedOldPwd = oldPwd.EncryptDES(AppSecret.User);
                if (encryptedOldPwd != userCurrentPwd) 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 = encryptedNewPwd,
                    LastModifyPwdDate = DateTime.Now
                }, new List<string> { nameof(Sys_User.LastModifyPwdDate), nameof(Sys_User.UserPwd) });
 
                return WebResponseContent.Instance.OK("密码修改成功");
            }
            catch (Exception)
            {
                return WebResponseContent.Instance.Error("服务器了点问题,请稍后再试");
            }
        }
        public WebResponseContent ModifyUserPwd(string password, string userName)
        {
            try
            {
                password = password?.Trim();
                if (string.IsNullOrEmpty(password)) return WebResponseContent.Instance.Error("密码不能为空");
 
                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)
                {
                    var token = JwtHelper.IssueJwt(new TokenModelJwt
                    {
                        UserId = user.UserId,
                        RoleId = user.RoleId,
                        UserName = user.UserName,
                        TenantId = user.TenantId,
                    });
                    _cacheService.AddOrUpdate(user.UserId.ToString(), token);
                }
 
                return WebResponseContent.Instance.OK("更改成功");
            }
            catch (Exception)
            {
                return WebResponseContent.Instance.Error("服务器了点问题,请稍后再试");
            }
        }
    }
}