AliyunController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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<dynamic>(getTotal);
  52. return new APIResult(new { data = result });
  53. }
  54. [HttpPost]
  55. public async Task<ActionResult> update([FromBody] JsonElement form)
  56. {
  57. var clientIp = _accessor.HttpContext.GetUserIp();
  58. var token = provider.Get(_accessor.HttpContext);
  59. int id = form.Read<int>("id", 0);
  60. string name = form.Read<string>("name", string.Empty);
  61. string val = form.Read<string>("val", string.Empty);
  62. if (id == 0)
  63. {
  64. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入参数" } });
  65. }
  66. int result = 0;
  67. switch (name)
  68. {
  69. case "status":
  70. bool bVal = val.Equals("True");
  71. result = new DBContext.Table("aliyun_pool")
  72. .Add(name, bVal)
  73. .Add("last_time", DateTime.Now)
  74. .Where("id=@id", new { id })
  75. .Update();
  76. break;
  77. case "balance":
  78. var item = new DBContext.Table("aliyun_pool").Get<dynamic>("id=@id", new { id });
  79. if (string.IsNullOrEmpty(item.accessKeyId)) result = 0;
  80. decimal amout = 0;
  81. try
  82. {
  83. AliyunCore core = new AliyunCore(item.id);
  84. var response = core.QueryAccountBalance();
  85. var balance = response.Body.Data.AvailableAmount;
  86. if (decimal.TryParse(balance, out amout))
  87. {
  88. result = 1;
  89. AliyunPoolCore.UpdateDrawBalance(item.name, amout);
  90. if (item.balance != amout && amout <= item.balance_alert_threshold)
  91. {
  92. string message = $"【{item.company}】阿里云当前余额:{amout} 元";
  93. NotifyCore.AnPushNotify("余额预警", message);
  94. NotifyCore.Notify(new NifyMessage
  95. {
  96. message = message,
  97. priority = NifyMessagePriority.high,
  98. tags = ["red_circle"]
  99. });
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. _ = new LoggerLibrary("aliyun", "balance_error")
  106. .Info($"Account:{item.id}\tbalance:{amout}")
  107. .Info(ex.Message, ex.StackTrace)
  108. .SaveAsync();
  109. }
  110. break;
  111. default:
  112. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  113. }
  114. bool success = result > 0;
  115. if (success)
  116. {
  117. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"aliyun_pool:{id}:{name}", string.Empty, val);
  118. }
  119. return new APIResult(new
  120. {
  121. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  122. });
  123. }
  124. [HttpPost]
  125. public ActionResult ecs_list([FromBody] JsonElement form)
  126. {
  127. var token = provider.Get(_accessor.HttpContext);
  128. int page = form.Read("current", 1);
  129. int size = form.Read("pageSize", 10);
  130. bool getTotal = form.Read("getTotal", true);
  131. string sort = form.Read<string>("sort");
  132. string order = form.Read<string>("order");
  133. string filter = string.Empty;
  134. var log_date = form.PathReadArray<string>("query_date[]");
  135. var accountName = form.Read("accountName", string.Empty);
  136. int channel = form.Read("channel", -1);
  137. if (!string.IsNullOrEmpty(accountName))
  138. {
  139. filter += $" AND accountName=@accountName";
  140. }
  141. else
  142. {
  143. filter += $" AND accountName!='all'";
  144. }
  145. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  146. if (log_date.Count == 2)
  147. {
  148. if (DateTime.TryParse(log_date[0], out stime) && DateTime.TryParse(log_date[1], out etime))
  149. {
  150. etime = etime.AddDays(1).AddSeconds(-1);
  151. if (etime > DateTime.Now) etime = DateTime.Now;
  152. filter += $" AND log_date BETWEEN @stime AND @etime";
  153. }
  154. }
  155. if (channel != -1)
  156. {
  157. filter += $" AND channel=@channel";
  158. }
  159. filter = filter.StringTrimStart(" AND ");
  160. //排序
  161. string orderBy = "id DESC";
  162. if (!string.IsNullOrEmpty(order))
  163. {
  164. order = "descending".Equals(order) ? "DESC" : "ASC";
  165. orderBy = sort switch
  166. {
  167. _ => $"{sort} {order}",
  168. };
  169. }
  170. using var conn = CenterHub.GetOpenConnection();
  171. var result = new DBContext.Table(conn, "center_daily_logs")
  172. .Where(filter, new { channel, accountName, stime, etime })
  173. .Page(size, page)
  174. .Order(orderBy)
  175. .PageList<DailyLogsDTO>(getTotal);
  176. var accounts = JdPoolCore.List();
  177. var hide_ids = accounts.Where(e => e.is_hide).Select(e => e.id).ToList();
  178. result.List = result.List.Where(e => e.channel != 1 || !hide_ids.Contains(e.accountId));
  179. return new APIResult(new { data = result });
  180. }
  181. }
  182. }