/* 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", getGroupStatistic: "/api/v1/chats/gm/statistic", getBadgeNumber: "/api/v1/application/badge_no" }; var actualServer; if (isProd === true) { actualServer = servers.prod; } else if (isDemo === true) { actualServer = servers.demo; } else { actualServer = servers.dev; } // REST接口实际路径 var endpoints = { login: actualServer + restPath.login, logout: actualServer + restPath.logout, updateStatus: actualServer + restPath.updateStatus, sendOneByOne: actualServer + restPath.sendPrivateMsg, getOneByOneMsg: actualServer + restPath.getPrivateMsg, sendGroup: actualServer + restPath.sendGroupMsg, getGroupMsg: actualServer + restPath.getGroupMsg, getGroupStatistic: actualServer + restPath.getGroupStatistic, getBadgeNumber: actualServer + restPath.getBadgeNumber }; // 本地临时缓存Key var plusStorageKey = { userId: "im_userid" }; var im = { // 登录 login: function (userId, token, client_id, platform) { console.log("IM - Login: " + userId); plus.storage.setItem(plusStorageKey.userId, userId); $.ajax({ type: "get", url: endpoints.login, data: {user_id: userId, token: token, client_id: client_id, platform: platform}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); }, error: function (msg) { } }); }, // 退出 logout: function (userId) { plus.storage.removeItem(plusStorageKey.userId); $.ajax({ type: "post", url: endpoints.logout, data: {user_id: userId}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); }, error: function (msg) { } }); }, // 更新用户在线状态 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) { $.ajax({ type: "get", url: endpoints.getGroupMsg, data: {user_id: uid, group_id: groupId, 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); } }); }, // 获取组消息数 getGroupInfo: function (uid, gid, handler) { $.ajax({ type: "get", url: endpoints.getGroupStatistic, data: {user_id: uid, group_id: gid}, async: true, dataType: "json", success: function (data) { handler(data); }, error: function (msg) { handler(msg); } }); }, // 发送私信 sendPrivateMsg: function (from_uid, to_uid, content, type) { function send() { $.ajax({ type: "post", url: endpoints.sendPrivateMsg, data: {from: from_uid, to: to_uid, content: content, contentType: type}, 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); } }); } 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)); if (handler) handler(data); }, error: function (msg) { console.log('error'); if (handler) handler(msg); } }); } }; /* 执行业务接口前,调用此函数判断当前用户是否在线。 */ function checkLogin(callback) { /*sendPost("/doctor/islive", {}, null, function (res) { if (res.status == 200) { callback(); } })*/ callback(); }