|
@ -1,263 +1,361 @@
|
|
|
/*
|
|
|
IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
|
|
|
/**
|
|
|
* IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
|
|
|
*/
|
|
|
var isProd = false; // 连接生产环境
|
|
|
var isDemo = false; // 连接公司演示或开发测试环境
|
|
|
|
|
|
// 服务器列表
|
|
|
var servers = {
|
|
|
dev: "http://192.168.131.115:3000",
|
|
|
demo: "http://ehr.yihu.com",
|
|
|
prod: "http://120.41.253.95:3000"
|
|
|
};
|
|
|
|
|
|
// REST接口相对路径
|
|
|
var restPath = {
|
|
|
login: "/api/v1/users/login",
|
|
|
logout: "/api/v1/users/logout",
|
|
|
status: "/api/v1/users/:user_id/status",
|
|
|
|
|
|
sendGroup: "/api/v1/chats/gm",
|
|
|
getGroupMsg: "/api/v1/chats/gm",
|
|
|
|
|
|
sendOneByOne: "/api/v1/chats/pm",
|
|
|
getOneByOneMsg: "/api/v1/chats/pm",
|
|
|
// 本地临时缓存Key
|
|
|
var LocalStorageKey = {
|
|
|
userId: "im_userid"
|
|
|
};
|
|
|
|
|
|
getGroupStatistic: "/api/v1/chats/gm/statistic",
|
|
|
// 服务器
|
|
|
var server = "http://127.0.0.1:3008";
|
|
|
|
|
|
getBadgeNumber: "/api/v1/application/badge_no"
|
|
|
};
|
|
|
// 资源实体在URL中的占位符
|
|
|
var UserPath = ":user_id";
|
|
|
var SessionPath = ":session_id";
|
|
|
var TopicPath = ":topic+id";
|
|
|
var ParticipantPath = ":participant_id";
|
|
|
|
|
|
var actualServer;
|
|
|
if (isProd === true) {
|
|
|
actualServer = servers.prod;
|
|
|
} else if (isDemo === true) {
|
|
|
actualServer = servers.demo;
|
|
|
} else {
|
|
|
actualServer = servers.dev;
|
|
|
}
|
|
|
// REST API
|
|
|
var ENDPOINTS = {
|
|
|
Application: {
|
|
|
BadgeNo: '/api/v2/application/badge_no'
|
|
|
},
|
|
|
Management: {
|
|
|
Health: '/api/v2/management/health',
|
|
|
DbStatus: '/db'
|
|
|
},
|
|
|
Users: {
|
|
|
Login: '/api/v2/users/login',
|
|
|
Logout: '/api/v2/users/logout',
|
|
|
|
|
|
// REST接口实际路径
|
|
|
var endpoints = {
|
|
|
login: actualServer + restPath.login,
|
|
|
logout: actualServer + restPath.logout,
|
|
|
updateStatus: actualServer + restPath.updateStatus,
|
|
|
User: '/api/v2/users/:user_id',
|
|
|
UserStatus: '/api/v2/users/:user_id/status'
|
|
|
},
|
|
|
Sessions: {
|
|
|
Session: '/api/v2/sessions/:session_id',
|
|
|
SessionSticky: '/api/v2/sessions/:session_id/sticky',
|
|
|
|
|
|
sendOneByOne: actualServer + restPath.sendPrivateMsg,
|
|
|
getOneByOneMsg: actualServer + restPath.getPrivateMsg,
|
|
|
RecentSessions: '/api/v2/sessions/recent',
|
|
|
|
|
|
sendGroup: actualServer + restPath.sendGroupMsg,
|
|
|
getGroupMsg: actualServer + restPath.getGroupMsg,
|
|
|
Topics: '/api/v2/sessions/:session_id/topics',
|
|
|
Topic: '/api/v2/sessions/:session_id/topics/:topic_id',
|
|
|
TopicEnded: '/api/v2/sessions/:session_id/topics/:topic_id/ended',
|
|
|
|
|
|
getGroupStatistic: actualServer + restPath.getGroupStatistic,
|
|
|
Messages: '/api/v2/sessions/:session_id/messages',
|
|
|
MessagesByTopic: '/api/v2/sessions/:session_id/topics/:topic_id/messages',
|
|
|
Message: '/api/v2/sessions/:session_id/messages/:message_id',
|
|
|
|
|
|
getBadgeNumber: actualServer + restPath.getBadgeNumber
|
|
|
};
|
|
|
SessionsUnreadMessageCount: '/api/v2/sessions/unread_message_count',
|
|
|
SessionUnreadMessageCount: '/api/v2/sessions/:session_id/unread_message_count',
|
|
|
SessionUnreadMessages: '/api/v2/sessions/:session_id/messages/unread',
|
|
|
|
|
|
// 本地临时缓存Key
|
|
|
var plusStorageKey = {
|
|
|
userId: "im_userid"
|
|
|
Participants: '/api/v2/sessions/:session_id/participants',
|
|
|
ParticipantsAvatar: '/api/v2/sessions/:session_id/participants/avatars',
|
|
|
Participant: '/api/v2/sessions/:session_id/participants/:participant_id',
|
|
|
ParticipantAvatar: '/api/v2/session/:session_id/participants/:participant_id/avatars'
|
|
|
}
|
|
|
};
|
|
|
|
|
|
var im = {
|
|
|
// 登录
|
|
|
login: function (userId, token, client_id, platform) {
|
|
|
console.log("IM - Login: " + userId);
|
|
|
plus.storage.setItem(plusStorageKey.userId, userId);
|
|
|
var httpClient = {
|
|
|
get: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.login,
|
|
|
data: {user_id: userId, token: token, client_id: client_id, platform: platform},
|
|
|
url: endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 退出
|
|
|
logout: function (userId) {
|
|
|
plus.storage.removeItem(plusStorageKey.userId);
|
|
|
post: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: endpoints.logout,
|
|
|
data: {user_id: userId},
|
|
|
url: endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 更新用户在线状态
|
|
|
updateStatus: function (userId, status) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.updateStatus.replace(':user_id', userId),
|
|
|
data: {user_id: userId, status: status},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 发送群组消息
|
|
|
sendGroupMsg: function (userId, groupId, content, type, handler, group_type) {
|
|
|
function send() {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: endpoints.sendGroupMsg,
|
|
|
data: {from: userId, group: groupId, groupType: group_type, contentType: type, content: content},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
handler(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
checkLogin(send);
|
|
|
},
|
|
|
// 获取组消息
|
|
|
getGroupMsg: function (uid, groupId, start, count, handler) {
|
|
|
put: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.getGroupMsg,
|
|
|
data: {user_id: uid, group_id: groupId, message_start_id: start, count: count},
|
|
|
type: "put",
|
|
|
url: endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
handler(data);
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 获取组消息数
|
|
|
getGroupInfo: function (uid, gid, handler) {
|
|
|
delete: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.getGroupStatistic,
|
|
|
data: {user_id: uid, group_id: gid},
|
|
|
type: "delete",
|
|
|
url: endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
handler(data);
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
};
|
|
|
|
|
|
var iChat = {
|
|
|
Application: {
|
|
|
// 获取应用角标数
|
|
|
getBadgeNo: function (userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Application.BadgeNo,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
}
|
|
|
},
|
|
|
Management: {
|
|
|
health: function (success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Management.Health,
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
}
|
|
|
},
|
|
|
// 发送私信
|
|
|
sendPrivateMsg: function (from_uid, to_uid, content, type) {
|
|
|
function send() {
|
|
|
Users: {
|
|
|
login: function (userId, token, client_id, platform, success, failure) {
|
|
|
plus.storage.setItem(LocalStorageKey.userId, userId);
|
|
|
|
|
|
httpClient.post(ENDPOINTS.Login,
|
|
|
{user_id: userId, token: token, client_id: client_id, platform: platform},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
logout: function (userId, success, failure) {
|
|
|
plus.storage.removeItem(LocalStorageKey.userId);
|
|
|
|
|
|
httpClient.delete(ENDPOINTS.Logout,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
updateStatus: function (userId, status, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.UserStatus,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: endpoints.sendPrivateMsg,
|
|
|
data: {from: from_uid, to: to_uid, content: content, contentType: type},
|
|
|
type: "get",
|
|
|
url: ENDPOINTS.UserStatus.replace(':user_id', userId),
|
|
|
data: {user_id: userId, status: status},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
handler(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
checkLogin(send);
|
|
|
},
|
|
|
},
|
|
|
// 发送私信
|
|
|
sendPrivateMsgWithCallback: function (from_uid, to_uid, content, type, msgCallback, errCallback) {
|
|
|
function send() {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: endpoints.sendPrivateMsg,
|
|
|
data: {from: from_uid, to: to_uid, content: contentType, type: type},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (msg) {
|
|
|
if (msgCallback) msgCallback(msg);
|
|
|
},
|
|
|
error: function (err) {
|
|
|
if (errCallback) errCallback(err);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
Sessions: {
|
|
|
// 创建MUC会话
|
|
|
createMucSession: function (userId, peerId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions,
|
|
|
{session_type: 1, user_id: userId, peer_id: peerId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
checkLogin(send);
|
|
|
},
|
|
|
// 获取私信
|
|
|
getPrivateMsg: function (uid, peer_uid, start, count) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.getPrivateMsg,
|
|
|
data: {user_id: uid, peer_id: peer_uid, message_start_id: start, count: count},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
handler(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 获取私信
|
|
|
getPrivateMsgWithCallback: function (uid, peer_uid, start, count, handler) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.getPrivateMsg,
|
|
|
data: {user_id: uid, peer_id: peer_uid, message_start_id: start, count: count},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
handler(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
handler(msg);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 获取应用角标数
|
|
|
getBadgeNumber: function (userId, handler) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoints.getBadgeNumber,
|
|
|
data: {user_id: userId},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
// 创建P2P会话
|
|
|
createP2pSession: function (userId, peerId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions,
|
|
|
{session_type: 2, user_id: userId, peer_id: peerId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
if (handler) handler(data);
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
console.log('error');
|
|
|
// 获取与患者发生的会话,实际上这些是MUC会话
|
|
|
getSessionsWithPatient: function (userId, page, size, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions,
|
|
|
{user_id: userId, session_type: "1", page: page, size: size},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
if (handler) handler(msg);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
// 获取与医生相关的会话,实际上这些是P2P,群聊和讨论组
|
|
|
getSessionsWithDoctor: function (userId, page, size, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions,
|
|
|
{user_id: userId, session_type: "2, 3", page: page, size: size},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取与患者的最近会话
|
|
|
getRecentSessionsWithPatient: function (userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.RecentSessions,
|
|
|
{session_type: 1, user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取与医生的最近会话,包括P2P、群聊与讨论组
|
|
|
getRecentSessionsWithDoctor: function (userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.RecentSessions,
|
|
|
{session_type: "2, 3", userId: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
|
|
|
// 置顶会话
|
|
|
stickSession: function (userId, sessionId, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.Sessions.SessionSticky.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 取消会话置顶
|
|
|
unstickSession: function (userId, sessionId, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.Sessions.SessionSticky.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取所有咨询
|
|
|
getTopics: function (sessionId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 创建咨询
|
|
|
createTopic: function (sessionId, userId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 结束咨询
|
|
|
endTopic: function (sessionId, userId, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.Sessions.Topics.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 咨询是否已结束
|
|
|
isTopicEnded: function (sessionId, topicId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.TopicEnded.replace(SessionPath, sessionId).replace(TopicPath, topicId),
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 发送消息,不论是何种会话,均不需要指定会话对象是谁,只需要向会话本身发送消息即可
|
|
|
sendMessage: function (sessionId, userId, content, contentType, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId),
|
|
|
{from: userId, content_type: contentType, content: content},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 按会话获取消息
|
|
|
getMessagesBySession: function (sessionId, startMessageId, count, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId),
|
|
|
{start_message_id: startMessageId, count: count},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 按议题获取消息
|
|
|
getMessagesByTopic: function (sessionId, topicId, startMessageId, count, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.MessagesByTopic.replace(SessionPath, sessionId),
|
|
|
{start_message_id: startMessageId, count: count},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取所有会话的未读消息数
|
|
|
getAllSessionUnreadMessageCount: function (userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.SessionsUnreadMessageCount,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取指定会话的未读消息数
|
|
|
getSessionUnreadMessageCount: function (sessionId, userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.SessionUnreadMessageCount.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取指定会话的未读消息
|
|
|
getSessionUnreadMessages: function (sessionId, userId, count, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.SessionUnreadMessages.replace(SessionPath, sessionId),
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 添加会话成员
|
|
|
addParticipant: function (sessionId, participantId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Participants.replace(SessionPath, sessionId),
|
|
|
{participant_id: participantId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取会话成员列表
|
|
|
getParticipants: function (sessionId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.Participants.replace(SessionPath, sessionId),
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 移除会话成员
|
|
|
removeParticipant: function (sessionId, participantId, success, failure) {
|
|
|
httpClient.delete(ENDPOINTS.Sessions.Participant.replace(SessionPath, sessionId).replace(ParticipantPath, participantId),
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取会话成员头像
|
|
|
getParticipantAvatar: function (sessionId, participantId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.ParticipantAvatar.replace(SessionPath, sessionId).replace(ParticipantPath, participantId),
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
}
|
|
|
},
|
|
|
Search: {}
|
|
|
};
|
|
|
|
|
|
/*
|