Files
Kg.SeaTime/Service/Application.Web/Controllers/User/RelationController.cs
Putoo f8d4a28d53 121
2026-05-30 17:32:27 +08:00

244 lines
8.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.User;
/// <summary>
/// 关系接口
/// </summary>
[ApiExplorerSettings(GroupName = "User")]
[Route("User/[controller]/[action]")]
[ApiController]
[Authorize]
public class RelationController : ControllerBase
{
private readonly IUnitUserRelationService _relationService;
private readonly IUnitUserService _userService;
private readonly IMessageService _messageService;
private readonly IUnitUserAccService _accService;
public RelationController(IUnitUserRelationService relationService, IUnitUserService userService,
IMessageService messageService, IUnitUserAccService accService)
{
_relationService = relationService;
_userService = userService;
_messageService = messageService;
_accService = accService;
}
/// <summary>
/// 关系列表数据
/// </summary>
/// <param name="type"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetRelation(int type, int page)
{
string userId = StateHelper.userId;
RefAsync<int> total = 0;
List<RelationView> result = new List<RelationView>();
if (type == 0)
{
var data = await _relationService.GetUserFriend(userId);
total = data.Count;
data = data.OrderByDescending(it => it.isOnline).ThenByDescending(it => it.sort).ToList();
result = PageExtend.GetPageList(data, page, 10);
}
else if (type == 1)
{
var data = await _relationService.GetUserFriend(userId);
data = data.FindAll(it => it.near >= 1000);
total = data.Count;
data = data.OrderByDescending(it => it.isOnline).ThenByDescending(it => it.near)
.ThenByDescending(it => it.sort).ToList();
result = PageExtend.GetPageList(data, page, 10);
}
else if (type == 3)
{
var data = await _relationService.GetUserEnemyData(userId);
total = data.Count;
data = data.OrderByDescending(it => it.isOnline)
.ThenByDescending(it => it.sort).ToList();
result = PageExtend.GetPageList(data, page, 10);
}
return PoAction.Ok(new { data = result, total = total.Value, online = result.Count(it => it.isOnline == 1) });
}
/// <summary>
/// 好友处理
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> FriendHandle(string no)
{
string userId = StateHelper.userId;
var userInfo = await _userService.GetUserInfoByUserNo(no); //基础资料
if (userInfo == null)
{
return PoAction.Message("玩家不存在!");
}
if (userInfo.userId == userId)
{
return PoAction.Message("不能加自己为好友!");
}
if (userInfo.areaId != StateHelper.areaId)
{
return PoAction.Message("玩家不存在!");
}
bool IsFriend = await _relationService.CheckIsFriend(userId, userInfo.userId);
if (IsFriend == false) //添加好友
{
var friendRole =
await _userService.GetUserConfigInfo(userInfo.userId, nameof(UserEnum.ConfigCode.FriendRole));
if (friendRole == 0)
{
return PoAction.Message("该玩家拒绝任何人添加好友!");
}
var friendCount = await _relationService.GetUserFriendCount(userId);
if (friendCount >= GameConfig.GameUserFriendMaxCount)
{
return PoAction.Message("好友人数已达上限!");
}
if (friendRole == 2)//不限制加好友
{
if (await _relationService.AddFriend(userId, userInfo.userId))
{
return PoAction.Ok(true, "添加好友成功!");
}
else
{
return PoAction.Ok(true, "添加好友失败,请稍后尝试!");
}
}
string token = $"{nameof(MessageEnum.EventCode.Friend)}_{userId}_{userInfo.userId}";
if (await _messageService.CheckAtEventMsg(token))
{
return PoAction.Message("已发送过交友消息,等待对方通过!");
}
var myInfo = await _userService.GetUserInfoByUserId(userId);
string sign = $"{UbbTool.HomeUbb(myInfo.userNo, myInfo.nick)} 希望添加您为好友?";
List<MsgEventBtn> btns = new List<MsgEventBtn>()
{
new MsgEventBtn() { status = 1, name = "通过", tips = "已通过申请" },
new MsgEventBtn() { status = 2, name = "拒绝", tips = "已拒绝申请" }
};
if (await _messageService.SendEventMsg(userInfo.userId, nameof(MessageEnum.EventCode.Friend), "交友消息通知",
sign, userId, token, btns, 3))
{
return PoAction.Ok(true, "好友申请已发送,请等待对方确认!");
}
else
{
return PoAction.Message("添加失败,请稍后尝试!");
}
}
else //删除好友
{
bool result = await _relationService.DeleteFriend(userId, userInfo.userId);
if (result)
{
return PoAction.Ok(true, "删除好友成功!");
}
else
{
return PoAction.Message("删除失败,请稍后尝试.");
}
}
}
/// <summary>
/// 删除仇人
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> DeleteEnemy(string no)
{
string userId = StateHelper.userId;
var userInfo = await _userService.GetUserInfoByUserNo(no); //基础资料
if (userInfo == null)
{
return PoAction.Message("玩家不存在!");
}
bool IsEnemy = await _relationService.CheckUserEnemy(userId, userInfo.userId);
if (IsEnemy == false)
{
return PoAction.Message("不是仇人,无需删除哦");
}
bool result = await _relationService.DeleteUserEnemy(userId, userInfo.userId);
if (result)
{
return PoAction.Ok(true, "删除成功!");
}
else
{
return PoAction.Message("删除失败,请稍后尝试.");
}
}
/// <summary>
/// 添加仇人
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> AddEnemy(string no)
{
string userId = StateHelper.userId;
var userInfo = await _userService.GetUserInfoByUserNo(no); //基础资料
if (userInfo == null)
{
return PoAction.Message("玩家不存在!");
}
if (userInfo.userId == userId)
{
return PoAction.Message("不能加自己为仇人!");
}
if (userInfo.areaId != StateHelper.areaId)
{
return PoAction.Message("玩家不存在!");
}
bool IsEnemy = await _relationService.CheckUserEnemy(userId, userInfo.userId);
if (IsEnemy == true)
{
return PoAction.Message("已经是仇人啦,无需重复添加!");
}
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.gold));
if (myAcc < GameConfig.GameRelationEnemyNeedGold)
{
return PoAction.Message("您的金元不足!");
}
if (await _accService.UpdateUserAcc(userId, 0, nameof(AccEnum.AccType.gold),
GameConfig.GameRelationEnemyNeedGold, nameof(AccEnum.Name.), "添加仇人"))
{
if (await _relationService.AddUserEnemy(userId, userInfo.userId))
{
return PoAction.Ok(true, "仇人添加成功!");
}
else
{
return PoAction.Message("添加失败,请稍后尝试.");
}
}
else
{
return PoAction.Message("添加失败,请稍后尝试.");
}
}
}