forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathUserController.cs
More file actions
246 lines (214 loc) · 8.13 KB
/
UserController.cs
File metadata and controls
246 lines (214 loc) · 8.13 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blog.Core.AuthHelper.OverWrite;
using Blog.Core.Common.Helper;
using Blog.Core.Common.HttpContextUser;
using Blog.Core.IRepository.UnitOfWork;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Blog.Core.Controllers
{
/// <summary>
/// 用户管理
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class UserController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
readonly ISysUserInfoServices _sysUserInfoServices;
readonly IUserRoleServices _userRoleServices;
readonly IRoleServices _roleServices;
private readonly IUser _user;
private readonly ILogger<UserController> _logger;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="unitOfWork"></param>
/// <param name="sysUserInfoServices"></param>
/// <param name="userRoleServices"></param>
/// <param name="roleServices"></param>
/// <param name="user"></param>
/// <param name="logger"></param>
public UserController(IUnitOfWork unitOfWork, ISysUserInfoServices sysUserInfoServices, IUserRoleServices userRoleServices, IRoleServices roleServices, IUser user, ILogger<UserController> logger)
{
_unitOfWork = unitOfWork;
_sysUserInfoServices = sysUserInfoServices;
_userRoleServices = userRoleServices;
_roleServices = roleServices;
_user = user;
_logger = logger;
}
/// <summary>
/// 获取全部用户
/// </summary>
/// <param name="page"></param>
/// <param name="key"></param>
/// <returns></returns>
// GET: api/User
[HttpGet]
public async Task<MessageModel<PageModel<sysUserInfo>>> Get(int page = 1, string key = "")
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
int intPageSize = 50;
var data = await _sysUserInfoServices.QueryPage(a => a.tdIsDelete != true && a.uStatus >= 0 && ((a.uLoginName != null && a.uLoginName.Contains(key)) || (a.uRealName != null && a.uRealName.Contains(key))), page, intPageSize, " uID desc ");
#region MyRegion
// 这里可以封装到多表查询,此处简单处理
var allUserRoles = await _userRoleServices.Query(d => d.IsDeleted == false);
var allRoles = await _roleServices.Query(d => d.IsDeleted == false);
var sysUserInfos = data.data;
foreach (var item in sysUserInfos)
{
var currentUserRoles = allUserRoles.Where(d => d.UserId == item.uID).Select(d => d.RoleId).ToList();
item.RIDs = currentUserRoles;
item.RoleNames = allRoles.Where(d => currentUserRoles.Contains(d.Id)).Select(d => d.Name).ToList();
}
data.data = sysUserInfos;
#endregion
return new MessageModel<PageModel<sysUserInfo>>()
{
msg = "获取成功",
success = data.dataCount >= 0,
response = data
};
}
// 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<sysUserInfo>> GetInfoByToken(string token)
{
var data = new MessageModel<sysUserInfo>();
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 = 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] sysUserInfo sysUserInfo)
{
var data = new MessageModel<string>();
sysUserInfo.uLoginPWD = MD5Helper.MD5Encrypt32(sysUserInfo.uLoginPWD);
sysUserInfo.uRemark = _user.Name;
var id = await _sysUserInfoServices.Add(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] sysUserInfo sysUserInfo)
{
// 这里使用事务处理
var data = new MessageModel<string>();
try
{
_unitOfWork.BeginTran();
if (sysUserInfo != null && sysUserInfo.uID > 0)
{
if (sysUserInfo.RIDs.Count > 0)
{
// 无论 Update Or Add , 先删除当前用户的全部 U_R 关系
var usreroles = (await _userRoleServices.Query(d => d.UserId == sysUserInfo.uID)).Select(d => d.Id.ToString()).ToArray();
if (usreroles.Count() > 0)
{
var isAllDeleted = await _userRoleServices.DeleteByIds(usreroles);
}
// 然后再执行添加操作
var userRolsAdd = new List<UserRole>();
sysUserInfo.RIDs.ForEach(rid =>
{
userRolsAdd.Add(new UserRole(sysUserInfo.uID, rid));
});
await _userRoleServices.Add(userRolsAdd);
}
data.success = await _sysUserInfoServices.Update(sysUserInfo);
_unitOfWork.CommitTran();
if (data.success)
{
data.msg = "更新成功";
data.response = sysUserInfo?.uID.ObjToString();
}
}
}
catch (Exception e)
{
_unitOfWork.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(int id)
{
var data = new MessageModel<string>();
if (id > 0)
{
var userDetail = await _sysUserInfoServices.QueryById(id);
userDetail.tdIsDelete = true;
data.success = await _sysUserInfoServices.Update(userDetail);
if (data.success)
{
data.msg = "删除成功";
data.response = userDetail?.uID.ObjToString();
}
}
return data;
}
}
}