UserController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using MiniWebApi.Handler;
  2. using MiniWebApi.Network;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Text;
  11. using Vinno.vCloud.Disk.Database;
  12. using Vinno.vCloud.Disk.Database.Dto;
  13. using Vinno.vCloud.Disk.UFile;
  14. using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Authorization;
  15. using System.Text.RegularExpressions;
  16. namespace Vinno.vCloud.Disk.Controllers
  17. {
  18. [WebApiHandler("User")]
  19. internal class UserController : BaseHandler
  20. {
  21. /// <summary>
  22. /// 获取用户列表
  23. /// </summary>
  24. /// <param name="context"></param>
  25. [Get]
  26. public void GetUserList(WebApiHttpContext context)
  27. {
  28. var page = context.GetParam("page").ToInt();
  29. var limit = context.GetParam("limit").ToInt();
  30. var data = SqlExecute.DB.Table<User>().OrderByDescending(f => f.Id);
  31. var dtos = data.ToList();
  32. var result = new
  33. {
  34. code = 0,
  35. msg = "",
  36. count = dtos.Count(),
  37. data = dtos.Skip((page - 1) * limit).Take(limit)
  38. };
  39. context.Response.Json(result, true);
  40. }
  41. /// <summary>
  42. /// 根据用户名获取用户
  43. /// </summary>
  44. /// <param name="context"></param>
  45. [Get]
  46. public void GetUserByName(WebApiHttpContext context)
  47. {
  48. var name = context.GetParam("name");
  49. var data = SqlExecute.DB.Query(SqlExecute.DB.GetMapping<User>(), "select * from User where LOWER(Name)=?", WebUtility.UrlDecode(name).ToLower())?.FirstOrDefault();
  50. context.Response.Json(data, true);
  51. }
  52. /// <summary>
  53. /// 删除用户
  54. /// </summary>
  55. /// <param name="context"></param>
  56. [Post]
  57. public void DeleteUser(WebApiHttpContext context)
  58. {
  59. var param = context.GetParams();
  60. var ids = param.GetArrayValue("ids");
  61. var isSuccess = SqlExecute.DB.Execute($"delete from User where Id in ({string.Join(',', ids)})") >0;
  62. context.Response.Json(new
  63. {
  64. Success = isSuccess,
  65. Msg = string.Empty
  66. }, true);
  67. }
  68. /// <summary>
  69. /// 添加用户
  70. /// </summary>
  71. /// <param name="context"></param>
  72. [Post]
  73. public void AddUser(WebApiHttpContext context)
  74. {
  75. var param = context.GetParams();
  76. var userName = param.GetStringValue("userName").Trim();
  77. var role = param.GetStringValue("role").ToInt();
  78. if (string.IsNullOrWhiteSpace(userName))
  79. {
  80. //这里需要加上失败返回类型
  81. return;
  82. }
  83. User user = new User();
  84. user.Name = userName;
  85. user.Role = role;
  86. var data = SqlExecute.DB.Query(SqlExecute.DB.GetMapping<User>(),"select * from User where LOWER(Name)=?", WebUtility.UrlDecode(userName).ToLower());
  87. if (data.Count <= 0)
  88. {
  89. var isSuccess = SqlExecute.DB.Insert(user) > 0;
  90. context.Response.Json(new
  91. {
  92. Success = isSuccess,
  93. Msg = string.Empty
  94. }, true);
  95. }
  96. else
  97. {
  98. context.Response.Json(new
  99. {
  100. Success = false,
  101. Msg = "User already exist!"
  102. }, true);
  103. }
  104. }
  105. /// <summary>
  106. /// 编辑用户
  107. /// </summary>
  108. /// <param name="context"></param>
  109. [Post]
  110. public void EditUser(WebApiHttpContext context)
  111. {
  112. var param = context.GetParams();
  113. var id = param.GetStringValue("id").ToInt();
  114. var name = param.GetStringValue("userName");
  115. var role = param.GetStringValue("role").ToInt();
  116. if (string.IsNullOrWhiteSpace(name))
  117. {
  118. //这里需要加上失败返回类型
  119. return;
  120. }
  121. User user = new User();
  122. user.Id = id;
  123. user.Name = name;
  124. user.Role = role;
  125. var isSuccess = SqlExecute.DB.Update(user) > 0;
  126. context.Response.Json(new
  127. {
  128. Success = isSuccess,
  129. Msg = string.Empty
  130. }, true);
  131. }
  132. }
  133. }