using System.Reflection; using Microsoft.AspNetCore.Mvc; namespace Application.Web.Controllers.Pub; /// /// 消息接口 /// [Route("[controller]/[action]")] [ApiController] [Authorize] public class MessageController : ControllerBase { private readonly IMessageService _messageService; private readonly IUnitUserService _userService; public MessageController(IMessageService messageService, IUnitUserService userService) { _messageService = messageService; _userService = userService; } /// /// 获取消息 /// /// /// /// [HttpGet] public async Task GetMessageData(int type, int page) { string userId = StateHelper.userId; RefAsync total = 0; List msgCount = await _messageService.GetNoReadCount(userId); if (type == 0) { var data = await _messageService.GetUserMessage(userId, 0, page, 10, total); return PoAction.Ok(new { data, total = total.Value, msgCount }); } else if (type == 1) { var data = await _messageService.GetUserMessage(userId, 1, page, 10, total); return PoAction.Ok(new { data, total = total.Value, msgCount }); } else if (type == 2) { var data = await _messageService.GetUserEventMessage(userId, page, 10, total); return PoAction.Ok(new { data, total = total.Value, msgCount }); } else if (type == 3) { var data = await _messageService.GetUserMailData(userId, page, 10, total); return PoAction.Ok(new { data, total = total.Value, msgCount }); } else { return PoAction.Ok(new { data = new List(), total = total.Value, msgCount }); } } /// /// 获取消息界面 /// /// /// [HttpGet] public async Task GetReadInfo(int no) { string userId = StateHelper.userId; UserModel user = new UserModel(); string talkId = string.Empty; if (no == 0) { user = await UserModelTool.GetUserView("0"); talkId = _messageService.GetTalkId(userId, "0"); } else { var userInfo = await _userService.GetUserInfoByUserNo(no.ToString()); if (userInfo == null) { return PoAction.Message("玩家不存在!"); } talkId = _messageService.GetTalkId(userId, userInfo.userId); user = await UserModelTool.GetUserView(userInfo.userId); } var noRead = await _messageService.GetUserNoReadData(talkId); //未读消息 if (noRead.Count > 0) { List msgIds = noRead.Select(it => it.msgId).ToList(); await _messageService.SetMsgRead(msgIds, userId); } RefAsync total = 0; var logs = await _messageService.GetUserMessageData(talkId, 1, 5, total); //消息记录 return PoAction.Ok(new { user, data = noRead, logs }); } /// /// 发送消息 /// /// /// [HttpPost] public async Task SendMsg([FromBody] SendMsgParms parms) { if (string.IsNullOrEmpty(parms.sign)) { return PoAction.Message("消息内容不能为空!"); } string userId = StateHelper.userId; int areaId = StateHelper.areaId; var msgRole = await _userService.GetUserConfigInfo(userId, nameof(UserEnum.ConfigCode.MsgRole)); if (msgRole != 1) { return PoAction.Message("消息权限已被封禁!"); } var toUser = await _userService.GetUserInfoByUserNo(parms.no.ToString()); if (toUser == null) { return PoAction.Message("玩家不存在!", 100); } if (toUser.areaId != areaId) { return PoAction.Message("暂不支持漫游消息!"); } parms.sign = StringAssist.NoHTML(parms.sign); if (await _messageService.SendMsg(userId, toUser.userId, parms.sign)) { return PoAction.Ok(true); } else { return PoAction.Message("发送失败,请稍后尝试"); } } /// /// 聊天记录 /// /// /// /// [HttpGet] public async Task MessageLog(int no, int page) { string userId = StateHelper.userId; UserModel user = new UserModel(); string talkId = string.Empty; if (no == 0) { user = await UserModelTool.GetUserView("0"); talkId = _messageService.GetTalkId(userId, "0"); } else { var userInfo = await _userService.GetUserInfoByUserNo(no.ToString()); if (userInfo == null) { return PoAction.Message("玩家不存在!"); } talkId = _messageService.GetTalkId(userId, userInfo.userId); user = await UserModelTool.GetUserView(userInfo.userId); } RefAsync total = 0; var logs = await _messageService.GetUserMessageData(talkId, page, 10, total); //消息记录 return PoAction.Ok(new { user, data = logs, total = total.Value }); } /// /// 删除对话 /// /// /// [HttpGet] public async Task DeleteMsg(int no) { string userId = StateHelper.userId; string talkId = string.Empty; if (no == 0) { talkId = _messageService.GetTalkId(userId, "0"); } else { var userInfo = await _userService.GetUserInfoByUserNo(no.ToString()); if (userInfo == null) { return PoAction.Message("玩家不存在!"); } talkId = _messageService.GetTalkId(userId, userInfo.userId); } if (await _messageService.DeleteMsgByTalkId(talkId, userId)) { return PoAction.Ok(true); } else { return PoAction.Message("删除失败,请稍后尝试!"); } } /// /// 获取邮件详情 /// /// /// [HttpGet] public async Task GetMailInfo(string no) { string userId = StateHelper.userId; var data = await _messageService.GetMailInfo(no); if (data == null) { return PoAction.Message("邮件不存在!"); } if (data.userId != userId) { return PoAction.Message("邮件不存在!"); } if (data.endTime < TimeExtend.GetTimeStampSeconds) { return PoAction.Message("邮件已过期!"); } if (data.isRead == 0) { data.isRead = 1; await _messageService.UpdateMaillInfo(data); } return PoAction.Ok(data); } /// /// 领取附件 /// /// /// [HttpGet] public async Task GetAward(string no) { string userId = StateHelper.userId; var data = await _messageService.GetMailInfo(no); if (data == null) { return PoAction.Message("邮件不存在!"); } if (data.userId != userId) { return PoAction.Message("邮件不存在!"); } if (data.endTime < TimeExtend.GetTimeStampSeconds) { return PoAction.Message("邮件已过期!"); } if (data.isGet == 1) { return PoAction.Message("邮件附件已领取!"); } data.isGet = 1; data.isRead = 1; if (await _messageService.UpdateMaillInfo(data)) { await GameBus.UpdateBag(userId, 1, data.award, $"邮件领取:{data.name}"); return PoAction.Ok(true); } else { return PoAction.Message("领取失败,请稍后尝试!"); } } /// /// 处理事件消息 /// /// /// /// [HttpGet] public async Task EventHandle(string eventId, int state) { string userId = StateHelper.userId; var eventInfo = await _messageService.GetEventMessageInfo(eventId); if (eventInfo == null) { return PoAction.Message("通知消息不存在!"); } if (eventInfo.userId != userId) { return PoAction.Message("通知消息不存在!"); } if (eventInfo.state != 0) { return PoAction.Message("消息已处理!"); } if (eventInfo.btns.All(it => it.status != state)) { return PoAction.Message("无法进行当前处理,请重试!"); } eventInfo.state = state; if (await _messageService.UpdateEventData(eventInfo)) { MessageHandle handle = new MessageHandle(); var result = await handle.HandleMsg(eventInfo, state); if (result.success) { return PoAction.Ok(true, result.msg); } else { eventInfo.state = 0; await _messageService.UpdateEventData(eventInfo); return PoAction.Message(result.msg); } } else { return PoAction.Message("处理失败,请稍后尝试!"); } } }