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 CallCenterApi.Common; using CallCenterApi.Interface.Models.Common; using CallCenterApi.Model; using CallCenterApi.Interface.Models.Input; using CallCenterApi.DB; using System.Web.Caching; 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")); [HttpPost] public ActionResult login(LoginModel login) { bool appResult = LoodLoop(); if (appResult) { DataTable dt = new DataTable(); try { Dictionary paras = new Dictionary(); string sql = " select * from T_Sys_UserAccount where F_UserCode=@F_UserCode and F_PassWord=@F_PassWord and F_IsDelete=0"; paras.Add("@F_UserCode", login.Username); paras.Add("@F_PassWord", login.Password); dt = DbHelperSQL.Query(sql, paras).Tables[0]; if (dt != null) { if (dt.Rows.Count > 0) { Dictionary Dic = new Dictionary(); Dic.Add("F_ID", dt.Rows[0]["F_Id"].ToString()); Dic.Add("F_UserCode", dt.Rows[0]["F_UserCode"].ToString()); Dic.Add("F_DeptId", dt.Rows[0]["F_DeptId"].ToString()); Dic.Add("F_UserName", dt.Rows[0]["F_UserName"].ToString()); Dic.Add("F_Telephone", dt.Rows[0]["F_Telephone"].ToString()); Dic.Add("F_RoleID", dt.Rows[0]["F_RoleID"].ToString()); var token = FormsPrincipal>.GetCookieValue(Dic["F_UserCode"], Dic); //放入缓存 //CacheHelper.Insert(token, Dic, 1440, System.Web.Caching.CacheItemPriority.NotRemovable, null); //放入redis缓存 RedisHelper.StringSet(token, Dic.ToJson(), new TimeSpan(24, 0, 0)); return Success("登录成功", new { token = token }); } else { return Error("账号或密码错误,请重新登录"); } } else { return Error("账号或密码错误,请重新登录"); } } catch (Exception ex) { return Error("错误:" + ex.Message); } finally { dt.Clear(); dt.Dispose(); } } else { return Error("授权过期,请联系系统厂家。"); } } public ActionResult Logout(string token = "") { if (Request.IsAuthenticated) { //CacheHelper.Remove(token); RedisHelper.KeyDelete(token); } return Success("退出成功"); } [Authority] public ActionResult Authority(string token = "", string url = "") { var roleId = Utils.StrToInt(User.UserData["F_RoleID"], 0); 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_Function().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 Success("成功"); } return UnAuthorized("未授权"); } /// /// 验证授权 /// /// 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); } } }