123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Autofac;
- using Bowin.Common;
- using EMIS.Utility;
- using EMIS.CommonLogic.CalendarManage;
- using EMIS.CommonLogic.SystemServices;
- using Bowin.Web.Controls.Mvc;
- using Bowin.Common.Utility;
- using EMIS.ViewModel.Account;
- using EMIS.Entities;
- using System.Web.Mvc;
- using EMIS.CommonLogic.Students;
- using EMIS.ViewModel;
- namespace EMIS.Web.Controls
- {
- public static class BaseExtensions
- {
- /// <summary>
- /// 查询用户字典信息
- /// </summary>
- public static void GetContextUser()
- {
- FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
-
- var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
- fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
- }
- /// <summary>
- /// 查询星期下拉列表
- /// </summary>
- public static List<DropdownListItem> WeekdayDropdownListItemList
- {
- get
- {
- return WeekHelper.WeekDictionary.OrderBy(x => (x.Key == 0 ? 7 : x.Key))
- .Select(x => new DropdownListItem
- {
- Text = x.Value,
- Value = x.Key
- }).ToList();
- }
- }
- /// <summary>
- /// 查询当前启用校历对应的学年学期ID(主键ID)
- /// </summary>
- /// <returns></returns>
- public static Guid GetCurrentSchoolYearID()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
- return schoolYearView.SchoolYearID.Value;
- }
- }
- /// <summary>
- /// 查询当前启用校历对应的学年学期Code(学年学期Code)
- /// </summary>
- /// <returns></returns>
- public static string GetCurrentSchoolYearCode()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
- return schoolYearView.Code;
- }
- }
- /// <summary>
- ///查询当前启用校历对应的下一个学年学期ID(主键ID)
- /// </summary>
- /// <returns></returns>
- public static Guid GetNextSchoolYearID()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
- return nextSchoolYear.SchoolYearID.Value;
- }
- }
- /// <summary>
- /// 查询当前启用校历对应的下一个学年学期Code(学年学期Code)
- /// </summary>
- /// <returns></returns>
- public static string GetNextSchoolYearCode()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
- return nextSchoolYear.Code;
- }
- }
- /// <summary>
- /// 查询当前启用校历对应的学年(学年)
- /// </summary>
- /// <returns></returns>
- public static int? GetCurrentYearID()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var schoolYear = SchoolYearServices.GetCurrentSchoolYear();
- return schoolYear.Years;
- }
- }
- /// <summary>
- /// 查询当前启用校历的对应的学年的下一学年(学年)
- /// </summary>
- /// <returns></returns>
- public static int? GetNextYearID()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var currentSchoolYear = SchoolYearServices.GetCurrentSchoolYear();
- var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
- if (nextSchoolYear.Years <= currentSchoolYear.Years)
- {
- return nextSchoolYear.Years + 1;
- }
- else
- {
- return nextSchoolYear.Years;
- }
- }
- }
- /// <summary>
- /// 查询当前启用的毕业学期ID(学年学期对应的ID)
- /// </summary>
- /// <returns></returns>
- public static Guid? GetGradSchoolYearID()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
- //查询设置的毕业学期信息
- var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
- if (!string.IsNullOrEmpty(graduationSchoolyear))
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
- if (schoolYearView != null)
- {
- return schoolYearView.SchoolYearID;
- }
- }
- return null;
- }
- }
- /// <summary>
- /// 查询当前启用的毕业学期Code(学年学期对应的Code)
- /// </summary>
- /// <returns></returns>
- public static string GetGradSchoolYearCode()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
- //查询设置的毕业学期信息
- var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
- if (!string.IsNullOrEmpty(graduationSchoolyear))
- {
- ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
- var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
- if (schoolYearView != null)
- {
- return schoolYearView.Code;
- }
- }
- return null;
- }
- }
- /// <summary>
- /// 登录成功
- /// </summary>
- /// <param name="controller"></param>
- /// <param name="model"></param>
- /// <param name="userName"></param>
- public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName, Guid? userID = null)
- {
- FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
- var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
- List<string> userData = new List<string>();
- Sys_User userpageResult;
- Sys_Role defaultRole = null;
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IUserServices userServices = scope.Resolve<IUserServices>();
- if (userID != null)
- {
- userpageResult = userServices.GetUserByUserID(userID, true);
- }
- else {
- userpageResult = userServices.GetUserByLoginID(userName, true);
- }
- if (userpageResult == null)
- {
- throw new Exception("获取用户失败。");
- }
- //只要不是学生,就需要获取角色,如果实在没有角色,则不用选,依然允许进入系统,反正他什么都看不到
- if (userpageResult.CF_Student == null)
- {
- defaultRole = userServices.GetMaxPrivilegeRoleByUserID(userpageResult.UserID);
- }
- }
- if (model.RememberMe)
- {
- controller.RemeberUserCookies(model);
- }
- else
- {
- controller.RemoveUserCookies(model);
- }
- userData.Add(userpageResult.LoginID);
- userData.Add(userpageResult.UserID.ToString());
- userData.Add(userpageResult.Name);
- userData.Add((userpageResult.CF_Staff != null) ? userpageResult.UserID.ToString() : "");//教职工ID
- userData.Add((userpageResult.CF_Student != null) ? userpageResult.UserID.ToString() : "");//学生ID
- userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.DepartmentID.ToString() : "");//部门(教研室)ID
- userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.HierarchyID : "");//部门(教研室)树结构ID
- userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.Name : "");//部门(教研室)名称
- if (userpageResult.CF_Student == null)
- {
- if (userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_College != null)
- {
- userData.Add(userpageResult.CF_Staff.CollegeID.ToString());//学院ID
- //userData.Add(userpageResult.CF_Staff.CF_College.CampusID.ToString());//校区ID
- //userData.Add(userpageResult.CF_Staff.CF_College.CF_Campus.UniversityID.ToString());//学校ID
- }
- else
- {
- userData.Add("");
- //userData.Add("");
- //userData.Add("");
- }
- if (defaultRole != null)
- {
- userData.Add(defaultRole.RoleID.ToString());//默认角色ID
- }
- else
- {
- userData.Add("");
- }
- }
- else
- {
- //学生的后续再加
- userData.Add("");
- //userData.Add("");
- //userData.Add("");
- userData.Add("");
- }
- fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
- }
- /// <summary>
- /// 角色切换
- /// </summary>
- /// <param name="controller"></param>
- /// <param name="roleID"></param>
- public static void SwitchUserRole(this ControllerBase controller, Guid roleID)
- {
- FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
- var userData = EMIS.Utility.FormValidate.CustomPrincipal.Current.GetUserData();
- var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
- userData[9] = roleID.ToString();
- fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="controller"></param>
- /// <param name="model"></param>
- public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
- {
- HttpCookie cookie = new HttpCookie("username");
- cookie.Value = model.UserName;
- cookie.Expires = DateTime.Now.AddDays(1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
- HttpCookie cookie2 = new HttpCookie("password");
- cookie2.Value = model.Password;
- cookie2.Expires = DateTime.Now.AddDays(1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
- HttpCookie cookie3 = new HttpCookie("rememberme");
- cookie3.Value = model.RememberMe.ToString();
- cookie3.Expires = DateTime.Now.AddDays(1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="controller"></param>
- /// <param name="model"></param>
- public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
- {
- HttpCookie cookie = new HttpCookie("username");
- cookie.Value = model.UserName;
- cookie.Expires = DateTime.Now.AddDays(-1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
- HttpCookie cookie2 = new HttpCookie("password");
- cookie2.Value = model.Password;
- cookie2.Expires = DateTime.Now.AddDays(-1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
- HttpCookie cookie3 = new HttpCookie("rememberme");
- cookie3.Value = model.RememberMe.ToString();
- cookie3.Expires = DateTime.Now.AddDays(-1);
- controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="controller"></param>
- /// <returns></returns>
- public static LogOnModel GetUserCookies(this ControllerBase controller)
- {
- LogOnModel model = new LogOnModel();
- HttpCookie cookie = controller.ControllerContext.HttpContext.Request.Cookies["username"];
- if (cookie != null)
- {
- model.UserName = cookie.Value;
- }
- HttpCookie cookie2 = controller.ControllerContext.HttpContext.Request.Cookies["password"];
- if (cookie2 != null)
- {
- model.Password = cookie2.Value;
- }
- HttpCookie cookie3 = controller.ControllerContext.HttpContext.Request.Cookies["rememberme"];
- if (cookie3 != null)
- {
- model.RememberMe = cookie3.Value.ToLower() == "true";
- }
- return model;
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public static int? GetDefaultLearnformForList()
- {
- using (var scope = AutofacHelper.Container.BeginLifetimeScope())
- {
- IStudentsServices studentServices = scope.Resolve<IStudentsServices>();
- return studentServices.GetDefaultLearnformForList();
- }
- }
- }
- }
|