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
190 lines (167 loc) · 6.37 KB
/
UserController.cs
File metadata and controls
190 lines (167 loc) · 6.37 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blog.Core.AuthHelper;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Blog.Core.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize("Permission")]
public class UserController : ControllerBase
{
IsysUserInfoServices _sysUserInfoServices;
IUserRoleServices _userRoleServices;
IRoleServices _roleServices;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sysUserInfoServices"></param>
/// <param name="userRoleServices"></param>
/// <param name="roleServices"></param>
public UserController(IsysUserInfoServices sysUserInfoServices, IUserRoleServices userRoleServices, IRoleServices roleServices)
{
_sysUserInfoServices = sysUserInfoServices;
_userRoleServices = userRoleServices;
_roleServices = roleServices;
}
// GET: api/User
[HttpGet]
public async Task<MessageModel<PageModel<sysUserInfo>>> Get(int page = 1, string key = "")
{
var data = new MessageModel<PageModel<sysUserInfo>>();
int intTotalCount = 50;
int TotalCount = 0;
int PageCount = 1;
List<sysUserInfo> sysUserInfos = new List<sysUserInfo>();
sysUserInfos = await _sysUserInfoServices.Query(a => a.tdIsDelete != true && a.uStatus >= 0);
if (!string.IsNullOrEmpty(key))
{
sysUserInfos = sysUserInfos.Where(t => (t.uLoginName != null && t.uLoginName.Contains(key)) || (t.uRealName != null && t.uRealName.Contains(key))).ToList();
}
//筛选后的数据总数
TotalCount = sysUserInfos.Count;
//筛选后的总页数
PageCount = (Math.Ceiling(TotalCount.ObjToDecimal() / intTotalCount.ObjToDecimal())).ObjToInt();
sysUserInfos = sysUserInfos.OrderByDescending(d => d.uID).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList();
var allUserRoles = await _userRoleServices.Query(d => d.IsDeleted == false);
var allRoles = await _roleServices.Query(d => d.IsDeleted == false);
foreach (var item in sysUserInfos)
{
item.uLoginPWD = "no see me";
if (item != null)
{
item.RID = (allUserRoles.Where(d => d.UserId == item.uID).FirstOrDefault()?.RoleId).ObjToInt();
item.RoleName = allRoles.Where(d=>d.Id==item.RID).FirstOrDefault()?.Name;
}
}
return new MessageModel<PageModel<sysUserInfo>>()
{
msg = "获取成功",
success = TotalCount >= 0,
response = new PageModel<sysUserInfo>()
{
page = page,
pageCount = PageCount,
dataCount = TotalCount,
data = sysUserInfos,
}
};
}
// GET: api/User/5
[HttpGet("{id}")]
public string Get(string id)
{
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;
}
// POST: api/User
[HttpPost]
public async Task<MessageModel<string>> Post([FromBody] sysUserInfo sysUserInfo)
{
var data = new MessageModel<string>();
var id = await _sysUserInfoServices.Add(sysUserInfo);
data.success = id > 0;
if (data.success)
{
data.response = id.ObjToString();
data.msg = "添加成功";
}
return data;
}
// PUT: api/User/5
[HttpPut]
public async Task<MessageModel<string>> Put([FromBody] sysUserInfo sysUserInfo)
{
var data = new MessageModel<string>();
if (sysUserInfo != null && sysUserInfo.uID > 0)
{
if (sysUserInfo.RID > 0)
{
var usrerole = await _userRoleServices.Query(d => d.UserId == sysUserInfo.uID && d.RoleId == sysUserInfo.RID);
if (usrerole.Count == 0)
{
await _userRoleServices.Add(new UserRole(sysUserInfo.uID, sysUserInfo.RID));
}
}
data.success = await _sysUserInfoServices.Update(sysUserInfo);
if (data.success)
{
data.msg = "更新成功";
data.response = sysUserInfo?.uID.ObjToString();
}
}
return data;
}
// 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;
}
}
}