forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.cs
More file actions
286 lines (248 loc) · 10.1 KB
/
UserController.cs
File metadata and controls
286 lines (248 loc) · 10.1 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using AutoMapper;
using Blog.Core.AuthHelper.OverWrite;
using Blog.Core.Common.Helper;
using Blog.Core.Common.HttpContextUser;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Blog.Core.Model.ViewModels;
using Blog.Core.Repository.UnitOfWorks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Blog.Core.Controllers
{
/// <summary>
/// 用户管理
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class UserController : BaseApiController
{
private readonly IUnitOfWorkManage _unitOfWorkManage;
readonly ISysUserInfoServices _sysUserInfoServices;
readonly IUserRoleServices _userRoleServices;
readonly IRoleServices _roleServices;
private readonly IDepartmentServices _departmentServices;
private readonly IUser _user;
private readonly IMapper _mapper;
private readonly ILogger<UserController> _logger;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="unitOfWorkManage"></param>
/// <param name="sysUserInfoServices"></param>
/// <param name="userRoleServices"></param>
/// <param name="roleServices"></param>
/// <param name="departmentServices"></param>
/// <param name="user"></param>
/// <param name="mapper"></param>
/// <param name="logger"></param>
public UserController(IUnitOfWorkManage unitOfWorkManage, ISysUserInfoServices sysUserInfoServices,
IUserRoleServices userRoleServices,
IRoleServices roleServices,
IDepartmentServices departmentServices,
IUser user, IMapper mapper, ILogger<UserController> logger)
{
_unitOfWorkManage = unitOfWorkManage;
_sysUserInfoServices = sysUserInfoServices;
_userRoleServices = userRoleServices;
_roleServices = roleServices;
_departmentServices = departmentServices;
_user = user;
_mapper = mapper;
_logger = logger;
}
/// <summary>
/// 获取全部用户
/// </summary>
/// <param name="page"></param>
/// <param name="key"></param>
/// <returns></returns>
// GET: api/User
[HttpGet]
public async Task<MessageModel<PageModel<SysUserInfoDto>>> Get(int page = 1, string key = "")
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
int intPageSize = 50;
var data = await _sysUserInfoServices.QueryPage(a => a.IsDeleted != true && a.Status >= 0 && ((a.LoginName != null && a.LoginName.Contains(key)) || (a.RealName != null && a.RealName.Contains(key))), page, intPageSize, " Id desc ");
#region MyRegion
// 这里可以封装到多表查询,此处简单处理
var allUserRoles = await _userRoleServices.Query(d => d.IsDeleted == false);
var allRoles = await _roleServices.Query(d => d.IsDeleted == false);
var allDepartments = await _departmentServices.Query(d => d.IsDeleted == false);
var sysUserInfos = data.data;
foreach (var item in sysUserInfos)
{
var currentUserRoles = allUserRoles.Where(d => d.UserId == item.Id).Select(d => d.RoleId).ToList();
item.RIDs = currentUserRoles;
item.RoleNames = allRoles.Where(d => currentUserRoles.Contains(d.Id)).Select(d => d.Name).ToList();
var departmentNameAndIds = GetFullDepartmentName(allDepartments, item.DepartmentId);
item.DepartmentName = departmentNameAndIds.Item1;
item.Dids = departmentNameAndIds.Item2;
}
data.data = sysUserInfos;
#endregion
return Success(data.ConvertTo<SysUserInfoDto>(_mapper));
}
private (string, List<long>) GetFullDepartmentName(List<Department> departments, long departmentId)
{
var departmentModel = departments.FirstOrDefault(d => d.Id == departmentId);
if (departmentModel == null)
{
return ("", new List<long>());
}
var pids = departmentModel.CodeRelationship?.TrimEnd(',').Split(',').Select(d => d.ObjToLong()).ToList();
pids.Add(departmentModel.Id);
var pnams = departments.Where(d => pids.Contains(d.Id)).ToList().Select(d => d.Name).ToArray();
var fullName = string.Join("/", pnams);
return (fullName, pids);
}
// GET: api/User/5
[HttpGet("{id}")]
[AllowAnonymous]
public string Get(string id)
{
_logger.LogError("test wrong");
return "value";
}
// GET: api/User/5
/// <summary>
/// 获取用户详情根据token
/// 【无权限】
/// </summary>
/// <param name="token">令牌</param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<SysUserInfoDto>> GetInfoByToken(string token)
{
var data = new MessageModel<SysUserInfoDto>();
if (!string.IsNullOrEmpty(token))
{
var tokenModel = JwtHelper.SerializeJwt(token);
if (tokenModel != null && tokenModel.Uid > 0)
{
var userinfo = await _sysUserInfoServices.QueryById(tokenModel.Uid);
if (userinfo != null)
{
data.response = _mapper.Map<SysUserInfoDto>(userinfo);
data.success = true;
data.msg = "获取成功";
}
}
}
return data;
}
/// <summary>
/// 添加一个用户
/// </summary>
/// <param name="sysUserInfo"></param>
/// <returns></returns>
// POST: api/User
[HttpPost]
public async Task<MessageModel<string>> Post([FromBody] SysUserInfoDto sysUserInfo)
{
var data = new MessageModel<string>();
sysUserInfo.uLoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.uLoginPWD);
sysUserInfo.uRemark = _user.Name;
var id = await _sysUserInfoServices.Add(_mapper.Map<SysUserInfo>(sysUserInfo));
data.success = id > 0;
if (data.success)
{
data.response = id.ObjToString();
data.msg = "添加成功";
}
return data;
}
/// <summary>
/// 更新用户与角色
/// </summary>
/// <param name="sysUserInfo"></param>
/// <returns></returns>
// PUT: api/User/5
[HttpPut]
public async Task<MessageModel<string>> Put([FromBody] SysUserInfoDto sysUserInfo)
{
// 这里使用事务处理
var data = new MessageModel<string>();
var oldUser = await _sysUserInfoServices.QueryById(sysUserInfo.uID);
if (oldUser is not { Id: > 0 })
{
return Failed<string>("用户不存在或已被删除");
}
try
{
if (sysUserInfo.uLoginPWD != oldUser.LoginPWD)
{
oldUser.CriticalModifyTime = DateTime.Now;
}
_mapper.Map(sysUserInfo, oldUser);
_unitOfWorkManage.BeginTran();
// 无论 Update Or Add , 先删除当前用户的全部 U_R 关系
var usreroles = (await _userRoleServices.Query(d => d.UserId == oldUser.Id));
if (usreroles.Any())
{
var ids = usreroles.Select(d => d.Id.ToString()).ToArray();
var isAllDeleted = await _userRoleServices.DeleteByIds(ids);
if (!isAllDeleted)
{
return Failed("服务器更新异常");
}
}
// 然后再执行添加操作
if (sysUserInfo.RIDs.Count > 0)
{
var userRolsAdd = new List<UserRole>();
sysUserInfo.RIDs.ForEach(rid => { userRolsAdd.Add(new UserRole(oldUser.Id, rid)); });
var oldRole = usreroles.Select(s => s.RoleId).OrderBy(i => i).ToArray();
var newRole = userRolsAdd.Select(s => s.RoleId).OrderBy(i => i).ToArray();
if (!oldRole.SequenceEqual(newRole))
{
oldUser.CriticalModifyTime = DateTime.Now;
}
await _userRoleServices.Add(userRolsAdd);
}
data.success = await _sysUserInfoServices.Update(oldUser);
_unitOfWorkManage.CommitTran();
if (data.success)
{
data.msg = "更新成功";
data.response = oldUser.Id.ObjToString();
}
}
catch (Exception e)
{
_unitOfWorkManage.RollbackTran();
_logger.LogError(e, e.Message);
}
return data;
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/ApiWithActions/5
[HttpDelete]
public async Task<MessageModel<string>> Delete(long id)
{
var data = new MessageModel<string>();
if (id > 0)
{
var userDetail = await _sysUserInfoServices.QueryById(id);
userDetail.IsDeleted = true;
data.success = await _sysUserInfoServices.Update(userDetail);
if (data.success)
{
data.msg = "删除成功";
data.response = userDetail?.Id.ObjToString();
}
}
return data;
}
}
}