/
HomeController.cs
194 lines (172 loc) · 6.47 KB
/
HomeController.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YiSha.Business.OrganizationManage;
using YiSha.Business.SystemManage;
using YiSha.Entity.SystemManage;
using YiSha.Enum;
using YiSha.IdGenerator;
using YiSha.Model.Result;
using YiSha.Util.Extension;
using YiSha.Web.Code;
using YiSha.Util.Model;
using YiSha.Util;
using YiSha.Entity.OrganizationManage;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace YiSha.Admin.Web.Controllers
{
public class HomeController : BaseController
{
private MenuBLL menuBLL = new MenuBLL();
private UserBLL userBLL = new UserBLL();
private LogLoginBLL logLoginBLL = new LogLoginBLL();
private MenuAuthorizeBLL menuAuthorizeBLL = new MenuAuthorizeBLL();
#region 视图功能
[HttpGet]
[AuthorizeFilter]
public async Task<IActionResult> Index()
{
OperatorInfo operatorInfo = await Operator.Instance.Current();
TData<List<MenuEntity>> objMenu = await menuBLL.GetList(null);
List<MenuEntity> menuList = objMenu.Data;
menuList = menuList.Where(p => p.MenuStatus == StatusEnum.Yes.ParseToInt()).ToList();
if (operatorInfo.IsSystem != 1)
{
TData<List<MenuAuthorizeInfo>> objMenuAuthorize = await menuAuthorizeBLL.GetAuthorizeList(operatorInfo);
List<long?> authorizeMenuIdList = objMenuAuthorize.Data.Select(p => p.MenuId).ToList();
menuList = menuList.Where(p => authorizeMenuIdList.Contains(p.Id)).ToList();
}
ViewBag.MenuList = menuList;
ViewBag.OperatorInfo = operatorInfo;
return View();
}
[HttpGet]
public IActionResult Welcome()
{
return View();
}
[HttpGet]
public IActionResult Login()
{
if (GlobalContext.SystemConfig.Demo)
{
ViewBag.UserName = "admin";
ViewBag.Password = "123456";
}
return View();
}
[HttpPost]
public async Task<IActionResult> LoginOffJson()
{
OperatorInfo user = await Operator.Instance.Current();
if (user != null)
{
#region 退出系统
// 如果不允许同一个用户多次登录,当用户登出的时候,就不在线了
if (!GlobalContext.SystemConfig.LoginMultiple)
{
await userBLL.UpdateUser(new UserEntity { Id = user.UserId, IsOnline = 0 });
}
// 登出日志
await logLoginBLL.SaveForm(new LogLoginEntity
{
LogStatus = OperateStatusEnum.Success.ParseToInt(),
Remark = "退出系统",
IpAddress = NetHelper.Ip,
IpLocation = string.Empty,
Browser = NetHelper.Browser,
OS = NetHelper.GetOSVersion(),
ExtraRemark = NetHelper.UserAgent,
BaseCreatorId = user.UserId
});
Operator.Instance.RemoveCurrent();
new CookieHelper().RemoveCookie("RememberMe");
return Json(new TData { Tag = 1 });
#endregion
}
else
{
throw new Exception("非法请求");
}
}
[HttpGet]
public IActionResult NoPermission()
{
return View();
}
[HttpGet]
public IActionResult Error(string message)
{
ViewBag.Message = message;
return View();
}
[HttpGet]
public IActionResult Skin()
{
return View();
}
#endregion
#region 获取数据
public IActionResult GetCaptchaImage()
{
string sessionId = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>().HttpContext.Session.Id;
Tuple<string, int> captchaCode = CaptchaHelper.GetCaptchaCode();
byte[] bytes = CaptchaHelper.CreateCaptchaImage(captchaCode.Item1);
new SessionHelper().WriteSession("CaptchaCode", captchaCode.Item2);
return File(bytes, @"image/jpeg");
}
#endregion
#region 提交数据
[HttpPost]
public async Task<IActionResult> LoginJson(string userName, string password, string captchaCode)
{
TData obj = new TData();
if (string.IsNullOrEmpty(captchaCode))
{
obj.Message = "验证码不能为空";
return Json(obj);
}
if (captchaCode != new SessionHelper().GetSession("CaptchaCode").ParseToString())
{
obj.Message = "验证码错误,请重新输入";
return Json(obj);
}
TData<UserEntity> userObj = await userBLL.CheckLogin(userName, password, (int)PlatformEnum.Web);
if (userObj.Tag == 1)
{
await new UserBLL().UpdateUser(userObj.Data);
await Operator.Instance.AddCurrent(userObj.Data.WebToken);
}
string ip = NetHelper.Ip;
string browser = NetHelper.Browser;
string os = NetHelper.GetOSVersion();
string userAgent = NetHelper.UserAgent;
Action taskAction = async () =>
{
LogLoginEntity logLoginEntity = new LogLoginEntity
{
LogStatus = userObj.Tag == 1 ? OperateStatusEnum.Success.ParseToInt() : OperateStatusEnum.Fail.ParseToInt(),
Remark = userObj.Message,
IpAddress = ip,
IpLocation = IpLocationHelper.GetIpLocation(ip),
Browser = browser,
OS = os,
ExtraRemark = userAgent,
BaseCreatorId = userObj.Data?.Id
};
// 让底层不用获取HttpContext
logLoginEntity.BaseCreatorId = logLoginEntity.BaseCreatorId ?? 0;
await logLoginBLL.SaveForm(logLoginEntity);
};
AsyncTaskHelper.StartTask(taskAction);
obj.Tag = userObj.Tag;
obj.Message = userObj.Message;
return Json(obj);
}
#endregion
}
}