using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Text; using CallCenterApi.Interface.Controllers.Base; using System.Data; using CallCenter.Utility; using CallCenterApi.Common; using CallCenterApi.Interface.Models.Common; using CallCenterApi.Model; using CallCenterApi.Interface.Models.Input; namespace CallCenterApi.Interface.Controllers { public class LoginController : BaseController { private BLL.T_Sys_RoleFunction roleFunctionBLL = new BLL.T_Sys_RoleFunction(); public DateTime ExpiredTime = DateTime.Now.AddDays(1);// Convert.ToDateTime(ReadFile(HttpRuntime.AppDomainAppPath + "tools\\hykj.hy")); /// /// 登录验证 获取token /// /// /// [HttpPost] public ActionResult login(LoginModel login) { bool appResult = LoodLoop(); if (appResult) { if (string.IsNullOrEmpty(login.Username)) return Error("账号不能为空!"); if (string.IsNullOrEmpty(login.Password)) return Error("密码不能为空!"); Model.T_Sys_UserAccount loginCode = new BLL.T_Sys_UserAccount().LoginGetModel(login.Username, login.Password); Model.T_Sys_UserAccount loginwork = new BLL.T_Sys_UserAccount().LogworkGetModel(login.Username, login.Password); if (loginCode != null|| loginwork != null ) { Model.T_Sys_UserAccount loginUser = null; string deptcode = ""; if (loginCode != null) loginUser = loginCode; else loginUser = loginwork; Model.T_Sys_Department deptModel = new BLL.T_Sys_Department().GetModel(loginUser.F_DeptId); if (deptModel != null) { deptcode = deptModel.F_DeptCode; } //写入登录日志 new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs() { F_LoginName = loginUser.F_UserName, F_LoginId = loginUser.F_UserId, F_Result = "登录成功", F_LoginIP = Common.DTRequest.GetIP(), F_Hostname = Common.DTRequest.GetIP(), F_LoginDate = DateTime.Now, F_Remark = "", F_State = 0 }); Cache.Models.CurrentUserInfo currentUserInfo = new Cache.Models.CurrentUserInfo() { F_UserId = loginUser.F_UserId, F_UserName = loginUser.F_UserName, F_UserCode = loginUser.F_UserCode, F_GroupId = loginUser.F_GroupId, F_RoleId = loginUser.F_RoleId, F_SeartGroupID = loginUser.F_SeartGroupID, F_WorkNumber = loginUser.F_WorkNumber, F_DeptId = loginUser.F_DeptId, F_DeptCode = deptcode, RegionId = loginUser.RegionId }; var token = FormsPrincipal.GetCookieValue(currentUserInfo.F_UserCode, currentUserInfo); //放入缓存 CacheHelper.Insert(token, currentUserInfo, 1440); return Success("登录成功", new { token = token }); } else { int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs() { F_LoginName = login.Username, F_LoginId = -1, F_Result = "登录失败:帐号-" + login.Username + ";分机号-" + login.ExtensionPhone, F_LoginIP = Common.DTRequest.GetIP(), F_Hostname = Common.DTRequest.GetIP(), F_LoginDate = DateTime.Now, F_Remark = "", F_State = 0 }); return Error("账号或密码错误,请重新登录"); } } else { return Error("授权过期,请联系系统厂家。"); } } public ActionResult Logout(string token = "") { int userId = CurrentUser.UserData.F_UserId; string userName = CurrentUser.UserData.F_UserName; if (Request.IsAuthenticated) { //写入登录日志 new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs() { F_LoginName = userName, F_LoginId = userId, F_Result = "退出成功", F_LoginIP = Common.DTRequest.GetIP(), F_Hostname = Common.DTRequest.GetIP(), F_LoginOutDate = DateTime.Now, F_Remark = "", F_State = 0 }); CacheHelper.Remove(token); } return Success("退出成功"); } public ActionResult Authority(string token = "", string url = "") { ActionResult res = NoToken("未知错误,请重新登录"); if (Request.IsAuthenticated) { var roleId = CurrentUser.UserData.F_RoleId; var role = new BLL.T_Sys_RoleInfo().GetModel(roleId); if (role != null) { var roleFunctionList = roleFunctionBLL.DataTableToList(roleFunctionBLL.GetList(" F_RoleId=" + role.F_RoleId).Tables[0]); var moduleFunction = new BLL.T_Sys_ModuleFunctions().GetModel(url); if (moduleFunction != null) { var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId); if (single != null) { return Success("", moduleFunction.F_OptUrl); } } } return UnAuthorized("未授权"); } return res; } /// /// 验证授权 /// /// private bool LoodLoop() { bool result = CheckValid(); //return true; if (result) return true; else return false; } /// /// 判断是否超时 /// /// private bool CheckValid() { return DateTime.Now < ExpiredTime; } private static string ReadFile(string filepath) { string str = System.IO.File.ReadAllText(filepath, Encoding.UTF8); return DESEncrypt.Decrypt(str); } } }