PddPoolCore.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. using System.Security.Cryptography;
  15. namespace molilian.core
  16. {
  17. public partial class PddPoolCore
  18. {
  19. private static readonly object _lockObj = new();
  20. private static IEnumerable<PddPoolDTO> _cached;
  21. public static PddPoolDTO? GetOne()
  22. {
  23. var list = List();
  24. if (!list.Any()) return null;
  25. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  26. }
  27. public static IEnumerable<PddPoolDTO> List(bool force = false)
  28. {
  29. if (!force && _cached != null) return _cached;
  30. string cache_key = $"cache:pdd_pool";
  31. var list = RedisHelper.Get<IEnumerable<PddPoolDTO>>(cache_key);
  32. if (force || list == null)
  33. {
  34. lock (_lockObj)
  35. {
  36. list = new DBContext.Table("pdd_pool")
  37. .Where("status=@status", new { status = 1 })
  38. .Select<PddPoolDTO>();
  39. if (list == null) return default;
  40. RedisHelper.Set(cache_key, list, 30 * 86400);
  41. }
  42. }
  43. _cached = list;
  44. return list;
  45. }
  46. public static void Refresh()
  47. {
  48. _ = List(true);
  49. }
  50. public static void Disabled(string name)
  51. {
  52. string cache_key = $"cache:pdd_pool:{name}:disabled";
  53. long count = RedisHelper.IncrBy(cache_key);
  54. RedisHelper.Expire(cache_key, 10);
  55. if (count > 1) return;
  56. new DBContext.Table("pdd_pool")
  57. .Add("status", 0)
  58. .Where("name=@name", new { name })
  59. .Update();
  60. _ = List(true);
  61. NotifyCore.Notify(new NifyMessage
  62. {
  63. message = $"【多多:{name}】调用异常",
  64. priority = NifyMessagePriority.high,
  65. tags = ["red_circle"]
  66. });
  67. }
  68. public static Dictionary<string, string> GenerateSignature(PddPoolDTO account, Dictionary<string, string> args)
  69. {
  70. if (args.ContainsKey("sign")) args.Remove("sign");
  71. // 对参数进行ASCII升序排序
  72. var sortedArgs = args.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
  73. // 拼接排序后的参数
  74. StringBuilder stringBuilder = new StringBuilder();
  75. foreach (var kv in sortedArgs)
  76. {
  77. stringBuilder.Append(kv.Key).Append(kv.Value);
  78. }
  79. // 在头部和尾部分别拼接client_secret
  80. string signString = account.app_secret + stringBuilder.ToString() + account.app_secret;
  81. signString = signString.MD5(false, false);
  82. if (!args.TryAdd("sign", signString))
  83. {
  84. args["sign"] = signString;
  85. }
  86. return args;
  87. }
  88. }
  89. }