// 服务器 var publish_version = true; /** * IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。 */ // Node.js模拟jQuery及ajax请求所需要的环境:document及XMLHttpRequest。 // 这些环境仅用于模拟,客户端在使用时候不需要真正引入 if (typeof process !== 'undefined') { module.exports = imClient; var jsdom = require('jsdom').jsdom; var document = jsdom('', {}); 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 imServer = "", socketUrl = ""; if(publish_version){ imServer = "http://27.154.233.186:3030/api/v2"; socketUrl = "http://27.154.233.186:3030"; }else{ // imServer = "http://172.19.103.72:3000/api/v2"; // socketUrl = "http://172.19.103.72:3000"; socketUrl = "http://192.168.131.24:3000"; imServer = "http://192.168.131.24:3000/api/v2"; } var imTimeOut = 20000; // 资源实体在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', getSessionsInfo: '/sessions/:session_id/session', 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: { Search: "/search" }, Consult :{ getHealthConsult: '/sessions/topics', updateConsultStatus: '/sessions/:session_id/topics' } }; var httpClient = { get: function (endpoint, data, successCallback, errorCallback) { $.ajax({ type: "get", url: imServer + endpoint, data: data, async: true, dataType: "json", timeout: imTimeOut, success: function (data) { successCallback?successCallback(data):""; }, error: function (xhr, status, error) { errorCallback ? errorCallback(xhr, status, error) : ""; } }); }, post: function (endpoint, data, successCallback, errorCallback) { $.ajax({ type: "post", url: imServer + endpoint, data: data, async: true, dataType: "json", timeout: imTimeOut, success: function (data) { successCallback?successCallback(data):""; }, error: function (xhr, status, error) { errorCallback?errorCallback(xhr, status, error):""; } }); }, put: function (endpoint, data, successCallback, errorCallback) { $.ajax({ type: "post", url: imServer + endpoint, data: data, async: true, dataType: "json", timeout: imTimeOut, headers: { "X-HTTP-Method-Override": "PUT" }, success: function (data) { successCallback? successCallback(data) : ""; }, error: function (xhr, status, error) { errorCallback? errorCallback(xhr, status, error): ""; } }); }, delete: function (endpoint, data, successCallback, errorCallback) { $.ajax({ type: "get", url: imServer + endpoint, data: data, async: true, dataType: "json", timeout: imTimeOut, headers: { "X-HTTP-Method-Override": "DELETE" }, success: function (data) { successCallback?successCallback(data): ""; }, error: function (xhr, status, error) { errorCallback? 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, sessionName, success, failure) { var p = {}; p[userId] = 0; p[peerId] = 0; httpClient.post(ENDPOINTS.Sessions.Sessions, {session_id: userId, session_type: 1, session_name: sessionName, participants: JSON.stringify(p)}, success, failure); }, // 创建P2P会话 createP2pSession: function (userId, peerId, sessionName, success, failure) { var p = {}; p[userId] = 0; p[peerId] = 0; httpClient.post(ENDPOINTS.Sessions.Sessions, {session_type: 2, session_name: sessionName, participants: JSON.stringify(p) }, success, failure); }, createGroupSession: function (teamId, sessionName, participants, success, failure) { var p = {}; for(var k in participants){ p[k] = 0; } httpClient.post(ENDPOINTS.Sessions.Sessions, {session_id: teamId, session_type: 3, session_name: sessionName, participants: JSON.stringify(p)}, success, failure); }, // 获取与患者发生的会话,实际上这些是MUC会话 getSessionsWithPatient: function (userId, page, size, status , success, failure) { httpClient.get(ENDPOINTS.Sessions.Sessions, {user_id: userId, business_type: "2", status: status, page: page, size: size}, success, failure); }, // 获取与医生相关的会话,实际上这些是P2P,群聊和讨论组 getSessionsWithDoctor: function (userId, page, size, success, failure) { httpClient.get(ENDPOINTS.Sessions.Sessions, {user_id: userId, business_type: "1", page: page, size: size}, success, failure); }, // 获取最近7天内的会话列表 getRecentSessions: function (userId, success, failure) { httpClient.get(ENDPOINTS.Sessions.RecentSessions, {user_id: userId, date_span: 7}, 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), {session_id: sessionId, topic_id: topicId}, success, failure); }, // 发送消息,不论是何种会话,均不需要指定会话对象是谁,只需要向会话本身发送消息即可 sendMessage: function (sessionId, userId, userName, content, contentType, success, failure) { var userAgent = plus.storage.getItem("userAgent"); var view = 0; //记录观察者模式 if(JSON.parse(userAgent).observer == "1"){ view = 1; } httpClient.post(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId), {sender_id: userId, sender_name: userName, content_type: contentType, content: content, view: view}, success, failure); }, // 按会话获取消息 getMessagesBySession: function (sessionId, userId, page, pagesize, startMsgId, endMsgId, success, failure, content_type, isoffset) { httpClient.get(ENDPOINTS.Sessions.Messages.replace(SessionPath, sessionId), { page: page || 1, pagesize: pagesize || 20, end_msg_id: endMsgId || "", start_msg_id: startMsgId || "", user: userId, session_id :sessionId, content_type: content_type || "", isoffset: isoffset || "" }, 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, success, failure) { httpClient.get(ENDPOINTS.Sessions.SessionUnreadMessages.replace(SessionPath, sessionId), {user_id: userId}, success, failure); }, // 添加会话成员 addParticipant: function (sessionId, participantId, success, failure) { httpClient.put(ENDPOINTS.Sessions.Participant.replace(SessionPath, sessionId).replace(ParticipantPath, participantId), {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); }, // 获取会话成员头像 getParticipantsAvatars: function (sessionId, success, failure) { httpClient.get(ENDPOINTS.Sessions.ParticipantsAvatar.replace(SessionPath, sessionId), {}, success, failure); }, //根据session_id获取会话信息 getSessionsInfo:function(sessionId,userId,success, failure){ httpClient.get(ENDPOINTS.Sessions.getSessionsInfo.replace(SessionPath, sessionId), {user_id:userId}, success, failure); } }, Search: { Doctor: { // 搜索所有对象:医生,会话及消息 searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'doctor', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions}, success, failure); }, // 搜索医生 searchDoctors: function (userId, keyword,excludeTopicEndedSessions, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'doctor', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size}, success, failure); }, // 搜索会话 searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'doctor', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size}, success, failure); }, // 搜索会话消息 searchMessages: function (userId, keyword, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'doctor', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size}, success, failure); } }, Patient: { // 搜索所有对象:患者,会话及消息 searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'patient', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions}, success, failure); }, // 搜索患者 searchPatient: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'patient', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size}, success, failure); }, // 搜索会话 searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'patient', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size}, success, failure); }, // 搜索会话消息 searchMessages: function (userId, keyword, page, size, success, failure) { httpClient.get(ENDPOINTS.Search.Search, {user_id: userId, target_role: 'patient', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size}, success, failure); } } }, Consult:{ //获取健康咨询列表 getHealthConsult : function(user, status, page, pagesize, success, failure){ httpClient.get(ENDPOINTS.Consult.getHealthConsult, {user: user,status:status,page:page,pagesize:pagesize}, success, failure); }, //更新咨询状态 updateConsultStatus : function(sessionId, id, status, success, failure){ httpClient.put(ENDPOINTS.Consult.updateConsultStatus.replace(SessionPath, sessionId), {topic_id:id,data:JSON.stringify({status:status})}, success, failure); } } };