AliyunController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using static molilian.core.ChartDataCore;
  6. using TencentCloud.Soe.V20180724.Models;
  7. namespace molilian.api.Controllers
  8. {
  9. [ApiController]
  10. [MyAuthorize("admin")]
  11. [Route("api/[controller]/[action]")]
  12. public class AliyunController : ControllerBase
  13. {
  14. readonly IAuthorizationProvider provider = new AdminProvider();
  15. protected IHttpContextAccessor _accessor;
  16. public AliyunController(IHttpContextAccessor accessor)
  17. {
  18. _accessor = accessor;
  19. }
  20. [HttpPost]
  21. public ActionResult account_list([FromBody] JsonElement form)
  22. {
  23. var token = provider.Get(_accessor.HttpContext);
  24. int page = form.Read("current", 1);
  25. int size = form.Read("pageSize", 10);
  26. bool getTotal = form.Read("getTotal", true);
  27. string sort = form.Read<string>("sort");
  28. string order = form.Read<string>("order");
  29. string filter = string.Empty;
  30. var keyword = form.Read("keyword", string.Empty);
  31. if (!string.IsNullOrEmpty(keyword))
  32. {
  33. filter += $" AND (name like @keyword OR username like @keyword OR company like @keyword OR mobile like @keyword)";
  34. keyword = $"%{keyword}%";
  35. }
  36. filter = filter.StringTrimStart(" AND ");
  37. //排序
  38. string orderBy = "id DESC";
  39. if (!string.IsNullOrEmpty(order))
  40. {
  41. order = "descending".Equals(order) ? "DESC" : "ASC";
  42. orderBy = sort switch
  43. {
  44. _ => $"{sort} {order}",
  45. };
  46. }
  47. var result = new DBContext.Table("aliyun_pool")
  48. .Where(filter, new { keyword })
  49. .Page(size, page)
  50. .Order(orderBy)
  51. .PageList<AliyunPoolDTO>(getTotal);
  52. foreach (var item in result.List)
  53. {
  54. item.ecs_list = AliyunPoolCore.EcsList(item);
  55. item.accessKeyId = string.Empty;
  56. item.accessKeySecret = string.Empty;
  57. }
  58. return new APIResult(new { data = result });
  59. }
  60. [HttpPost]
  61. public async Task<ActionResult> update([FromBody] JsonElement form)
  62. {
  63. var clientIp = _accessor.HttpContext.GetUserIp();
  64. var token = provider.Get(_accessor.HttpContext);
  65. int id = form.Read<int>("id", 0);
  66. string name = form.Read<string>("name", string.Empty);
  67. string val = form.Read<string>("val", string.Empty);
  68. if (id == 0)
  69. {
  70. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入参数" } });
  71. }
  72. int result = 0;
  73. switch (name)
  74. {
  75. case "status":
  76. bool bVal = val.Equals("True");
  77. result = new DBContext.Table("aliyun_pool")
  78. .Add(name, bVal)
  79. .Add("last_time", DateTime.Now)
  80. .Where("id=@id", new { id })
  81. .Update();
  82. break;
  83. case "balance":
  84. var item = new DBContext.Table("aliyun_pool").Get<dynamic>("id=@id", new { id });
  85. if (string.IsNullOrEmpty(item.accessKeyId)) result = 0;
  86. decimal amout = 0;
  87. try
  88. {
  89. AliyunCore core = new AliyunCore(item.id);
  90. var response = core.QueryAccountBalance();
  91. var balance = response.Body.Data.AvailableAmount;
  92. if (decimal.TryParse(balance, out amout))
  93. {
  94. result = 1;
  95. AliyunPoolCore.UpdateDrawBalance(item.name, amout);
  96. if (item.balance != amout && amout <= item.balance_alert_threshold)
  97. {
  98. string message = $"【{item.company}】阿里云当前余额:{amout} 元";
  99. _ = NotifyCore.QYWeixinPushNotifyAsync("余额预警", message);
  100. NotifyCore.Notify(new NifyMessage
  101. {
  102. message = message,
  103. priority = NifyMessagePriority.high,
  104. tags = ["red_circle"]
  105. });
  106. }
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. _ = new LoggerLibrary("aliyun", "balance_error")
  112. .Info($"Account:{item.id}\tbalance:{amout}")
  113. .Info(ex.Message, ex.StackTrace)
  114. .SaveAsync();
  115. }
  116. break;
  117. default:
  118. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  119. }
  120. bool success = result > 0;
  121. if (success)
  122. {
  123. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"aliyun_pool:{id}:{name}", string.Empty, val);
  124. }
  125. return new APIResult(new
  126. {
  127. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  128. });
  129. }
  130. [HttpPost]
  131. public async Task<ActionResult> ecs_list([FromBody] JsonElement form)
  132. {
  133. var token = provider.Get(_accessor.HttpContext);
  134. int page = form.Read("current", 1);
  135. int size = form.Read("pageSize", 10);
  136. bool getTotal = form.Read("getTotal", true);
  137. string sort = form.Read<string>("sort");
  138. string order = form.Read<string>("order");
  139. string filter = string.Empty;
  140. var log_date = form.PathReadArray<string>("query_date[]");
  141. var accountName = form.Read("accountName", string.Empty);
  142. int channel = form.Read("channel", -1);
  143. if (!string.IsNullOrEmpty(accountName))
  144. {
  145. filter += $" AND accountName=@accountName";
  146. }
  147. else
  148. {
  149. filter += $" AND accountName!='all'";
  150. }
  151. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  152. if (log_date.Count == 2)
  153. {
  154. if (DateTime.TryParse(log_date[0], out stime) && DateTime.TryParse(log_date[1], out etime))
  155. {
  156. etime = etime.AddDays(1).AddSeconds(-1);
  157. if (etime > DateTime.Now) etime = DateTime.Now;
  158. filter += $" AND log_date BETWEEN @stime AND @etime";
  159. }
  160. }
  161. if (channel != -1)
  162. {
  163. filter += $" AND channel=@channel";
  164. }
  165. filter = filter.StringTrimStart(" AND ");
  166. //排序
  167. string orderBy = "id DESC";
  168. if (!string.IsNullOrEmpty(order))
  169. {
  170. order = "descending".Equals(order) ? "DESC" : "ASC";
  171. orderBy = sort switch
  172. {
  173. _ => $"{sort} {order}",
  174. };
  175. }
  176. using var conn = CenterHub.GetOpenConnection();
  177. var result = new DBContext.Table(conn, "aliyun_ecs")
  178. .Where(filter, new { channel, accountName, stime, etime })
  179. .Page(size, page)
  180. .Order(orderBy)
  181. .PageList<AliyunEcsDTO>(getTotal);
  182. return new APIResult(new { data = result });
  183. }
  184. }
  185. }