Files
Kg.SeaTime/Service/Application.Web/Controllers/Chat/ChatController.cs
Putoo dbace8a8b2 222
2026-05-23 18:36:37 +08:00

137 lines
4.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Chat;
/// <summary>
/// 公聊接口
/// </summary>
[Route("Chat/[controller]/[action]")]
[ApiController]
[Authorize]
public class ChatController : ControllerBase
{
private readonly IGameChatService _chatService;
private readonly IGameGoodsService _goodsService;
public ChatController(IGameChatService chatService,IGameGoodsService goodsService)
{
_chatService = chatService;
_goodsService = goodsService;
}
/// <summary>
/// 获取公聊信息
/// </summary>
/// <param name="type"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetChatData(int type, int page)
{
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
string teamId = "";
string groupId = "";
RefAsync<int> Total = 0;
var data = await _chatService.GetChatData(type, areaId, teamId, groupId, page, 10, Total);
//物品数量
int sendGoodsCount = 0;
string sendGoodsName = "";
switch (type)
{
case 0:
sendGoodsCount = await _goodsService.GetUserGoodsCount(userId,GameConfig.SendChatGoodsBase);
sendGoodsName = "小海螺";
break;
case 3:
sendGoodsCount = await _goodsService.GetUserGoodsCount(userId,GameConfig.SendChatGoodsArea);
sendGoodsName = "大海螺";
break;
case 4:
sendGoodsCount = await _goodsService.GetUserGoodsCount(userId,GameConfig.SendChatGoodsService);
sendGoodsName = "金海螺";
break;
}
return PoAction.Ok(new { data, total = Total.Value ,sendGoodsCount,sendGoodsName});
}
/// <summary>
/// 发言
/// </summary>
/// <param name="pars"></param>
/// <returns></returns>
[HttpPost]
public async Task<IPoAction> SendChat([FromBody] SendChatParms pars)
{
if (string.IsNullOrEmpty(pars.sign))
{
return PoAction.Message("发言内容不能为空!");
}
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
string par = string.Empty;
string code = GameChatEnum.Code.Public.ToString();
int goodsId = 0;
bool isSend = false;
switch (pars.type)
{
case 0:
isSend = true;
code =nameof(GameChatEnum.Code.Public);
goodsId = GameConfig.SendChatGoodsBase;
break;
case 1:
isSend = true;
code = nameof(GameChatEnum.Code.Team);
par = "";
break;
case 2:
isSend = true;
code = nameof(GameChatEnum.Code.Group);
par="";
break;
case 3:
isSend = true;
goodsId = GameConfig.SendChatGoodsArea;
code = nameof(GameChatEnum.Code.Region);
break;
case 4:
isSend = true;
goodsId = GameConfig.SendChatGoodsService;
code = nameof(GameChatEnum.Code.Dress);
break;
}
if (isSend == false)
{
return PoAction.Message("无法发言!");
}
if (goodsId != 0)
{
var myCount = await _goodsService.GetUserGoodsCount(userId, goodsId);
if (myCount < 1)
{
return PoAction.Message("暂无发言道具,去商城购买海螺才可以发言哦!");
}
}
string sign = StringAssist.NoHTML(pars.sign);
bool result = await _chatService.SendChat(userId, areaId, code, sign, par);
if (result)
{
if (goodsId != 0)//扣除道具
{
await _goodsService.UpdateUserGoods(userId, 0, goodsId, 1, "发言");
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("发送失败,请稍后尝试!");
}
}
}