LoginController.cs
7.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
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.Json;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service;
using Hh.Mes.Service.Configure;
using Hh.Mes.Service.Logs;
using Hh.Mes.Service.SystemAuth;
using Hh.Mes.Service.WebService.Base;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Net.Http.Headers;
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using WebMvc.Common;
namespace WebMvc
{
public class LoginController : Controller
{
#region 属性
private readonly string _appKey = "hhweb";
private readonly IAuth _authUtil;
private readonly SysCompanyService sysCompanyService;
private readonly FaceLoginService faceLoginService;
private readonly SysAppService sysAppService;
private readonly LogService logService;
private readonly LineService lineService;
#endregion
public LoginController(IAuth authUtil, SysCompanyService sysCompanyService, FaceLoginService faceLoginService, SysAppService sysAppService,LogService logService, LineService lineService)
{
_authUtil = authUtil;
this.sysCompanyService = sysCompanyService;
this.faceLoginService = faceLoginService;
this.sysAppService = sysAppService;
this.logService = logService;
this.lineService = lineService;
}
public ActionResult Index()
{
var company = sysCompanyService.GetSysCompanyOne();
var sysFile = sysCompanyService.GetSysFile(company.companyId);
ViewBag.filePath = sysAppService.Download();
ViewBag.ver = company.ver;
//ViewBag.keyStr = JsEncrypt._keyStr;
ViewBag.Url = sysFile.FirstOrDefault(x => x.position == "home")?.url;
ViewBag.copyright = "Copyright © " + DateTime.Now.ToString("yyyy ") + AppSettings.GetAppSeting("copyright");
var lineCodes = lineService.GetList().Select(x => new SelectListItem
{
Value = x.lineCode,
Text = x.lineName
}).ToList();
lineCodes.Insert(0, new SelectListItem() { Value = "", Text = "全部" });
return View(lineCodes);
}
/// <summary>
/// 登入
/// </summary>
[HttpPost]
public string Login(string username, string password, string webcam, string idcard, string lineCode)
{
var resp = new Response();
try
{
if (!string.IsNullOrEmpty(idcard))
{
#region 工卡登录
Response faceResult = faceLoginService.IdCardSearchService(idcard);
if (faceResult.Code != 200)
{
resp.Code = 500;
resp.Message = faceResult.Message;
return JsonHelper.Instance.Serialize(resp);
}
username = faceResult.Result.account;
password = Encryption.Decrypt(faceResult.Result.password);
#endregion
}
else if (!string.IsNullOrEmpty(webcam))
{
#region 人脸登入
Response faceResult = faceLoginService.FaceSearchService(webcam);
if (faceResult.Code == -100 || faceResult.Code == -200)
{
resp.Code = 500;
resp.Message = faceResult.Message;
return JsonHelper.Instance.Serialize(resp);
}
username = faceResult.Result.account;
password = Encryption.Decrypt(faceResult.Result.password);
#endregion
}
password = JsEncrypt.DecodeBase64(Encoding.Default, password);
var result = _authUtil.Login(_appKey, username, password);
if (result.Code == 200)
{
resp.Token = result.Token;
resp.Result = result.currentSession;
//写登入日志
var sysLogs = new sys_login_log
{
token = result.Token,
id = result.currentSession.Id,
account = result.currentSession.Account,
name = result.currentSession.Name,
ipaddr = HttpContext.Connection.RemoteIpAddress.ToString(),
browser = Request.Headers[HeaderNames.UserAgent].ToString(),
loginTime = DateTime.Now,
};
logService.loginAfter(sysLogs);
//写cookies
//https://www.cnblogs.com/land/archive/2009/04/10/1433074.html
Response.Cookies.Append(SSOAuthAttribute.token, result.Token);
LineCodeRedisHelper.SaveLineCode(username, lineCode);
}
else
{
resp.Code = 500;
resp.Message = result.Message;
}
}
catch (Exception e)
{
resp.Code = 500;
resp.Message = e.Message;
}
return JsonHelper.Instance.Serialize(resp);
}
/// <summary>
/// 第3方登入new
/// 测试地址: https://localhost:5001/Login/OtherLogin?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiJTeXN0ZW0iLCJJU1NVUkUiOiJkaW1zIiwiZXhwIjoxNjUwNTI3MDQ2LjY2MTY2NjZ9.fJbsXxC0eirEZRW87BxfMULwwMiSMGDtOVZKdeLQHTM
/// </summary>
/// <returns></returns>
public ActionResult OtherLogin(string token)
{
var userInfo = JwtEncryption.Decode(token);
var userInfoJosn = DynamicJson.Parse(userInfo);
Response loginResponseResult = faceLoginService.OtherLoginNew(userInfoJosn.loginName);
if (loginResponseResult.Code != 200)
{
ViewBag.msgInfo = loginResponseResult.Message + "--" + userInfo;
return View();
}
var herf= sysCompanyService.GetDictionaryDictValue(MethodBase.GetCurrentMethod().Name);
var cookieOptions = new CookieOptions();
Response.Cookies.Append(SSOAuthAttribute.token, loginResponseResult.Token, cookieOptions);
//javascript 输出到页面 写入缓存跳转页面
ViewBag.js = $@"localStorage.setItem('Account', '{loginResponseResult.Result.Account}');
localStorage.setItem('Name', '{loginResponseResult.Result.Name}')
window.location.href = '{herf}'";
return View();
}
/// <summary>
/// 退出
/// </summary>
public ActionResult Logout()
{
var token = Request.Cookies[SSOAuthAttribute.token];
logService.Logout(token);
return RedirectToAction("Index", "Login");
}
}
}