HomeController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using MySystem.Models;
  8. using Library;
  9. using LitJson;
  10. using MySystemLib;
  11. namespace MySystem.Areas.Admin.Controllers
  12. {
  13. [Area("Admin")]
  14. [Route("Admin/[controller]/[action]")]
  15. public class HomeController : Controller
  16. {
  17. public WebCMSEntities db = new WebCMSEntities();
  18. public IHttpContextAccessor _accessor;
  19. public HomeController(IHttpContextAccessor accessor)
  20. {
  21. _accessor = accessor;
  22. }
  23. /// <summary>
  24. /// 主界面
  25. /// </summary>
  26. /// <returns></returns>
  27. public IActionResult Index()
  28. {
  29. if (function.GetSession(_accessor.HttpContext, "IsLogin") != "1")
  30. {
  31. return Redirect("/Admin/Home/Login");
  32. }
  33. ViewBag.SysUserName = function.GetCookie(_accessor.HttpContext, "SysUserName");
  34. List<RightDic> RightList = db.RightDic.OrderBy(m => m.Id).ToList();
  35. ViewBag.RightList = RightList;
  36. ViewBag.SysRealName = function.GetCookie(_accessor.HttpContext, "SysRealName");
  37. ViewBag.RightInfo = "," + function.GetSession(_accessor.HttpContext, "RightInfo") + ",";
  38. ViewBag.MyTaskCount = "0";
  39. List<RightDic> menus = db.RightDic.Where(m => !string.IsNullOrEmpty(m.Url)).ToList();
  40. ViewBag.menus = menus;
  41. string Role = function.GetCookie(_accessor.HttpContext, "SysRealRole");
  42. ViewBag.Role = Role;
  43. return View();
  44. }
  45. /// <summary>
  46. /// 统计界面
  47. /// </summary>
  48. /// <returns></returns>
  49. public IActionResult Main()
  50. {
  51. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  52. ViewBag.SMSCount = set.QueryCount.ToString();
  53. ViewBag.RightInfo = function.GetSession(_accessor.HttpContext, "RightInfo");
  54. List<RightDic> menus = db.RightDic.Where(m => m.MainMenu == 1).OrderByDescending(m => m.Sort).ThenBy(m => m.Id).Take(8).ToList();
  55. ViewBag.menus = menus;
  56. List<Dictionary<string, object>> statlist = new List<Dictionary<string, object>>();
  57. List<RightDic> stats = db.RightDic.Where(m => m.MainStat == 1).OrderBy(m => m.Id).ToList();
  58. foreach (RightDic sub in stats)
  59. {
  60. string url = sub.Url;
  61. if (!string.IsNullOrEmpty(url))
  62. {
  63. string table = url.Split('/')[2];
  64. Dictionary<string, object> item = new Dictionary<string, object>();
  65. item.Add("Name", function.CheckNull(sub.Name).Replace("管理", "").Replace("列表", ""));
  66. int count = 0;
  67. DataTable dt = dbconn.dtable("select count(Id) from " + table);
  68. if (dt.Rows.Count > 0)
  69. {
  70. count = int.Parse(dt.Rows[0][0].ToString());
  71. }
  72. item.Add("Count", count);
  73. statlist.Add(item);
  74. }
  75. }
  76. ViewBag.statlist = statlist;
  77. List<RightDic> datalist = db.RightDic.Where(m => m.MainDataList == 1).OrderBy(m => m.Id).ToList();
  78. ViewBag.datalist = datalist;
  79. string Role = function.GetCookie(_accessor.HttpContext, "SysRealRole");
  80. ViewBag.Role = Role;
  81. return View();
  82. }
  83. /// <summary>
  84. /// 搜索菜单
  85. /// </summary>
  86. /// <returns></returns>
  87. public IActionResult Search(string keywords = "")
  88. {
  89. ViewBag.RightInfo = "," + function.GetSession(_accessor.HttpContext, "RightInfo") + ",";
  90. List<RightDic> menus = db.RightDic.Where(m => m.Name.Contains(keywords) && !string.IsNullOrEmpty(m.Url)).ToList();
  91. ViewBag.menus = menus;
  92. return View();
  93. }
  94. #region 后台管理员登录
  95. public IActionResult Login()
  96. {
  97. function.WriteSession(_accessor.HttpContext, "GotoLogin", "");
  98. string Pwd = function.MD5_32("ym87540628");
  99. SysAdmin sys = db.SysAdmin.FirstOrDefault(m => m.AdminName == "admin");
  100. if (sys == null)
  101. {
  102. string Role = "0";
  103. List<SysAdminRole> roles = db.SysAdminRole.ToList();
  104. if (roles.Count > 0)
  105. {
  106. Role = roles[0].Id.ToString();
  107. }
  108. db.SysAdmin.Add(new SysAdmin()
  109. {
  110. AdminName = "admin", //用户名
  111. Password = Pwd, //密码
  112. RealName = "系统管理员", //名称
  113. Role = Role,
  114. });
  115. db.SaveChanges();
  116. }
  117. return View();
  118. }
  119. /// <summary>
  120. /// 后台管理员登录
  121. /// </summary>
  122. /// <param name="UserName">用户名</param>
  123. /// <param name="Pwd">密码</param>
  124. /// <returns></returns>
  125. [HttpPost]
  126. public string Login(string UserName, string Pwd, string CheckCode)
  127. {
  128. string result = "";
  129. if (function.GetCookie(_accessor.HttpContext, "checkcode") != CheckCode)
  130. {
  131. result = "验证码错误!!";
  132. }
  133. else
  134. {
  135. Pwd = function.MD5_32(Pwd);
  136. var user = db.SysAdmin.FirstOrDefault(m => m.AdminName == UserName && m.Password == Pwd);
  137. if (user != null)
  138. {
  139. user.LastLoginDate = DateTime.Now;
  140. db.SaveChanges();
  141. function.WriteCookie(_accessor.HttpContext, "SysUserName", user.AdminName);
  142. function.WriteCookie(_accessor.HttpContext, "SysRealName", user.RealName);
  143. function.WriteCookie(_accessor.HttpContext, "SysRealRole", user.Role);
  144. int RoleId = int.Parse(function.CheckInt(user.Role));
  145. SysAdminRole Role = db.SysAdminRole.FirstOrDefault(m => m.Id == RoleId) ?? new SysAdminRole();
  146. string RightInfo = function.CheckNull(Role.RightInfo);
  147. function.WriteSession(_accessor.HttpContext, "RightInfo", RightInfo);
  148. string UserId = user.Id.ToString();
  149. function.WriteCookie(_accessor.HttpContext, "SysId", UserId);
  150. function.WriteSession(_accessor.HttpContext, "IsLogin", "1");
  151. result = "success";
  152. }
  153. else
  154. {
  155. result = "用户名或密码错误";
  156. }
  157. }
  158. return result;
  159. }
  160. #endregion
  161. #region 退出登录
  162. /// <summary>
  163. /// 退出登录
  164. /// </summary>
  165. public void Quit()
  166. {
  167. function.WriteCookie(_accessor.HttpContext, "SysUserName", "", -1);
  168. function.WriteCookie(_accessor.HttpContext, "SysRealName", "", -1);
  169. function.WriteCookie(_accessor.HttpContext, "SysId", "", -1);
  170. function.WriteCookie(_accessor.HttpContext, "Role", "", -1);
  171. function.WriteCookie(_accessor.HttpContext, "IsLogin", "", -1);
  172. _accessor.HttpContext.Response.Redirect("/Admin/Home/Login");
  173. }
  174. #endregion
  175. #region 修改登录密码
  176. /// <summary>
  177. /// 修改登录密码
  178. /// </summary>
  179. /// <returns></returns>
  180. public IActionResult LoginPassword()
  181. {
  182. if (function.GetCookie(_accessor.HttpContext, "SysRealRole") == "3")
  183. {
  184. return Redirect("/Admin/Merchants/LoginPassword");
  185. }
  186. return View();
  187. }
  188. public string LoginPasswordPost(string OldPassword, string NewPassword, string NewPassword2)
  189. {
  190. if (string.IsNullOrEmpty(OldPassword))
  191. {
  192. return "请输入旧密码";
  193. }
  194. if (string.IsNullOrEmpty(NewPassword))
  195. {
  196. return "请输入新密码";
  197. }
  198. if (string.IsNullOrEmpty(NewPassword2))
  199. {
  200. return "请确认密码";
  201. }
  202. if (NewPassword != NewPassword2)
  203. {
  204. return "两次密码输入不一致";
  205. }
  206. int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
  207. SysAdmin sys = db.SysAdmin.FirstOrDefault(m => m.Id == SysId);
  208. if (sys != null)
  209. {
  210. if (function.MD5_32(OldPassword) != sys.Password)
  211. {
  212. return "旧密码不正确";
  213. }
  214. sys.Password = function.MD5_32(NewPassword);
  215. db.SaveChanges();
  216. return "success";
  217. }
  218. return "修改失败";
  219. }
  220. #endregion
  221. #region 个人资料
  222. /// <summary>
  223. /// 个人资料
  224. /// </summary>
  225. /// <returns></returns>
  226. public IActionResult AdminInfo()
  227. {
  228. if (function.GetCookie(_accessor.HttpContext, "SysRealRole") == "3")
  229. {
  230. return Redirect("/Admin/Merchants/AdminInfo");
  231. }
  232. int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
  233. SysAdmin data = db.SysAdmin.FirstOrDefault(m => m.Id == SysId) ?? new SysAdmin();
  234. ViewBag.data = data;
  235. return View();
  236. }
  237. public string AdminInfoPost(SysAdmin data)
  238. {
  239. int SysId = int.Parse(function.CheckInt(function.GetCookie(_accessor.HttpContext, "SysId")));
  240. SysAdmin sys = db.SysAdmin.FirstOrDefault(m => m.Id == SysId);
  241. if (sys != null)
  242. {
  243. sys.RealName = data.RealName;
  244. db.SaveChanges();
  245. return "success";
  246. }
  247. return "修改失败";
  248. }
  249. #endregion
  250. public string MakeData()
  251. {
  252. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  253. Dictionary<string, Dictionary<string, string>> tables = SystemPublicFuction.dbtables;
  254. foreach (string table in tables.Keys)
  255. {
  256. int num = function.get_Random(50, 100);
  257. for (int i = 0; i < num; i++)
  258. {
  259. string fields = "";
  260. string values = "";
  261. Dictionary<string, string> columns = tables[table];
  262. foreach (string column in columns.Keys)
  263. {
  264. string columnType = columns[column];
  265. fields += column + ",";
  266. if (columnType == "numeric" || columnType == "int")
  267. {
  268. values += function.get_Random(1) + ",";
  269. }
  270. else if (columnType == "datetime")
  271. {
  272. values += "'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',";
  273. }
  274. else if (columnType == "bit")
  275. {
  276. values += "1,";
  277. }
  278. else
  279. {
  280. values += "'" + function.get_Random(20) + "',";
  281. }
  282. }
  283. string sql = "insert into " + table + " (" + fields.TrimEnd(',') + ") values (" + values.TrimEnd(',') + ")";
  284. OtherMySqlConn.op(sql);
  285. }
  286. }
  287. OtherMySqlConn.connstr = "";
  288. return "ok";
  289. }
  290. }
  291. }