| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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"));
- /// <summary>
- /// 登录验证 获取token
- /// </summary>
- /// <param name="login"></param>
- /// <returns></returns>
- [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<Cache.Models.CurrentUserInfo>.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;
- }
- /// <summary>
- /// 验证授权
- /// </summary>
- /// <returns></returns>
- private bool LoodLoop()
- {
- bool result = CheckValid();
- //return true;
- if (result)
- return true;
- else
- return false;
- }
- /// <summary>
- /// 判断是否超时
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- }
|