GameDouyinComponentSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Newtonsoft.Json.Linq;
  6. namespace ET.Server
  7. {
  8. [FriendOf(typeof (GameDouyinComponent))]
  9. public static class GameDouyinComponentSystem
  10. {
  11. public class GameDouyinComponentAwakeSystem: AwakeSystem<GameDouyinComponent>
  12. {
  13. protected override void Awake(GameDouyinComponent self)
  14. {
  15. // 初始化数据
  16. self.InitAccessToken();
  17. }
  18. }
  19. public class GameDouyinComponentDestroySystem: DestroySystem<GameDouyinComponent>
  20. {
  21. protected override void Destroy(GameDouyinComponent self)
  22. {
  23. Log.Info($"销毁session玩家抖音组件");
  24. }
  25. }
  26. public class GameDouyinComponentUpdateSystem: UpdateSystem<GameDouyinComponent>
  27. {
  28. protected override void Update(GameDouyinComponent self)
  29. {
  30. if (TimeHelper.ClientNow() >= self.AccessTokenTime)
  31. {
  32. Log.Info($"刷新AccessToken...");
  33. self.InitAccessToken();
  34. Log.Info($"AccessToken刷新完成, AccessToken:{self.AccessToken}, AccessTokenTime:{self.AccessTokenTime}");
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// 初始化抖音接口调用凭证
  40. /// </summary>
  41. /// <param name="self"></param>
  42. private static void InitAccessToken(this GameDouyinComponent self)
  43. {
  44. // 请求头
  45. Dictionary<string, string> head = new Dictionary<string, string>();
  46. // 参数
  47. JObject param = new JObject();
  48. param.Add("appid", DouyinConst.Appid);
  49. param.Add("secret", DouyinConst.Secret);
  50. param.Add("grant_type", "client_credential");
  51. string str = HttpHelper.PostRequestByDouyin(DouyinConst.GetAccessTokenUrl, head, param);
  52. if (string.IsNullOrEmpty(str))
  53. {
  54. Log.Error($"InitAccessToken - GetAccessTokenUrl请求失败...返回为null");
  55. return;
  56. }
  57. JObject jObject = JObject.Parse(str);
  58. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  59. if (errNo != 0)
  60. { string errTips = Convert.ToString(jObject.SelectToken("err_tips"));
  61. Log.Error($"InitAccessToken - GetAccessTokenUrl请求成功...返回错误:{errNo}, 错误信息:{errTips}");
  62. return;
  63. }
  64. self.AccessToken = Convert.ToString(jObject.SelectToken("data").SelectToken("access_token"));
  65. long time = Convert.ToInt64(jObject.SelectToken("data").SelectToken("expires_in"));
  66. self.AccessTokenTime = TimeHelper.ClientNow() + time * 1000;
  67. }
  68. /// <summary>
  69. /// 获取抖音直播信息
  70. /// </summary>
  71. /// <param name="self"></param>
  72. /// <param name="token"></param>
  73. /// <returns></returns>
  74. public static JObject GetRoomInfo(this GameDouyinComponent self, string token)
  75. {
  76. if (string.IsNullOrEmpty(self.AccessToken))
  77. {
  78. Log.Error($"InitRoomId...AccessToken为null");
  79. return null;
  80. }
  81. // 请求头
  82. Dictionary<string, string> head = new Dictionary<string, string>();
  83. head.Add("X-Token", self.AccessToken);
  84. // 参数
  85. JObject param = new JObject();
  86. param.Add("token", token);
  87. string str = HttpHelper.PostRequestByDouyin(DouyinConst.GetLiveInfoUrl, head, param);
  88. if (string.IsNullOrEmpty(str))
  89. {
  90. Log.Error($"InitRoomId - GetLiveInfoUrl请求失败...返回为null");
  91. return null;
  92. }
  93. JObject jObject = JObject.Parse(str);
  94. int errNo = Convert.ToInt32(jObject.SelectToken("errcode"));
  95. if (errNo != 0)
  96. {
  97. Log.Error($"InitRoomId - GetLiveInfoUrl请求成功...返回错误:{errNo}");
  98. return null;
  99. }
  100. return jObject;
  101. }
  102. /// <summary>
  103. /// 签名验证
  104. /// </summary>
  105. /// <param name="header"> = {
  106. /// "x-nonce-str": "123456",
  107. /// "x-timestamp": "456789",
  108. /// "x-roomid": "268",
  109. /// "x-msg-type": "live_gift",
  110. /// } </param>
  111. /// <param name="bodyStr"> = "abc123你好"</param>
  112. /// <param name="secret"> = "oumuamua410"</param>
  113. /// <returns>PDcKhdlsrKEJif6uMKD2dw==</returns>
  114. public static string Signature(Dictionary<string, string> header, string bodyStr, string secret)
  115. {
  116. List<string> keyList = new List<string>(4);
  117. keyList.AddRange(header.Select(keyValuePair => keyValuePair.Key));
  118. keyList.Sort();
  119. List<string> kvList = new List<string>(4);
  120. kvList.AddRange(keyList.Select(key => key + "=" + header[key]));
  121. string urlParams = string.Join("&", kvList);
  122. string rawData = MD5Helper.StringMD5(urlParams + bodyStr + secret);
  123. byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(rawData);
  124. return Convert.ToBase64String(bytes);
  125. }
  126. }
  127. }