using Microsoft.AspNetCore.Mvc; namespace Application.Web.Controllers.User; /// /// 关系接口 /// [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; public RelationController(IUnitUserRelationService relationService, IUnitUserService userService, IMessageService messageService) { _relationService = relationService; _userService = userService; _messageService = messageService; } /// /// 关系列表数据 /// /// /// /// [HttpGet] public async Task GetRelation(int type, int page) { string userId = StateHelper.userId; RefAsync total = 0; List result = new List(); 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); } return PoAction.Ok(new { data = result, total = total.Value, online = result.Count(it => it.isOnline == 1) }); } [HttpGet] public async Task 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) { 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 btns = new List() { 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("删除失败,请稍后尝试."); } } } }