SysAdminController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * 后台管理员
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using MySystem.BsModels;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. namespace MySystem.Areas.Admin.Controllers
  19. {
  20. [Area("Admin")]
  21. [Route("Admin/[controller]/[action]")]
  22. public class SysAdminController : BaseController
  23. {
  24. public SysAdminController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["BsSqlConnStr"].ToString();
  27. }
  28. #region 后台管理员列表
  29. /// <summary>
  30. /// 根据条件查询后台管理员列表
  31. /// </summary>
  32. /// <returns></returns>
  33. public IActionResult Index(SysAdmin data, string right)
  34. {
  35. ViewBag.RightInfo = RightInfo;
  36. ViewBag.right = right;
  37. string Condition = "";
  38. Condition += "AdminName:\"" + data.AdminName + "\",";
  39. Condition += "RealName:\"" + data.RealName + "\",";
  40. Condition += "Role:\"" + data.Role + "\",";
  41. if (!string.IsNullOrEmpty(Condition))
  42. {
  43. Condition = Condition.TrimEnd(',');
  44. Condition = ", where: {" + Condition + "}";
  45. }
  46. ViewBag.Condition = Condition;
  47. return View();
  48. }
  49. #endregion
  50. #region 根据条件查询后台管理员列表
  51. /// <summary>
  52. /// 后台管理员列表
  53. /// </summary>
  54. /// <returns></returns>
  55. public JsonResult IndexData(SysAdmin data, int page = 1, int limit = 30)
  56. {
  57. Dictionary<string, string> Fields = new Dictionary<string, string>();
  58. Fields.Add("AdminName", "2"); //用户名
  59. Fields.Add("RealName", "2"); //名称
  60. Fields.Add("Role", "1"); //角色
  61. Fields.Add("LastLoginDate", "3"); //最后登录时间
  62. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("SysAdmin", Fields, "Id desc", "0", page, limit, " and Status>-1 and AdminName!='admin'");
  63. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  64. foreach (Dictionary<string, object> dic in diclist)
  65. {
  66. //角色
  67. dic["Role"] = RelationClass.GetSysAdminRoleInfo(int.Parse(dic["Role"].ToString()));
  68. }
  69. return Json(obj);
  70. }
  71. #endregion
  72. #region 增加后台管理员
  73. /// <summary>
  74. /// 增加或修改后台管理员信息
  75. /// </summary>
  76. /// <returns></returns>
  77. public IActionResult Add(string right)
  78. {
  79. ViewBag.RightInfo = RightInfo;
  80. ViewBag.right = right;
  81. return View();
  82. }
  83. #endregion
  84. #region 增加后台管理员
  85. /// <summary>
  86. /// 增加或修改后台管理员信息
  87. /// </summary>
  88. /// <returns></returns>
  89. [HttpPost]
  90. public string Add(SysAdmin data)
  91. {
  92. Dictionary<string, object> Fields = new Dictionary<string, object>();
  93. Fields.Add("AdminName", data.AdminName); //用户名
  94. Fields.Add("Password", function.MD5_32(data.Password)); //密码
  95. Fields.Add("RealName", data.RealName); //名称
  96. Fields.Add("Role", data.Role); //角色
  97. Fields.Add("SeoTitle", data.SeoTitle);
  98. Fields.Add("SeoKeyword", data.SeoKeyword);
  99. Fields.Add("SeoDescription", data.SeoDescription);
  100. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Add("SysAdmin", Fields, 0);
  101. AddSysLog(data.Id.ToString(), "SysAdmin", "add");
  102. bsdb.SaveChanges();
  103. return "success";
  104. }
  105. #endregion
  106. #region 修改后台管理员
  107. /// <summary>
  108. /// 增加或修改后台管理员信息
  109. /// </summary>
  110. /// <returns></returns>
  111. public IActionResult Edit(string right, int Id = 0)
  112. {
  113. ViewBag.RightInfo = RightInfo;
  114. ViewBag.right = right;
  115. SysAdmin editData = bsdb.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
  116. ViewBag.data = editData;
  117. return View();
  118. }
  119. #endregion
  120. #region 修改后台管理员
  121. /// <summary>
  122. /// 增加或修改后台管理员信息
  123. /// </summary>
  124. /// <returns></returns>
  125. [HttpPost]
  126. public string Edit(SysAdmin data)
  127. {
  128. Dictionary<string, object> Fields = new Dictionary<string, object>();
  129. Fields.Add("AdminName", data.AdminName); //用户名
  130. if (!string.IsNullOrEmpty(data.Password)) //密码不为空就修改密码,否则不操作
  131. {
  132. Fields.Add("Password", function.MD5_32(data.Password)); //密码
  133. }
  134. Fields.Add("RealName", data.RealName); //名称
  135. Fields.Add("Role", data.Role); //角色
  136. Fields.Add("SeoTitle", data.SeoTitle);
  137. Fields.Add("SeoKeyword", data.SeoKeyword);
  138. Fields.Add("SeoDescription", data.SeoDescription);
  139. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("SysAdmin", Fields, data.Id);
  140. AddSysLog(data.Id.ToString(), "SysAdmin", "update");
  141. bsdb.SaveChanges();
  142. return "success";
  143. }
  144. #endregion
  145. #region 删除后台管理员信息
  146. /// <summary>
  147. /// 删除后台管理员信息
  148. /// </summary>
  149. /// <returns></returns>
  150. public string Delete(string Id)
  151. {
  152. string[] idlist = Id.Split(new char[] { ',' });
  153. AddSysLog(Id, "SysAdmin", "del");
  154. foreach (string subid in idlist)
  155. {
  156. int id = int.Parse(subid);
  157. Dictionary<string, object> Fields = new Dictionary<string, object>();
  158. Fields.Add("Status", -1);
  159. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("SysAdmin", Fields, id);
  160. }
  161. bsdb.SaveChanges();
  162. return "success";
  163. }
  164. #endregion
  165. #region 开启
  166. /// <summary>
  167. /// 开启
  168. /// </summary>
  169. /// <returns></returns>
  170. public string Open(string Id)
  171. {
  172. string[] idlist = Id.Split(new char[] { ',' });
  173. AddSysLog(Id, "SysAdmin", "open");
  174. foreach (string subid in idlist)
  175. {
  176. int id = int.Parse(subid);
  177. Dictionary<string, object> Fields = new Dictionary<string, object>();
  178. Fields.Add("Status", 1);
  179. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("SysAdmin", Fields, id);
  180. }
  181. bsdb.SaveChanges();
  182. return "success";
  183. }
  184. #endregion
  185. #region 关闭
  186. /// <summary>
  187. /// 关闭
  188. /// </summary>
  189. /// <returns></returns>
  190. public string Close(string Id)
  191. {
  192. string[] idlist = Id.Split(new char[] { ',' });
  193. AddSysLog(Id, "SysAdmin", "close");
  194. foreach (string subid in idlist)
  195. {
  196. int id = int.Parse(subid);
  197. Dictionary<string, object> Fields = new Dictionary<string, object>();
  198. Fields.Add("Status", 0);
  199. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("SysAdmin", Fields, id);
  200. }
  201. bsdb.SaveChanges();
  202. return "success";
  203. }
  204. #endregion
  205. #region 排序
  206. /// <summary>
  207. /// 排序
  208. /// </summary>
  209. /// <param name="Id"></param>
  210. public string Sort(int Id, int Sort)
  211. {
  212. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("SysAdmin", Sort, Id);
  213. AddSysLog(Id.ToString(), "SysAdmin", "sort");
  214. return "success";
  215. }
  216. #endregion
  217. #region 导入数据
  218. /// <summary>
  219. /// 导入数据
  220. /// </summary>
  221. /// <param name="ExcelData"></param>
  222. public string Import(string ExcelData)
  223. {
  224. ExcelData = HttpUtility.UrlDecode(ExcelData);
  225. JsonData list = JsonMapper.ToObject(ExcelData);
  226. for (int i = 1; i < list.Count; i++)
  227. {
  228. JsonData dr = list[i];
  229. bsdb.SysAdmin.Add(new SysAdmin()
  230. {
  231. CreateDate = DateTime.Now,
  232. UpdateDate = DateTime.Now,
  233. });
  234. bsdb.SaveChanges();
  235. }
  236. AddSysLog("0", "SysAdmin", "Import");
  237. return "success";
  238. }
  239. #endregion
  240. #region 导出Excel
  241. /// <summary>
  242. /// 导出Excel
  243. /// </summary>
  244. /// <returns></returns>
  245. public JsonResult ExportExcel(SysAdmin data)
  246. {
  247. Dictionary<string, string> Fields = new Dictionary<string, string>();
  248. Fields.Add("AdminName", "2"); //用户名
  249. Fields.Add("RealName", "2"); //名称
  250. Fields.Add("Role", "1"); //角色
  251. Fields.Add("LastLoginDate", "3"); //最后登录时间
  252. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("SysAdmin", Fields, "Id desc", "0", 1, 20000, "", "", false);
  253. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  254. foreach (Dictionary<string, object> dic in diclist)
  255. {
  256. }
  257. Dictionary<string, object> result = new Dictionary<string, object>();
  258. result.Add("Status", "1");
  259. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  260. result.Add("Obj", diclist);
  261. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  262. result.Add("Fields", ReturnFields);
  263. AddSysLog("0", "SysAdmin", "ExportExcel");
  264. return Json(result);
  265. }
  266. #endregion
  267. }
  268. }