BaseExtensions.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Autofac;
  6. using Bowin.Common;
  7. using EMIS.Utility;
  8. using EMIS.CommonLogic.CalendarManage;
  9. using EMIS.CommonLogic.SystemServices;
  10. using Bowin.Web.Controls.Mvc;
  11. using Bowin.Common.Utility;
  12. using EMIS.ViewModel.Account;
  13. using EMIS.Entities;
  14. using System.Web.Mvc;
  15. using EMIS.CommonLogic.Students;
  16. using EMIS.ViewModel;
  17. namespace EMIS.Web.Controls
  18. {
  19. public static class BaseExtensions
  20. {
  21. /// <summary>
  22. /// 查询用户字典信息
  23. /// </summary>
  24. public static void GetContextUser()
  25. {
  26. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  27. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  28. fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
  29. }
  30. /// <summary>
  31. /// 查询星期下拉列表
  32. /// </summary>
  33. public static List<DropdownListItem> WeekdayDropdownListItemList
  34. {
  35. get
  36. {
  37. return WeekHelper.WeekDictionary.OrderBy(x => (x.Key == 0 ? 7 : x.Key))
  38. .Select(x => new DropdownListItem
  39. {
  40. Text = x.Value,
  41. Value = x.Key
  42. }).ToList();
  43. }
  44. }
  45. /// <summary>
  46. /// 查询当前启用校历对应的学年学期ID(主键ID)
  47. /// </summary>
  48. /// <returns></returns>
  49. public static Guid GetCurrentSchoolYearID()
  50. {
  51. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  52. {
  53. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  54. var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
  55. return schoolYearView.SchoolYearID.Value;
  56. }
  57. }
  58. /// <summary>
  59. /// 查询当前启用校历对应的学年学期Code(学年学期Code)
  60. /// </summary>
  61. /// <returns></returns>
  62. public static string GetCurrentSchoolYearCode()
  63. {
  64. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  65. {
  66. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  67. var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
  68. return schoolYearView.Code;
  69. }
  70. }
  71. /// <summary>
  72. ///查询当前启用校历对应的下一个学年学期ID(主键ID)
  73. /// </summary>
  74. /// <returns></returns>
  75. public static Guid GetNextSchoolYearID()
  76. {
  77. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  78. {
  79. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  80. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  81. return nextSchoolYear.SchoolYearID.Value;
  82. }
  83. }
  84. /// <summary>
  85. /// 查询当前启用校历对应的下一个学年学期Code(学年学期Code)
  86. /// </summary>
  87. /// <returns></returns>
  88. public static string GetNextSchoolYearCode()
  89. {
  90. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  91. {
  92. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  93. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  94. return nextSchoolYear.Code;
  95. }
  96. }
  97. /// <summary>
  98. /// 查询当前启用校历对应的学年(学年)
  99. /// </summary>
  100. /// <returns></returns>
  101. public static int? GetCurrentYearID()
  102. {
  103. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  104. {
  105. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  106. var schoolYear = SchoolYearServices.GetCurrentSchoolYear();
  107. return schoolYear.Years;
  108. }
  109. }
  110. /// <summary>
  111. /// 查询当前启用校历的对应的学年的下一学年(学年)
  112. /// </summary>
  113. /// <returns></returns>
  114. public static int? GetNextYearID()
  115. {
  116. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  117. {
  118. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  119. var currentSchoolYear = SchoolYearServices.GetCurrentSchoolYear();
  120. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  121. if (nextSchoolYear.Years <= currentSchoolYear.Years)
  122. {
  123. return nextSchoolYear.Years + 1;
  124. }
  125. else
  126. {
  127. return nextSchoolYear.Years;
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// 查询当前启用的毕业学期ID(学年学期对应的ID)
  133. /// </summary>
  134. /// <returns></returns>
  135. public static Guid? GetGradSchoolYearID()
  136. {
  137. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  138. {
  139. IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
  140. //查询设置的毕业学期信息
  141. var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
  142. if (!string.IsNullOrEmpty(graduationSchoolyear))
  143. {
  144. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  145. var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
  146. if (schoolYearView != null)
  147. {
  148. return schoolYearView.SchoolYearID;
  149. }
  150. }
  151. return null;
  152. }
  153. }
  154. /// <summary>
  155. /// 查询当前启用的毕业学期Code(学年学期对应的Code)
  156. /// </summary>
  157. /// <returns></returns>
  158. public static string GetGradSchoolYearCode()
  159. {
  160. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  161. {
  162. IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
  163. //查询设置的毕业学期信息
  164. var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
  165. if (!string.IsNullOrEmpty(graduationSchoolyear))
  166. {
  167. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  168. var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
  169. if (schoolYearView != null)
  170. {
  171. return schoolYearView.Code;
  172. }
  173. }
  174. return null;
  175. }
  176. }
  177. /// <summary>
  178. /// 登录成功
  179. /// </summary>
  180. /// <param name="controller"></param>
  181. /// <param name="model"></param>
  182. /// <param name="userName"></param>
  183. public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName, Guid? userID = null)
  184. {
  185. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  186. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  187. List<string> userData = new List<string>();
  188. Sys_User userpageResult;
  189. Sys_Role defaultRole = null;
  190. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  191. {
  192. IUserServices userServices = scope.Resolve<IUserServices>();
  193. if (userID != null)
  194. {
  195. userpageResult = userServices.GetUserByUserID(userID, true);
  196. }
  197. else {
  198. userpageResult = userServices.GetUserByLoginID(userName, true);
  199. }
  200. if (userpageResult == null)
  201. {
  202. throw new Exception("获取用户失败。");
  203. }
  204. //只要不是学生,就需要获取角色,如果实在没有角色,则不用选,依然允许进入系统,反正他什么都看不到
  205. if (userpageResult.CF_Student == null)
  206. {
  207. defaultRole = userServices.GetMaxPrivilegeRoleByUserID(userpageResult.UserID);
  208. }
  209. }
  210. if (model.RememberMe)
  211. {
  212. controller.RemeberUserCookies(model);
  213. }
  214. else
  215. {
  216. controller.RemoveUserCookies(model);
  217. }
  218. userData.Add(userpageResult.LoginID);
  219. userData.Add(userpageResult.UserID.ToString());
  220. userData.Add(userpageResult.Name);
  221. userData.Add((userpageResult.CF_Staff != null) ? userpageResult.UserID.ToString() : "");//教职工ID
  222. userData.Add((userpageResult.CF_Student != null) ? userpageResult.UserID.ToString() : "");//学生ID
  223. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.DepartmentID.ToString() : "");//部门(教研室)ID
  224. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.HierarchyID : "");//部门(教研室)树结构ID
  225. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.Name : "");//部门(教研室)名称
  226. if (userpageResult.CF_Student == null)
  227. {
  228. if (userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_College != null)
  229. {
  230. userData.Add(userpageResult.CF_Staff.CollegeID.ToString());//学院ID
  231. //userData.Add(userpageResult.CF_Staff.CF_College.CampusID.ToString());//校区ID
  232. //userData.Add(userpageResult.CF_Staff.CF_College.CF_Campus.UniversityID.ToString());//学校ID
  233. }
  234. else
  235. {
  236. userData.Add("");
  237. //userData.Add("");
  238. //userData.Add("");
  239. }
  240. if (defaultRole != null)
  241. {
  242. userData.Add(defaultRole.RoleID.ToString());//默认角色ID
  243. }
  244. else
  245. {
  246. userData.Add("");
  247. }
  248. }
  249. else
  250. {
  251. //学生的后续再加
  252. userData.Add("");
  253. //userData.Add("");
  254. //userData.Add("");
  255. userData.Add("");
  256. }
  257. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  258. }
  259. /// <summary>
  260. /// 角色切换
  261. /// </summary>
  262. /// <param name="controller"></param>
  263. /// <param name="roleID"></param>
  264. public static void SwitchUserRole(this ControllerBase controller, Guid roleID)
  265. {
  266. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  267. var userData = EMIS.Utility.FormValidate.CustomPrincipal.Current.GetUserData();
  268. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  269. userData[9] = roleID.ToString();
  270. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  271. }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. /// <param name="controller"></param>
  276. /// <param name="model"></param>
  277. public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
  278. {
  279. HttpCookie cookie = new HttpCookie("username");
  280. cookie.Value = model.UserName;
  281. cookie.Expires = DateTime.Now.AddDays(1);
  282. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  283. HttpCookie cookie2 = new HttpCookie("password");
  284. cookie2.Value = model.Password;
  285. cookie2.Expires = DateTime.Now.AddDays(1);
  286. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  287. HttpCookie cookie3 = new HttpCookie("rememberme");
  288. cookie3.Value = model.RememberMe.ToString();
  289. cookie3.Expires = DateTime.Now.AddDays(1);
  290. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  291. }
  292. /// <summary>
  293. ///
  294. /// </summary>
  295. /// <param name="controller"></param>
  296. /// <param name="model"></param>
  297. public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
  298. {
  299. HttpCookie cookie = new HttpCookie("username");
  300. cookie.Value = model.UserName;
  301. cookie.Expires = DateTime.Now.AddDays(-1);
  302. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  303. HttpCookie cookie2 = new HttpCookie("password");
  304. cookie2.Value = model.Password;
  305. cookie2.Expires = DateTime.Now.AddDays(-1);
  306. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  307. HttpCookie cookie3 = new HttpCookie("rememberme");
  308. cookie3.Value = model.RememberMe.ToString();
  309. cookie3.Expires = DateTime.Now.AddDays(-1);
  310. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  311. }
  312. /// <summary>
  313. ///
  314. /// </summary>
  315. /// <param name="controller"></param>
  316. /// <returns></returns>
  317. public static LogOnModel GetUserCookies(this ControllerBase controller)
  318. {
  319. LogOnModel model = new LogOnModel();
  320. HttpCookie cookie = controller.ControllerContext.HttpContext.Request.Cookies["username"];
  321. if (cookie != null)
  322. {
  323. model.UserName = cookie.Value;
  324. }
  325. HttpCookie cookie2 = controller.ControllerContext.HttpContext.Request.Cookies["password"];
  326. if (cookie2 != null)
  327. {
  328. model.Password = cookie2.Value;
  329. }
  330. HttpCookie cookie3 = controller.ControllerContext.HttpContext.Request.Cookies["rememberme"];
  331. if (cookie3 != null)
  332. {
  333. model.RememberMe = cookie3.Value.ToLower() == "true";
  334. }
  335. return model;
  336. }
  337. /// <summary>
  338. ///
  339. /// </summary>
  340. /// <returns></returns>
  341. public static int? GetDefaultLearnformForList()
  342. {
  343. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  344. {
  345. IStudentsServices studentServices = scope.Resolve<IStudentsServices>();
  346. return studentServices.GetDefaultLearnformForList();
  347. }
  348. }
  349. }
  350. }