BaseExtensions.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. else
  210. {//是学生的话,检查一下密码是不是初始密码
  211. if (model.Password.ToUpper() == ("Ab!" + userpageResult.CF_Student.IDNumber.Substring(userpageResult.CF_Student.IDNumber.Length - 6)).MD5())
  212. {
  213. model.NeedChangePwd = true;
  214. model.NeedChangePwdReason = "你在重置密码后首次登录";
  215. }
  216. }
  217. }
  218. if (model.RememberMe)
  219. {
  220. controller.RemeberUserCookies(model);
  221. }
  222. else
  223. {
  224. controller.RemoveUserCookies(model);
  225. }
  226. userData.Add(userpageResult.LoginID);
  227. userData.Add(userpageResult.UserID.ToString());
  228. userData.Add(userpageResult.Name);
  229. userData.Add((userpageResult.CF_Staff != null) ? userpageResult.UserID.ToString() : "");//教职工ID
  230. userData.Add((userpageResult.CF_Student != null) ? userpageResult.UserID.ToString() : "");//学生ID
  231. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.DepartmentID.ToString() : "");//部门(教研室)ID
  232. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.HierarchyID : "");//部门(教研室)树结构ID
  233. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.Name : "");//部门(教研室)名称
  234. userData.Add(model.NeedChangePwd ? "1" : "");
  235. userData.Add(model.NeedChangePwdReason);
  236. if (userpageResult.CF_Student == null)
  237. {
  238. if (userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_College != null)
  239. {
  240. userData.Add(userpageResult.CF_Staff.CollegeID.ToString());//学院ID
  241. //userData.Add(userpageResult.CF_Staff.CF_College.CampusID.ToString());//校区ID
  242. //userData.Add(userpageResult.CF_Staff.CF_College.CF_Campus.UniversityID.ToString());//学校ID
  243. }
  244. else
  245. {
  246. userData.Add("");
  247. //userData.Add("");
  248. //userData.Add("");
  249. }
  250. if (defaultRole != null)
  251. {
  252. userData.Add(defaultRole.RoleID.ToString());//默认角色ID
  253. }
  254. else
  255. {
  256. userData.Add("");
  257. }
  258. }
  259. else
  260. {
  261. //学生的后续再加
  262. userData.Add("");
  263. //userData.Add("");
  264. //userData.Add("");
  265. userData.Add("");
  266. }
  267. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  268. }
  269. /// <summary>
  270. /// 角色切换
  271. /// </summary>
  272. /// <param name="controller"></param>
  273. /// <param name="roleID"></param>
  274. public static void SwitchUserRole(this ControllerBase controller, Guid roleID)
  275. {
  276. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  277. var userData = EMIS.Utility.FormValidate.CustomPrincipal.Current.GetUserData();
  278. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  279. userData[9] = roleID.ToString();
  280. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  281. }
  282. /// <summary>
  283. ///
  284. /// </summary>
  285. /// <param name="controller"></param>
  286. /// <param name="model"></param>
  287. public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
  288. {
  289. HttpCookie cookie = new HttpCookie("username");
  290. cookie.Value = model.UserName;
  291. cookie.Expires = DateTime.Now.AddDays(1);
  292. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  293. HttpCookie cookie2 = new HttpCookie("password");
  294. cookie2.Value = model.Password;
  295. cookie2.Expires = DateTime.Now.AddDays(1);
  296. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  297. HttpCookie cookie3 = new HttpCookie("rememberme");
  298. cookie3.Value = model.RememberMe.ToString();
  299. cookie3.Expires = DateTime.Now.AddDays(1);
  300. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  301. }
  302. /// <summary>
  303. ///
  304. /// </summary>
  305. /// <param name="controller"></param>
  306. /// <param name="model"></param>
  307. public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
  308. {
  309. HttpCookie cookie = new HttpCookie("username");
  310. cookie.Value = model.UserName;
  311. cookie.Expires = DateTime.Now.AddDays(-1);
  312. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  313. HttpCookie cookie2 = new HttpCookie("password");
  314. cookie2.Value = model.Password;
  315. cookie2.Expires = DateTime.Now.AddDays(-1);
  316. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  317. HttpCookie cookie3 = new HttpCookie("rememberme");
  318. cookie3.Value = model.RememberMe.ToString();
  319. cookie3.Expires = DateTime.Now.AddDays(-1);
  320. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  321. }
  322. /// <summary>
  323. ///
  324. /// </summary>
  325. /// <param name="controller"></param>
  326. /// <returns></returns>
  327. public static LogOnModel GetUserCookies(this ControllerBase controller)
  328. {
  329. LogOnModel model = new LogOnModel();
  330. HttpCookie cookie = controller.ControllerContext.HttpContext.Request.Cookies["username"];
  331. if (cookie != null)
  332. {
  333. model.UserName = cookie.Value;
  334. }
  335. HttpCookie cookie2 = controller.ControllerContext.HttpContext.Request.Cookies["password"];
  336. if (cookie2 != null)
  337. {
  338. model.Password = cookie2.Value;
  339. }
  340. HttpCookie cookie3 = controller.ControllerContext.HttpContext.Request.Cookies["rememberme"];
  341. if (cookie3 != null)
  342. {
  343. model.RememberMe = cookie3.Value.ToLower() == "true";
  344. }
  345. return model;
  346. }
  347. /// <summary>
  348. ///
  349. /// </summary>
  350. /// <returns></returns>
  351. public static int? GetDefaultLearnformForList()
  352. {
  353. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  354. {
  355. IStudentsServices studentServices = scope.Resolve<IStudentsServices>();
  356. return studentServices.GetDefaultLearnformForList();
  357. }
  358. }
  359. }
  360. }