using System.Collections.Generic; using System.Linq; using System.Net; using Newtonsoft.Json.Linq; namespace ET.Server { /// /// 抖音推送数据, http回调 /// [HttpHandler(SceneType.RouterManager, "/tk")] public class HttpCommentHandler: IHttpHandler { public async ETTask Handle(Entity domain, HttpListenerContext context) { string xnoncestr = context.Request.Headers["x-nonce-str"]; string xtimestamp = context.Request.Headers["x-timestamp"]; string xsignature = context.Request.Headers["x-signature"]; string xroomId = context.Request.Headers["x-roomid"]; string msgType = context.Request.Headers["x-msg-type"]; if (string.IsNullOrEmpty(xroomId)) { Log.Error($"抖音推送数据http回调 找不到请求头:x-roomid"); return; } if (string.IsNullOrEmpty(msgType)) { Log.Error($"抖音推送数据http回调 找不到请求头:x-msg-type"); return; } string bodyStr = await HttpHelper.GetBodyParameter(context); if (string.IsNullOrEmpty(bodyStr)) { Log.Error($"抖音推送数据http回调 body为空"); return; } // 验签 Dictionary header = new Dictionary(); header.Add("x-nonce-str", xnoncestr); header.Add("x-timestamp", (string.IsNullOrEmpty(xtimestamp)? 0 : long.Parse(xtimestamp)).ToString()); header.Add("x-roomid", xroomId); header.Add("x-msg-type", msgType); string res = HttpHelper.Signature(header, bodyStr, DouyinConst.SignatureSecret); if (!xsignature.Equals(res)) { Log.Warning($"抖音推送数据http回调 验签数据不合法 xsignature={xsignature}, res={res}"); return; } long roomId = long.Parse(xroomId); switch (msgType) { case "live_comment": { // 直播间评论 Log.Debug($"收到抖音推送http回调 评论回调..."); LiveComment(domain, context, bodyStr, roomId); break; } case "live_gift": { // 直播间送礼 Log.Debug($"收到抖音推送http回调 刷礼物回调..."); LiveGift(domain, context, bodyStr, roomId); break; } case "live_like": { // 直播间点赞 Log.Debug($"收到抖音推送http回调 点赞回调..."); LiveLike(domain, context, bodyStr, roomId); break; } } await ETTask.CompletedTask; } /// /// 评论 /// /// /// /// /// private static async void LiveComment(Entity domain, HttpListenerContext context, string bodyStr, long roomId) { HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse(); JArray jsonArray = JArray.Parse(bodyStr); foreach (JToken jToken in jsonArray) { JObject jsonObject = (JObject)jToken; string msgId = (string)jsonObject["msg_id"]; string secOpenId = (string)jsonObject["sec_openid"]; string content = (string)jsonObject["content"]; string avatarUrl = (string)jsonObject["avatar_url"]; string nickname = (string)jsonObject["nickname"]; long timestamp = (long)jsonObject["timestamp"]; Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, content={content}"); // 判断参数 if (string.IsNullOrEmpty(secOpenId) || roomId <= 0) { response.Error = ErrorCode.ERR_ParameterError; response.Message = "参数错误"; HttpHelper.Response(context, response); return; } List list = RealmGateAddressHelper.GetAllGame(1); foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 })) { MessageHelper.SendActor(config.InstanceId, new R2G_LiveComment() { OpenId = secOpenId, RoomId = roomId, Content = content.Trim(), NickName = nickname, Url = avatarUrl}); break; } HttpHelper.Response(context, response); } await ETTask.CompletedTask; } /// /// 刷礼物 /// /// /// /// /// private static async void LiveGift(Entity domain, HttpListenerContext context, string bodyStr, long roomId) { HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse(); JArray jsonArray = JArray.Parse(bodyStr); foreach (JToken jToken in jsonArray) { JObject jsonObject = (JObject)jToken; string msgId = (string)jsonObject["msg_id"]; string secOpenId = (string)jsonObject["sec_openid"]; // 加密的礼物id string secGiftId = (string)jsonObject["sec_gift_id"]; // 送出的礼物数量 long giftNum = (long)jsonObject["gift_num"]; // 礼物总价值,单位分 long giftValue = (long)jsonObject["gift_value"]; string avatarUrl = (string)jsonObject["avatar_url"]; string nickname = (string)jsonObject["nickname"]; long timestamp = (long)jsonObject["timestamp"]; Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, secGiftId={secGiftId}, giftNum={giftNum}, giftValue={giftValue}"); // 判断参数 if (string.IsNullOrEmpty(secOpenId) || roomId <= 0) { response.Error = ErrorCode.ERR_ParameterError; response.Message = "参数错误"; HttpHelper.Response(context, response); return; } List list = RealmGateAddressHelper.GetAllGame(1); foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 })) { MessageHelper.SendActor(config.InstanceId, new R2G_LiveGift() { OpenId = secOpenId, RoomId = roomId, NickName = nickname, Url = avatarUrl, GiftId = secGiftId, GiftNum = giftNum, GiftValue = giftValue}); break; } HttpHelper.Response(context, response); } await ETTask.CompletedTask; } /// /// 点赞 /// /// /// /// /// private static async void LiveLike(Entity domain, HttpListenerContext context, string bodyStr, long roomId) { HttpDouyinApiCallbackResponse response = new HttpDouyinApiCallbackResponse(); JArray jsonArray = JArray.Parse(bodyStr); foreach (JToken jToken in jsonArray) { JObject jsonObject = (JObject)jToken; string msgId = (string)jsonObject["msg_id"]; string secOpenId = (string)jsonObject["sec_openid"]; long likeNum = (long)jsonObject["like_num"]; string avatarUrl = (string)jsonObject["avatar_url"]; string nickname = (string)jsonObject["nickname"]; long timestamp = (long)jsonObject["timestamp"]; Log.Debug($"body参数: openid={secOpenId}, roomId={roomId}, likes={likeNum}"); // 判断参数 if (string.IsNullOrEmpty(secOpenId) || roomId <= 0 || likeNum < 0) { response.Error = ErrorCode.ERR_ParameterError; response.Message = "参数错误"; HttpHelper.Response(context, response); return; } List list = RealmGateAddressHelper.GetAllGame(1); foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 })) { MessageHelper.SendActor(config.InstanceId, new R2G_LiveLike() { OpenId = secOpenId, RoomId = roomId, NickName = nickname, Url = avatarUrl, Likes = likeNum}); break; } HttpHelper.Response(context, response); } await ETTask.CompletedTask; } } }