Нет описания

LoginController.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Text;
  7. using CallCenterApi.Interface.Controllers.Base;
  8. using System.Data;
  9. using CallCenter.Utility;
  10. using CallCenterApi.Common;
  11. using CallCenterApi.Interface.Models.Common;
  12. using CallCenterApi.Model;
  13. using CallCenterApi.Interface.Models.Input;
  14. namespace CallCenterApi.Interface.Controllers
  15. {
  16. public class LoginController : BaseController
  17. {
  18. private BLL.T_Sys_RoleFunction roleFunctionBLL = new BLL.T_Sys_RoleFunction();
  19. public DateTime ExpiredTime = DateTime.Now.AddDays(1);// Convert.ToDateTime(ReadFile(HttpRuntime.AppDomainAppPath + "tools\\hykj.hy"));
  20. /// <summary>
  21. /// 登录验证 获取token
  22. /// </summary>
  23. /// <param name="login"></param>
  24. /// <returns></returns>
  25. [HttpPost]
  26. public ActionResult login(LoginModel login)
  27. {
  28. bool appResult = LoodLoop();
  29. if (appResult)
  30. {
  31. if (string.IsNullOrEmpty(login.Username))
  32. return Error("账号不能为空!");
  33. if (string.IsNullOrEmpty(login.Password))
  34. return Error("密码不能为空!");
  35. Model.T_Sys_UserAccount loginCode = new BLL.T_Sys_UserAccount().LoginGetModel(login.Username, login.Password);
  36. Model.T_Sys_UserAccount loginwork = new BLL.T_Sys_UserAccount().LogworkGetModel(login.Username, login.Password);
  37. if (loginCode != null|| loginwork != null )
  38. {
  39. Model.T_Sys_UserAccount loginUser = null;
  40. string deptcode = "";
  41. if (loginCode != null)
  42. loginUser = loginCode;
  43. else
  44. loginUser = loginwork;
  45. Model.T_Sys_Department deptModel = new BLL.T_Sys_Department().GetModel(loginUser.F_DeptId);
  46. if (deptModel != null)
  47. {
  48. deptcode = deptModel.F_DeptCode;
  49. }
  50. //写入登录日志
  51. new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  52. {
  53. F_LoginName = loginUser.F_UserName,
  54. F_LoginId = loginUser.F_UserId,
  55. F_Result = "登录成功",
  56. F_LoginIP = Common.DTRequest.GetIP(),
  57. F_Hostname = Common.DTRequest.GetIP(),
  58. F_LoginDate = DateTime.Now,
  59. F_Remark = "",
  60. F_State = 0
  61. });
  62. Cache.Models.CurrentUserInfo currentUserInfo = new Cache.Models.CurrentUserInfo()
  63. {
  64. F_UserId = loginUser.F_UserId,
  65. F_UserName = loginUser.F_UserName,
  66. F_UserCode = loginUser.F_UserCode,
  67. F_GroupId = loginUser.F_GroupId,
  68. F_RoleId = loginUser.F_RoleId,
  69. F_SeartGroupID = loginUser.F_SeartGroupID,
  70. F_WorkNumber = loginUser.F_WorkNumber,
  71. F_DeptId = loginUser.F_DeptId,
  72. F_DeptCode = deptcode,
  73. RegionId = loginUser.RegionId
  74. };
  75. var token = FormsPrincipal<Cache.Models.CurrentUserInfo>.GetCookieValue(currentUserInfo.F_UserCode, currentUserInfo);
  76. //放入缓存
  77. CacheHelper.Insert(token, currentUserInfo, 1440);
  78. return Success("登录成功", new
  79. {
  80. token = token
  81. });
  82. }
  83. else
  84. {
  85. int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  86. {
  87. F_LoginName = login.Username,
  88. F_LoginId = -1,
  89. F_Result = "登录失败:帐号-" + login.Username + ";分机号-" + login.ExtensionPhone,
  90. F_LoginIP = Common.DTRequest.GetIP(),
  91. F_Hostname = Common.DTRequest.GetIP(),
  92. F_LoginDate = DateTime.Now,
  93. F_Remark = "",
  94. F_State = 0
  95. });
  96. return Error("账号或密码错误,请重新登录");
  97. }
  98. }
  99. else
  100. {
  101. return Error("授权过期,请联系系统厂家。");
  102. }
  103. }
  104. public ActionResult Logout(string token = "")
  105. {
  106. int userId = CurrentUser.UserData.F_UserId;
  107. string userName = CurrentUser.UserData.F_UserName;
  108. if (Request.IsAuthenticated)
  109. {
  110. //写入登录日志
  111. new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  112. {
  113. F_LoginName = userName,
  114. F_LoginId = userId,
  115. F_Result = "退出成功",
  116. F_LoginIP = Common.DTRequest.GetIP(),
  117. F_Hostname = Common.DTRequest.GetIP(),
  118. F_LoginOutDate = DateTime.Now,
  119. F_Remark = "",
  120. F_State = 0
  121. });
  122. CacheHelper.Remove(token);
  123. }
  124. return Success("退出成功");
  125. }
  126. public ActionResult Authority(string token = "", string url = "")
  127. {
  128. ActionResult res = NoToken("未知错误,请重新登录");
  129. if (Request.IsAuthenticated)
  130. {
  131. var roleId = CurrentUser.UserData.F_RoleId;
  132. var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
  133. if (role != null)
  134. {
  135. var roleFunctionList = roleFunctionBLL.DataTableToList(roleFunctionBLL.GetList(" F_RoleId=" + role.F_RoleId).Tables[0]);
  136. var moduleFunction = new BLL.T_Sys_ModuleFunctions().GetModel(url);
  137. if (moduleFunction != null)
  138. {
  139. var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
  140. if (single != null)
  141. {
  142. return Success("", moduleFunction.F_OptUrl);
  143. }
  144. }
  145. }
  146. return UnAuthorized("未授权");
  147. }
  148. return res;
  149. }
  150. /// <summary>
  151. /// 验证授权
  152. /// </summary>
  153. /// <returns></returns>
  154. private bool LoodLoop()
  155. {
  156. bool result = CheckValid();
  157. //return true;
  158. if (result)
  159. return true;
  160. else
  161. return false;
  162. }
  163. /// <summary>
  164. /// 判断是否超时
  165. /// </summary>
  166. /// <returns></returns>
  167. private bool CheckValid()
  168. {
  169. return DateTime.Now < ExpiredTime;
  170. }
  171. private static string ReadFile(string filepath)
  172. {
  173. string str = System.IO.File.ReadAllText(filepath, Encoding.UTF8);
  174. return DESEncrypt.Decrypt(str);
  175. }
  176. }
  177. }