SysAdminController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. namespace MySystem.Areas.Api.Controllers.v1
  15. {
  16. [Area("Api")]
  17. [Route("Api/v1/[controller]/[action]")]
  18. public class SysAdminController : BaseController
  19. {
  20. public SysAdminController(IHttpContextAccessor accessor) : base(accessor)
  21. {
  22. }
  23. #region 登录
  24. public JsonResult Login(string value)
  25. {
  26. value = PublicFunction.DesDecrypt(value);
  27. JsonData jsonObj = JsonMapper.ToObject(value);
  28. string userName = jsonObj["userName"].ToString(); //账号
  29. string pwd = jsonObj["pwd"].ToString(); //密码
  30. SysAdmin sys = new SysAdminService().Query(userName, function.MD5_32(pwd));
  31. if(sys.Id == 0)
  32. {
  33. return Json(new AppResultJson() { Status = "-1", Info = "账号或密码不正确" });
  34. }
  35. int RoleId = int.Parse(function.CheckInt(sys.Role));
  36. SysAdminRole Role = new SysAdminRoleService().Query(RoleId);
  37. string RightInfo = function.CheckNull(Role.RightInfo);
  38. Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
  39. obj.Add("rightList", new AdminRightList().GetRight(sys.Role, RightInfo)); //权限列表
  40. obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
  41. obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10));
  42. string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
  43. RefreshTokens check = new RefreshTokensService().Query(sys.Id);
  44. if(check.UserId == 0)
  45. {
  46. Dictionary<string, object> Fields = new Dictionary<string, object>();
  47. Fields.Add("UserId", sys.Id);
  48. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  49. Fields.Add("RefreshToken", token);
  50. new RefreshTokensService().Add(Fields);
  51. }
  52. else
  53. {
  54. Dictionary<string, object> Fields = new Dictionary<string, object>();
  55. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  56. Fields.Add("RefreshToken", token);
  57. new RefreshTokensService().Edit(Fields, sys.Id);
  58. }
  59. List<string> roles = new List<string>();
  60. roles.Add(sys.Role);
  61. obj.Add("roles", roles);
  62. obj.Add("realName", sys.RealName);
  63. obj.Add("refreshToken", token); //主token,用于刷新apiToken
  64. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  65. }
  66. #endregion
  67. #region 刷新token
  68. public JsonResult RefreshToken(string value)
  69. {
  70. value = PublicFunction.DesDecrypt(value);
  71. JsonData jsonObj = JsonMapper.ToObject(value);
  72. string refreshToken = jsonObj["refreshToken"].ToString(); //账号
  73. Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
  74. string[] data = dbconn.Decrypt3DES(refreshToken).Split('-');
  75. int Id = int.Parse(data[0]);
  76. SysAdmin sys = new SysAdminService().Query(Id);
  77. if(sys.Id == 0)
  78. {
  79. return Json(new AppResultJson() { Status = "-1", Info = "刷新失败" });
  80. }
  81. obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
  82. obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10));
  83. string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
  84. RefreshTokens check = new RefreshTokensService().Query(sys.Id);
  85. if(check.UserId == 0)
  86. {
  87. Dictionary<string, object> Fields = new Dictionary<string, object>();
  88. Fields.Add("UserId", sys.Id);
  89. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  90. Fields.Add("RefreshToken", token);
  91. new RefreshTokensService().Add(Fields);
  92. }
  93. else
  94. {
  95. Dictionary<string, object> Fields = new Dictionary<string, object>();
  96. Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
  97. Fields.Add("RefreshToken", token);
  98. new RefreshTokensService().Edit(Fields, sys.Id);
  99. }
  100. obj.Add("refreshToken", token); //主token,用于刷新apiToken
  101. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  102. }
  103. #endregion
  104. }
  105. }