| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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<string, string> paras = new Dictionary<string, string>();
- 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<string, string> Dic = new Dictionary<string, string>();
- 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<Dictionary<string, string>>.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("未授权");
- }
- /// <summary>
- /// 验证授权
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- }
|