using Learun.Application.IM;
using Learun.Util;
using Nancy;
using System;
namespace Learun.Application.WebApi.Modules
{
///
/// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:数字化智慧校园-框架开发组
/// 日 期:2018.06.14
/// 描 述:即时通讯接口
///
public class IMApi : BaseApi
{
public IMApi()
: base("/learun/adms/im")
{
Get["/contacts"] = GetContactsList;
Post["/send"] = SendMsg;
Post["/addcontact"] = AddContact;
Post["/updete"] = UpdateContactState;
Get["/msg/lastlist"] = GetMsgList;
Get["/msg/list"] = GetMsgList1;
Get["/msg/list2"] = GetMsgList2;
}
private IMSysUserIBLL iMSysUserIBLL = new IMSysUserBLL();
private IMMsgIBLL iMMsgIBLL = new IMMsgBLL();
private IMContactsIBLL iMContactsIBLL = new IMContactsBLL();
///
/// 获取最近联系人列表
///
///
public Response GetContactsList(dynamic _)
{
var sysUserList = iMSysUserIBLL.GetList("");
string time = this.GetReqData();// 获取模板请求数据
DateTime beginTime = DateTime.Now;
var data = iMContactsIBLL.GetList(userInfo.userId,DateTime.Parse(time));
var jsondata = new
{
data = data,
sysUserList,
time = beginTime
};
return Success(jsondata);
}
///
/// 发送消息
///
///
///
public Response SendMsg(dynamic _)
{
MsgModel msyModel = this.GetReqData();// 获取模板请求数据
IMMsgEntity entity = new IMMsgEntity();
entity.F_SendUserId = userInfo.userId;
entity.F_RecvUserId = msyModel.userId;
entity.F_Content = msyModel.content;
iMMsgIBLL.SaveEntity(entity);
// 向即时消息服务器发送一条信息
SendHubs.callMethod("sendMsg2", userInfo.userId, msyModel.userId, msyModel.content, 0);
var jsonData = new
{
time = entity.F_CreateDate,
msgId = entity.F_MsgId
};
return Success(jsonData);
}
///
/// 添加一条最近的联系人
///
///
///
public Response AddContact(dynamic _)
{
string otherUserId = this.GetReqData();// 获取模板请求数据
IMContactsEntity entity = new IMContactsEntity();
entity.F_MyUserId = userInfo.userId;
entity.F_OtherUserId = otherUserId;
iMContactsIBLL.SaveEntity(entity);
return Success("添加成功!");
}
///
/// 更新消息读取状态
///
///
///
public Response UpdateContactState(dynamic _)
{
string otherUserId = this.GetReqData();// 获取模板请求数据
iMContactsIBLL.UpdateState(userInfo.userId, otherUserId);
return Success("更新成功!");
}
///
/// 获取最近10条聊天记录
///
///
///
public Response GetMsgList(dynamic _) {
string otherUserId = this.GetReqData();// 获取模板请求数据
var data = iMMsgIBLL.GetList(userInfo.userId, otherUserId);
return Success(data);
}
///
/// 获取小于某时间点的5条记录
///
///
///
public Response GetMsgList1(dynamic _)
{
MsgReqModel msgReqModel = this.GetReqData();// 获取模板请求数据
var data = iMMsgIBLL.GetListByTime(userInfo.userId, msgReqModel.otherUserId, msgReqModel.time);
return Success(data);
}
///
/// 获取大于某时间点的所有数据
///
///
///
public Response GetMsgList2(dynamic _)
{
MsgReqModel msgReqModel = this.GetReqData();// 获取模板请求数据
var data = iMMsgIBLL.GetListByTime2(userInfo.userId, msgReqModel.otherUserId, msgReqModel.time);
return Success(data);
}
}
public class MsgModel {
///
/// 发送给人员Id
///
public string userId { get; set; }
///
/// 消息内容
///
public string content { get; set; }
}
public class MsgReqModel
{
///
/// 发送给人员Id
///
public string otherUserId { get; set; }
///
/// 消息内容
///
public DateTime time { get; set; }
}
}