|
@ -0,0 +1,386 @@
|
|
|
/**
|
|
|
* IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
|
|
|
*/
|
|
|
|
|
|
// Node.js模拟jQuery及ajax请求所需要的环境:document及XMLHttpRequest。
|
|
|
// 这些环境仅用于模拟,客户端在使用时候不需要真正引入
|
|
|
if (typeof process !== 'undefined') {
|
|
|
var jsdom = require('jsdom').jsdom;
|
|
|
var document = jsdom('<html></html>', {});
|
|
|
var window = document.defaultView;
|
|
|
var jQuery = require('jquery');
|
|
|
var $ = jQuery(window);
|
|
|
|
|
|
$.support.cors = true;
|
|
|
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
|
|
$.ajaxSettings.xhr = function () {
|
|
|
return new XMLHttpRequest();
|
|
|
};
|
|
|
}
|
|
|
|
|
|
// 本地临时缓存Key
|
|
|
var LocalStorageKey = {
|
|
|
userId: "im_userid"
|
|
|
};
|
|
|
|
|
|
// 服务器
|
|
|
var server = "http://127.0.0.1:3008/api/v2";
|
|
|
|
|
|
// 资源实体在URL中的占位符
|
|
|
var UserPath = ":user_id";
|
|
|
var SessionPath = ":session_id";
|
|
|
var TopicPath = ":topic_id";
|
|
|
var ParticipantPath = ":participant_id";
|
|
|
|
|
|
// REST API
|
|
|
var ENDPOINTS = {
|
|
|
Application: {
|
|
|
BadgeNo: '/application/badge_no'
|
|
|
},
|
|
|
Management: {
|
|
|
Health: '/management/health',
|
|
|
DbStatus: '/management/db'
|
|
|
},
|
|
|
Users: {
|
|
|
Login: '/users/login',
|
|
|
Logout: '/users/logout',
|
|
|
|
|
|
User: '/users/:user_id',
|
|
|
UserStatus: '/users/:user_id/status'
|
|
|
},
|
|
|
Sessions: {
|
|
|
Sessions: '/sessions',
|
|
|
|
|
|
Session: '/sessions/:session_id',
|
|
|
SessionSticky: '/sessions/:session_id/sticky',
|
|
|
|
|
|
RecentSessions: '/sessions/recent',
|
|
|
|
|
|
Topics: '/sessions/:session_id/topics',
|
|
|
Topic: '/sessions/:session_id/topics/:topic_id',
|
|
|
TopicEnded: '/sessions/:session_id/topics/:topic_id/ended',
|
|
|
|
|
|
Messages: '/sessions/:session_id/messages',
|
|
|
MessagesByTopic: '/sessions/:session_id/topics/:topic_id/messages',
|
|
|
Message: '/sessions/:session_id/messages/:message_id',
|
|
|
|
|
|
SessionsUnreadMessageCount: '/sessions/unread_message_count',
|
|
|
SessionUnreadMessageCount: '/sessions/:session_id/unread_message_count',
|
|
|
SessionUnreadMessages: '/sessions/:session_id/messages/unread',
|
|
|
|
|
|
Participants: '/sessions/:session_id/participants',
|
|
|
ParticipantsAvatar: '/sessions/:session_id/participants/avatars',
|
|
|
Participant: '/sessions/:session_id/participants/:participant_id',
|
|
|
ParticipantAvatar: '/session/:session_id/participants/:participant_id/avatars'
|
|
|
},
|
|
|
Search: {}
|
|
|
};
|
|
|
|
|
|
var httpClient = {
|
|
|
get: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
post: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
put: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
headers: {
|
|
|
"X-HTTP-Method-Override": "PUT"
|
|
|
},
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
delete: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
headers: {
|
|
|
"X-HTTP-Method-Override": "DELETE"
|
|
|
},
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
// 执行业务接口前,调用此函数判断当前用户是否在线。
|
|
|
isOnline: function (callback, failure) {
|
|
|
httpClient.get(ENDPOINTS.Users.UserStatus, {}, null, function (res) {
|
|
|
if (res.status == 200) {
|
|
|
callback();
|
|
|
} else {
|
|
|
failure();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
};
|
|
|
|
|
|
var imClient = {
|
|
|
Application: {
|
|
|
// 获取应用角标数
|
|
|
getBadgeNo: function (userId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Application.BadgeNo,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
}
|
|
|
},
|
|
|
Management: {
|
|
|
getDbStatus: function (success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Management.DbStatus,
|
|
|
{},
|
|
|
success,
|
|
|
failure);
|
|
|
}
|
|
|
},
|
|
|
Users: {
|
|
|
// 登录
|
|
|
login: function (userId, token, client_id, platform, success, failure) {
|
|
|
if (typeof plus !== 'undefined') plus.storage.setItem(LocalStorageKey.userId, userId);
|
|
|
|
|
|
httpClient.post(ENDPOINTS.Users.Login,
|
|
|
{user_id: userId, token: token, client_id: client_id, platform: platform},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 退出
|
|
|
logout: function (userId, success, failure) {
|
|
|
if (typeof plus !== 'undefined') plus.storage.removeItem(LocalStorageKey.userId);
|
|
|
|
|
|
httpClient.delete(ENDPOINTS.Users.Logout,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 更新用户状态
|
|
|
updateStatus: function (userId, appInBg, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.Users.UserStatus.replace(UserPath, userId),
|
|
|
{app_in_bg: appInBg},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
},
|
|
|
Sessions: {
|
|
|
// 创建MUC会话
|
|
|
createMucSession: function (userId, peerId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Sessions,
|
|
|
{session_type: 1, session_name: "咨询", participants: []},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 创建P2P会话
|
|
|
createP2pSession: function (userId, peerId, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Sessions,
|
|
|
{session_type: 2, session_name: "P2P", participants: [userId + ":0", peerId + ":0"]},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取与患者发生的会话,实际上这些是MUC会话
|
|
|
getSessionsWithPatient: function (userId, page, size, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.Sessions,
|
|
|
{user_id: userId, session_type: "1", page: page, size: size},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取与医生相关的会话,实际上这些是P2P,群聊和讨论组
|
|
|
getSessionsWithDoctor: function (userId, page, size, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.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, userName, content, contentType, success, failure) {
|
|
|
httpClient.post(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId),
|
|
|
{sender_id: userId, sender_name: userName, 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.SessionParticipants.replace(SessionPath, sessionId),
|
|
|
{participant_id: participantId},
|
|
|
success,
|
|
|
failure);
|
|
|
},
|
|
|
|
|
|
// 获取会话成员列表
|
|
|
getParticipants: function (sessionId, success, failure) {
|
|
|
httpClient.get(ENDPOINTS.Sessions.SessionParticipants.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: {}
|
|
|
};
|
|
|
|
|
|
module.exports = imClient;
|