MapDouyinLiveLikeComponentSystem.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json.Linq;
  4. namespace ET.Server
  5. {
  6. [FriendOf(typeof (MapDouyinLiveLikeComponent))]
  7. public static class MapDouyinLiveLikeComponentSystem
  8. {
  9. public class MapDouyinLiveLikeComponentAwakeSystem: AwakeSystem<MapDouyinLiveLikeComponent>
  10. {
  11. protected override void Awake(MapDouyinLiveLikeComponent self)
  12. {
  13. Log.Info($"创建抖音直播点赞任务组件...");
  14. }
  15. }
  16. public class MapDouyinLiveLikeComponentDestroySystem: DestroySystem<MapDouyinLiveLikeComponent>
  17. {
  18. protected override void Destroy(MapDouyinLiveLikeComponent self)
  19. {
  20. Log.Info($"销毁抖音直播点赞任务组件");
  21. bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
  22. if (tokenIsNull)
  23. {
  24. return;
  25. }
  26. self.StopTask();
  27. }
  28. }
  29. public class MapDouyinLiveLikeComponentUpdateSystem: UpdateSystem<MapDouyinLiveLikeComponent>
  30. {
  31. protected override void Update(MapDouyinLiveLikeComponent self)
  32. {
  33. bool tokenIsNull = self.GetParent<Map>().TokenIsNull();
  34. if (tokenIsNull)
  35. {
  36. return;
  37. }
  38. // 运行中状态
  39. if (self.Status == 3)
  40. {
  41. return;
  42. }
  43. Log.Debug($"检查抖音直播点赞任务状态...Status={self.Status}");
  44. self.Status = self.CheckTaskStatus();
  45. switch (self.Status)
  46. {
  47. case -1:
  48. {
  49. // 接口请求出错
  50. Log.Debug($"检查抖音直播点赞任务状态 - 抖音接口请求出错");
  51. break;
  52. }
  53. case 1:
  54. {
  55. // 数据推送回调任务不存在
  56. Log.Debug($"检查抖音直播点赞任务状态 - 任务已经被删除,可能是因为主播取消挂载/已关播");
  57. break;
  58. }
  59. case 2:
  60. {
  61. // 数据推送回调任务未启动则启动
  62. self.StartTask();
  63. break;
  64. }
  65. case 3:
  66. // 数据推送回调任务运行中,不处理
  67. break;
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 启动点赞推送任务
  73. /// </summary>
  74. /// <param name="self"></param>
  75. private static void StartTask(this MapDouyinLiveLikeComponent self)
  76. {
  77. // 请求头
  78. Dictionary<string, string> head = new Dictionary<string, string>();
  79. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  80. // 参数
  81. JObject param = new JObject();
  82. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  83. param.Add("appid", DouyinConst.Appid);
  84. param.Add("msg_type", "live_like");
  85. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StartTaskUrl, head, param);
  86. if (string.IsNullOrEmpty(str))
  87. {
  88. Log.Error($"启动点赞推送任务 - StartTaskUrl 请求失败...返回为null");
  89. return;
  90. }
  91. JObject jObject = JObject.Parse(str);
  92. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  93. if (errNo != 0)
  94. {
  95. Log.Error($"启动点赞推送任务 - StartTaskUrl 请求成功...返回错误:{errNo}");
  96. return;
  97. }
  98. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  99. self.TaskId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("task_id"));
  100. }
  101. /// <summary>
  102. /// 停止点赞推送任务
  103. /// </summary>
  104. /// <param name="self"></param>
  105. private static void StopTask(this MapDouyinLiveLikeComponent self)
  106. {
  107. // 请求头
  108. Dictionary<string, string> head = new Dictionary<string, string>();
  109. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  110. // 参数
  111. JObject param = new JObject();
  112. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  113. param.Add("appid", DouyinConst.Appid);
  114. param.Add("msg_type", "live_like");
  115. string str = HttpHelper.PostRequestByDouyin(DouyinConst.StopTaskUrl, head, param);
  116. if (string.IsNullOrEmpty(str))
  117. {
  118. Log.Error($"停止点赞推送任务 - StopTaskUrl 请求失败...返回为null");
  119. return;
  120. }
  121. JObject jObject = JObject.Parse(str);
  122. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  123. if (errNo != 0)
  124. {
  125. Log.Error($"停止点赞推送任务 - StopTaskUrl 请求成功...返回错误:{errNo}");
  126. return;
  127. }
  128. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  129. }
  130. /// <summary>
  131. /// 检查点赞推送任务状态 (1-任务不存在, 2-任务未启动, 3-任务运行中)
  132. /// </summary>
  133. /// <param name="self"></param>
  134. /// <returns></returns>
  135. private static int CheckTaskStatus(this MapDouyinLiveLikeComponent self)
  136. {
  137. Dictionary<string, string> head = new Dictionary<string, string>();
  138. head.Add("access-token", self.GetParent<Map>().GetDouyinAccessToken());
  139. // 参数
  140. Dictionary<string, string> param = new Dictionary<string, string>();
  141. param.Add("roomid", self.GetParent<Map>().RoomId.ToString());
  142. param.Add("appid", DouyinConst.Appid);
  143. param.Add("msg_type", "live_like");
  144. string str = HttpHelper.GetRequestByDouyin(DouyinConst.CheckTaskUrl, head, param);
  145. if (string.IsNullOrEmpty(str))
  146. {
  147. Log.Error($"检查点赞推送任务状态 - CheckTaskUrl 请求失败...返回为null");
  148. return -1;
  149. }
  150. JObject jObject = JObject.Parse(str);
  151. int errNo = Convert.ToInt32(jObject.SelectToken("err_no"));
  152. if (errNo != 0)
  153. {
  154. Log.Error($"检查点赞推送任务状态 - CheckTaskUrl 请求成功...返回错误:{errNo}");
  155. return -1;
  156. }
  157. self.LogId = Convert.ToInt64(jObject.SelectToken("data").SelectToken("logid"));
  158. return Convert.ToInt32(jObject.SelectToken("data").SelectToken("status"));
  159. }
  160. }
  161. }