SysAdminController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * 后台管理员
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Http;
  9. using MySystem.Models.Bs;
  10. using Library;
  11. using LitJson;
  12. using Microsoft.AspNetCore.Authorization;
  13. using MySystem.Service.Bs;
  14. using System.Linq;
  15. namespace MySystem.Areas.Api.Controllers.v1
  16. {
  17. [Area("Api")]
  18. [Route("Api/v1/[controller]/[action]")]
  19. public class SysAdminController : BaseController
  20. {
  21. public SysAdminController(IHttpContextAccessor accessor) : base(accessor)
  22. {
  23. }
  24. #region 登录
  25. public JsonResult Login(string value)
  26. {
  27. value = PublicFunction.DesDecrypt(value);
  28. JsonData jsonObj = JsonMapper.ToObject(value);
  29. string userName = jsonObj["userName"].ToString(); //账号
  30. string pwd = jsonObj["pwd"].ToString(); //密码
  31. SysAdmin sys = SysAdminService.Query(userName, function.MD5_32(pwd));
  32. if (sys.Id == 0)
  33. {
  34. return Json(new AppResultJson() { Status = "-1", Info = "账号或密码不正确" });
  35. }
  36. sys.LastLoginDate = DateTime.Now;
  37. int RoleId = int.Parse(function.CheckInt(sys.Role));
  38. SysAdminRole Role = SysAdminRoleService.Query(RoleId);
  39. string RightInfo = function.CheckNull(Role.RightInfo);
  40. Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
  41. obj.Add("rightList", new AdminRightList().GetRight(sys.Role, RightInfo)); //权限列表
  42. obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
  43. obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10).ToString("yyyy-MM-dd HH:mm:ss"));
  44. string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
  45. // RefreshTokens check = RefreshTokensService.Query(" and UserId=" + sys.Id);
  46. // if (check.UserId == 0)
  47. // {
  48. // Dictionary<string, object> Fields = new Dictionary<string, object>();
  49. // Fields.Add("UserId", sys.Id);
  50. // Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  51. // Fields.Add("RefreshToken", token);
  52. // RefreshTokensService.Add(Fields);
  53. // }
  54. // else
  55. // {
  56. // Dictionary<string, object> Fields = new Dictionary<string, object>();
  57. // Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  58. // Fields.Add("RefreshToken", token);
  59. // RefreshTokensService.Edit(Fields, sys.Id);
  60. // }
  61. List<string> roles = new List<string>();
  62. roles.Add(sys.Role);
  63. obj.Add("roles", roles);
  64. obj.Add("realName", sys.RealName);
  65. obj.Add("refreshToken", token); //主token,用于刷新apiToken
  66. obj.Add("SysAdminId", sys.Id); //当前登录系统用户Id
  67. AppConfig.LoginSession.sysId = sys.Id;
  68. AppConfig.LoginSession.sysAdminName = sys.AdminName;
  69. AppConfig.LoginSession.sysRealName = sys.RealName;
  70. bsdb.SaveChanges();
  71. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  72. }
  73. #endregion
  74. #region 刷新token
  75. public JsonResult RefreshToken(string value)
  76. {
  77. value = PublicFunction.DesDecrypt(value);
  78. JsonData jsonObj = JsonMapper.ToObject(value);
  79. string refreshToken = jsonObj["refreshToken"].ToString(); //账号
  80. Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
  81. string[] data = dbconn.Decrypt3DES(refreshToken).Split('-');
  82. int Id = int.Parse(data[0]);
  83. SysAdmin sys = SysAdminService.Query(Id);
  84. if (sys.Id == 0)
  85. {
  86. return Json(new AppResultJson() { Status = "-1", Info = "刷新失败" });
  87. }
  88. obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
  89. obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10));
  90. string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
  91. RefreshTokens check = RefreshTokensService.Query(sys.Id);
  92. if (check.UserId == 0)
  93. {
  94. Dictionary<string, object> Fields = new Dictionary<string, object>();
  95. Fields.Add("UserId", sys.Id);
  96. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  97. Fields.Add("RefreshToken", token);
  98. RefreshTokensService.Add(Fields);
  99. }
  100. else
  101. {
  102. Dictionary<string, object> Fields = new Dictionary<string, object>();
  103. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  104. Fields.Add("RefreshToken", token);
  105. RefreshTokensService.Edit(Fields, sys.Id);
  106. }
  107. obj.Add("refreshToken", token); //主token,用于刷新apiToken
  108. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  109. }
  110. #endregion
  111. #region 修改当前操作人密码
  112. [Authorize]
  113. [Route("/v1/qrcodeplatemain/sysadmin/changeloginpassword")]
  114. public JsonResult ChangeLoginPassword(string value)
  115. {
  116. value = PublicFunction.DesDecrypt(value);
  117. JsonData jsonObj = JsonMapper.ToObject(value);
  118. int SysAdminId = int.Parse(jsonObj["SysAdminId"].ToString()); //系统用户Id
  119. string OldPassword = jsonObj["OldPassword"].ToString(); //旧密码
  120. string NewPassword = jsonObj["NewPassword"].ToString(); //新密码
  121. string NewPassword2 = jsonObj["NewPassword2"].ToString(); //确认新密码
  122. if (string.IsNullOrEmpty(OldPassword))
  123. {
  124. return Json(new AppResultJson() { Status = "-1", Info = "旧密码不能为空" });
  125. }
  126. if (string.IsNullOrEmpty(NewPassword))
  127. {
  128. return Json(new AppResultJson() { Status = "-1", Info = "新密码不能为空" });
  129. }
  130. if (string.IsNullOrEmpty(NewPassword2))
  131. {
  132. return Json(new AppResultJson() { Status = "-1", Info = "确认新密码不能为空" });
  133. }
  134. if (NewPassword != NewPassword2)
  135. {
  136. return Json(new AppResultJson() { Status = "-1", Info = "新密码和确认新密码不一致" });
  137. }
  138. var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.Id == SysAdminId && m.Password == function.MD5_32(OldPassword)) ?? new SysAdmin();
  139. if (sysAdmin.Id > 0)
  140. {
  141. if (NewPassword == NewPassword2)
  142. {
  143. sysAdmin.Password = function.MD5_32(NewPassword);
  144. }
  145. }
  146. else
  147. {
  148. return Json(new AppResultJson() { Status = "-1", Info = "未找到该用户信息,请检查用户Id和旧密码是否一致" });
  149. }
  150. bsdb.SaveChanges();
  151. return Json(new AppResultJson() { Status = "1", Info = "成功" });
  152. }
  153. #endregion
  154. #region 权限树
  155. public JsonResult AuthsTree(string value)
  156. {
  157. value = PublicFunction.DesDecrypt(value);
  158. JsonData jsonObj = JsonMapper.ToObject(value);
  159. Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
  160. var anths = BaseClass.GetRightJson();
  161. obj.Add("AuthsTree", anths); //主token,用于刷新apiToken
  162. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  163. }
  164. #endregion
  165. #region 系统管理-系统用户管理-系统用户信息列表
  166. [Authorize]
  167. [Route("/v1/qrcodeplatemain/sysadmin/sysadminlist")]
  168. public JsonResult SysAdminList(string value)
  169. {
  170. value = PublicFunction.DesDecrypt(value);
  171. JsonData data = JsonMapper.ToObject(value);
  172. Dictionary<string, object> Other = new Dictionary<string, object>();
  173. List<Dictionary<string, object>> dataList = SysAdminListDo(value, out Other);
  174. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
  175. }
  176. private List<Dictionary<string, object>> SysAdminListDo(string value, out Dictionary<string, object> Other)
  177. {
  178. JsonData data = JsonMapper.ToObject(value);
  179. string AdminName = data["AdminName"].ToString(); //角色名称
  180. string RealName = data["RealName"].ToString(); //名称
  181. string RoleId = data["RoleId"].ToString(); //角色Id
  182. string LastLoginDate = data["LastLoginDate"].ToString(); //最后登录时间
  183. int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
  184. int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
  185. List<RelationData> relationData = new List<RelationData>();
  186. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  187. string condition = "";
  188. if (!string.IsNullOrEmpty(data["AdminName"].ToString()))
  189. {
  190. condition += " and AdminName like '%" + AdminName + "%'";
  191. }
  192. if (!string.IsNullOrEmpty(data["RealName"].ToString()))
  193. {
  194. condition += " and RealName like '%" + RealName + "%'";
  195. }
  196. if (!string.IsNullOrEmpty(data["RoleId"].ToString()))
  197. {
  198. condition += " and Role='" + RoleId + "'";
  199. }
  200. if (!string.IsNullOrEmpty(data["LastLoginDate"].ToString()))
  201. {
  202. string[] datelist = LastLoginDate.Split(new string[] { " - " }, StringSplitOptions.None);
  203. string start = datelist[0];
  204. string end = datelist[1];
  205. condition += " and LastLoginDate>='" + start + " 00:00:00' and LastLoginDate<='" + end + " 23:59:59'";
  206. }
  207. Other = new Dictionary<string, object>();
  208. int count = 0;
  209. List<Dictionary<string, object>> source = SysAdminService.List(relationData, condition, out count, pageNum, pageSize);
  210. foreach (Dictionary<string, object> subdata in source)
  211. {
  212. Dictionary<string, object> curData = new Dictionary<string, object>();
  213. curData.Add("Id", int.Parse(subdata["Id"].ToString())); //Id
  214. curData.Add("AdminName", subdata["AdminName"].ToString()); //用户名
  215. curData.Add("RealName", subdata["RealName"].ToString()); //名称
  216. curData.Add("RoleId", int.Parse(subdata["Role"].ToString())); //角色
  217. curData.Add("LastLoginDate", subdata["LastLoginDate"].ToString() == "" ? "" : DateTime.Parse(subdata["LastLoginDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //最后登录时间
  218. dataList.Add(curData);
  219. }
  220. Other.Add("Count", count); //总数
  221. return dataList;
  222. }
  223. #endregion
  224. #region 系统管理-系统用户管理-添加系统用户信息
  225. [Authorize]
  226. [Route("/v1/qrcodeplatemain/sysadmin/addsysadminuserinfo")]
  227. public JsonResult AddSysAdminUserInfo(string value)
  228. {
  229. value = PublicFunction.DesDecrypt(value);
  230. JsonData data = JsonMapper.ToObject(value);
  231. AppResultJson result = AddSysAdminUserInfoDo(value);
  232. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  233. }
  234. private AppResultJson AddSysAdminUserInfoDo(string value)
  235. {
  236. JsonData data = JsonMapper.ToObject(value);
  237. string AdminName = data["AdminName"].ToString(); //用户名
  238. string RealName = data["RealName"].ToString(); //名称
  239. string PassWord = data["PassWord"].ToString(); //密码
  240. string RoleId = data["RoleId"].ToString(); //角色
  241. Dictionary<string, object> fields = new Dictionary<string, object>();
  242. fields.Add("AdminName", AdminName); //用户名
  243. fields.Add("RealName", RealName); //名称
  244. fields.Add("Password", function.MD5_32(PassWord)); //密码
  245. fields.Add("Role", RoleId); //角色
  246. var Id = int.Parse(SysAdminService.Add(fields).Data.ToString());
  247. if (Id > 0)
  248. {
  249. return new AppResultJson() { Status = "1", Info = "成功", Data = Id };
  250. }
  251. else
  252. {
  253. return new AppResultJson() { Status = "-1", Info = "失败", Data = Id };
  254. }
  255. }
  256. #endregion
  257. #region 系统管理-系统用户管理-编辑系统用户信息
  258. [Authorize]
  259. [Route("/v1/qrcodeplatemain/sysadmin/editsysadminuserinfo")]
  260. public JsonResult EditSysAdminUserInfo(string value)
  261. {
  262. value = PublicFunction.DesDecrypt(value);
  263. JsonData data = JsonMapper.ToObject(value);
  264. AppResultJson result = EditSysAdminUserInfoDo(value);
  265. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  266. }
  267. private AppResultJson EditSysAdminUserInfoDo(string value)
  268. {
  269. JsonData data = JsonMapper.ToObject(value);
  270. int SysAdminId = int.Parse(data["SysAdminId"].ToString()); //系统用户Id
  271. string AdminName = data["AdminName"].ToString(); //用户名
  272. string RealName = data["RealName"].ToString(); //名称
  273. string PassWord = data["PassWord"].ToString(); //密码
  274. string RoleId = data["RoleId"].ToString(); //角色
  275. var sysAdmin = bsdb.SysAdmin.FirstOrDefault(m => m.Id == SysAdminId) ?? new SysAdmin();
  276. if (sysAdmin.Id > 0)
  277. {
  278. if (!string.IsNullOrEmpty(AdminName))
  279. {
  280. sysAdmin.AdminName = AdminName;
  281. }
  282. if (!string.IsNullOrEmpty(RealName))
  283. {
  284. sysAdmin.RealName = RealName;
  285. }
  286. if (!string.IsNullOrEmpty(PassWord))
  287. {
  288. sysAdmin.Password = function.MD5_32(PassWord);
  289. }
  290. if (!string.IsNullOrEmpty(data["RoleId"].ToString()) && RoleId != sysAdmin.Role)
  291. {
  292. sysAdmin.Role = RoleId;
  293. }
  294. }
  295. bsdb.SaveChanges();
  296. return new AppResultJson() { Status = "1", Info = "成功" };
  297. }
  298. #endregion
  299. #region 系统管理-系统用户管理-删除系统用户信息
  300. [Authorize]
  301. [Route("/v1/qrcodeplatemain/sysadmin/deletesysadminuserinfo")]
  302. public JsonResult DeleteSysAdminUserInfo(string value)
  303. {
  304. value = PublicFunction.DesDecrypt(value);
  305. JsonData data = JsonMapper.ToObject(value);
  306. AppResultJson result = DeleteSysAdminUserInfoDo(value);
  307. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  308. }
  309. private AppResultJson DeleteSysAdminUserInfoDo(string value)
  310. {
  311. JsonData data = JsonMapper.ToObject(value);
  312. int SysAdminId = int.Parse(data["SysAdminId"].ToString()); //系统用户Id
  313. SysAdminService.Delete(SysAdminId);
  314. return new AppResultJson() { Status = "1", Info = "成功" };
  315. }
  316. #endregion
  317. }
  318. }