using learun.iapplication; using learun.util; using Microsoft.AspNetCore.Mvc; using System; using System.Threading.Tasks; namespace learun.webapi.Controllers { /// /// 版 本 Learun-ADMS-Core 力软管理后台 /// Copyright (c) 2019-present 力软信息技术(苏州)有限公司 /// 创建人:tobin /// 日 期:2020.04.22 /// 描 述:即时通讯 /// public class IMController : MvcControllerBase { private readonly IMSysUserIBLL _iMSysUserIBLL; private readonly IMMsgIBLL _iMMsgIBLL; private readonly IMContactsIBLL _iMContactsIBLL; /// /// 初始化方法 /// /// 即时通讯系统用户 /// 消息方法 /// 联系人方法 public IMController(IMSysUserIBLL iMSysUserIBLL, IMMsgIBLL iMMsgIBLL, IMContactsIBLL iMContactsIBLL) { _iMSysUserIBLL = iMSysUserIBLL; _iMMsgIBLL = iMMsgIBLL; _iMContactsIBLL = iMContactsIBLL; } /// /// 获取最近联系人列表 /// /// 开始时间 /// [HttpGet] public async Task contacts(string time) { var sysUserList = await _iMSysUserIBLL.GetList(""); DateTime beginTime = DateTime.Now; var data = await _iMContactsIBLL.GetList(this.GetUserId(), DateTime.Parse(time)); var jsondata = new { data, sysUserList, time = beginTime }; return Success(jsondata); } /// /// 发送消息 /// /// 接收人主键 /// 内容 /// [HttpPost] public async Task Send([FromForm]string userId, [FromForm]string content) { IMMsgEntity entity = new IMMsgEntity(); entity.F_SendUserId = this.GetUserId(); entity.F_RecvUserId = userId; entity.F_Content = content; await _iMMsgIBLL.SaveEntity(entity); // 向即时消息服务器发送一条信息 await SendHubs.callMethod("sendMsg2", this.GetUserId(), userId, content, 0); var jsonData = new { time = entity.F_CreateDate, msgId = entity.F_MsgId }; return Success(jsonData); } /// /// 添加一条最近的联系人 /// /// 联系人 /// [HttpPost] public async Task AddContact([FromForm]string otherUserId) { IMContactsEntity entity = new IMContactsEntity(); entity.F_MyUserId = this.GetUserId(); entity.F_OtherUserId = otherUserId; await _iMContactsIBLL.SaveEntity(entity); return SuccessInfo("添加成功!"); } /// /// 更新消息读取状态 /// /// 消息发送者ID /// [HttpPost] public async Task Update([FromForm]string otherUserId) { await _iMContactsIBLL.UpdateState(this.GetUserId(), otherUserId); return SuccessInfo("更新成功!"); } /// /// 获取最近10条聊天记录 /// /// 消息发送者ID /// [HttpGet] public async Task LastMsg(string otherUserId) { var data = await _iMMsgIBLL.GetList(this.GetUserId(), otherUserId); return Success(data); } /// /// 获取小于某时间点的5条记录 /// /// 消息交谈方Id /// 时间 /// [HttpGet] public async Task MsgList(string otherUserId, DateTime time) { var data = await _iMMsgIBLL.GetListByTime(this.GetUserId(), otherUserId, time); return Success(data); } /// /// 获取大于某时间点的所有数据 /// /// 消息交谈方Id /// 时间 /// [HttpGet] public async Task MsgList2(string otherUserId, DateTime time) { var data = await _iMMsgIBLL.GetListByTime2(this.GetUserId(), otherUserId, time); return Success(data); } } }