/* IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。 */ var isProd = true; // 连接生产环境 var isDemo = false; // 连接公司演示或开发测试环境 // 服务器列表 var servers = { dev: "http://192.168.131.109:3008", prod: "http://27.154.233.186:3030", demo: "http://ehr.yihu.com", //prod: "http://120.41.253.95:3000" // prod:"http://192.168.131.24:3000" }; // REST接口相对路径 var restPath = { login: "/api/v1/users/login", logout: "/api/v1/users/logout", status: "/api/v1/users/:user_id/status", sendGroupMsg: "/api/v1/chats/gm", getGroupMsg: "/api/v1/chats/gm", getGroupUnreadMsg: "/api/v1/chats/gm/unread", getGroupImgMsg: "/api/v1/chats/gm/images", sendOneByOne: "/api/v1/chats/pm", getOneByOneMsg: "/api/v1/chats/pm", getOneByOneUnreadMsg: "/api/v1/chats/pm/unread", getGroupStatistic: "/api/v1/chats/gm/statistic", getBadgeNumber: "/api/v1/application/badge_no", getRecentContacts: "/api/v1/chats/recent", getDDoctorImInfo: "/api/v1/chats/list/doctor",//搜索消息页的医生im列表 getDPatientImInfo: "/api/v1/chats/list/patient",//搜索消息页的居民im列表 getGroupsMemberAvatars: "/api/v1/groups/member/avatars",//搜索讨论组的组员头像 getSearchPatient: "/api/v1/chats/search/patient",//搜索消息页居民im-搜索接口 getSearchDoctor: "/api/v1/chats/search/doctor",//搜索消息页的居民im-搜索接口 getSearchZKList:"/api/v1/chats/list",//搜索专科医生的Im列表 getSearchPatientList: "/api/v1/chats/search/patient/list",//搜索居民im的详情列表 getSearchPatientAll: "/api/v1/chats/search/patient/all",//搜索居民im的查看更多列表( type参数:1、医生和患者 2、聊天记录 3、群聊) getSearchDoctorList: "/api/v1/chats/search/doctor/content/list",//搜索医生im的详情列表 getSearchDoctorAll: "/api/v1/chats/search/doctor/list",//搜索医生im的查看更多列表(type参数: type = 1 查询聊过的医生; type = 2 查询群组标题和人员包含的群聊 ; type = 3 查询聊天关键字) getWeiDuMessageCount: "/api/v1/chats/msg/amount",//获取医生im和居民im的未读消息数 consultFinish: "/api/v1/chats/pm/finished", getHealthConsult: "/api/v2/sessions/topics", updateConsultStatus :"/api/v2/sessions/sessionId/topics" }; //api/v1/groups/member/avatars?groups 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.status, sendGroupMsg: actualServer + restPath.sendGroupMsg, getGroupMsg: actualServer + restPath.getGroupMsg, getGroupUnreadMsg: actualServer + restPath.getGroupUnreadMsg, getGroupImgMsg: actualServer + restPath.getGroupImgMsg, getGroupStatistic: actualServer + restPath.getGroupStatistic, getBadgeNumber: actualServer + restPath.getBadgeNumber, getPrivateMsg: actualServer + restPath.getOneByOneMsg, getPrivateUnreadMsg: actualServer + restPath.getOneByOneUnreadMsg, sendPrivateMsg: actualServer + restPath.sendOneByOne, getRecentContacts: actualServer + restPath.getRecentContacts, getDDoctorImInfo: actualServer + restPath.getDDoctorImInfo, getDPatientImInfo: actualServer + restPath.getDPatientImInfo, getGroupsMemberAvatars :actualServer + restPath.getGroupsMemberAvatars, getSearchPatient: actualServer + restPath.getSearchPatient, getSearchDoctor: actualServer + restPath.getSearchDoctor, getSearchZKList: actualServer + restPath.getSearchZKList, getSearchPatientList: actualServer + restPath.getSearchPatientList, getSearchPatientAll: actualServer + restPath.getSearchPatientAll, getSearchDoctorList: actualServer + restPath.getSearchDoctorList, getSearchDoctorAll: actualServer + restPath.getSearchDoctorAll, getWeiDuMessageCount: actualServer + restPath.getWeiDuMessageCount, consultFinish: actualServer + restPath.consultFinish, getHealthConsult: actualServer + restPath.getHealthConsult, updateConsultStatus: actualServer + restPath.updateConsultStatus }; // 本地临时缓存Key var plusStorageKey = { userId: "im_userid" }; var im = { // 登录 login: function (userId, token, client_id, platform) { console.log("IM - Login: " + userId); console.log('endpoints.login'+endpoints.login); console.log(JSON.stringify({user_id: userId, token: token, client_id: client_id, platform: platform})); 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) { console.log(JSON.stringify(msg)); } }); }, // 退出 logout: function (userId) { plus.storage.removeItem(plusStorageKey.userId); $.ajax({ type: "get", 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) { //alert(endpoints.updateStatus.replace(':user_id', userId)); if(userId == undefined || userId == "")return; $.ajax({ type: "post", url: endpoints.updateStatus.replace(':user_id', userId), data: {user_id: userId, status: status}, async: true, dataType: "json", success: function (data) { console.log("logout:"+JSON.stringify(data)); }, error: function (msg) { console.log("logout:"+JSON.stringify(msg)); } }); }, // 发送群组消息 sendGroupMsg: function (userId, groupId, content, type, handler, group_type, err) { function send() { $.ajax({ type: "post", url: endpoints.sendGroupMsg, data: {at: "", from: userId, group: groupId, groupType: group_type, contentType: type, content: content}, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { console.log(JSON.stringify(msg)); if(err) err(msg); else{ mui.toast("发送失败") } } }); } checkLogin(send); }, // 获取组消息 getGroupMsg: function (uid, groupId, start, count, handler, contentType, end) { var p = {user_id: uid, group_id: groupId, count: count} if(start>0) p.message_start_id = start; if(end) p.message_end_id = end; if(contentType) p.content_type = contentType; $.ajax({ type: "get", url: endpoints.getGroupMsg, data: p, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { console.log("getGroupMsg:"+ JSON.stringify(msg)); mui.toast('获取消息失败'); } }); }, // 获取组未读消息 getGroupUnreadMsg: function (uid, groupId, handler) { $.ajax({ type: "get", url: endpoints.getGroupUnreadMsg, data: {user_id: uid, group_id: groupId}, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { console.log("getGroupUnreadMsg:"+ JSON.stringify(msg)); mui.toast("获取消息失败"); } }); }, // 获取组图片消息 getGroupImgMsg: function (uid, groupId, handler) { $.ajax({ type: "get", url: endpoints.getGroupImgMsg, data: {user_id: uid, group_id: groupId}, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { console.log(JSON.stringify(msg)); mui.toast("获取消息失败"); } }); }, // 获取组消息数 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, success,error) { 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) { if(success) success(data); }, error: function (data) { console.log(JSON.stringify(data)); if(error) error(data); mui.toast("发送失败") } }); } checkLogin(send); }, // 获取私信 getPrivateMsg: function (uid, peer_uid, start, count, handler, type, end) { var p = {user_id: uid, peer_id: peer_uid, count: count} if(start>0) p.message_start_id = start; if(end) p.message_end_id = end; if(type) p.content_type = type; $.ajax({ type: "get", url: endpoints.getPrivateMsg, data: p, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { mui.toast("获取聊天信息失败") } }); }, getPrivateUnreadMsg: function (uid, peer_uid, handler) { $.ajax({ type: "get", url: endpoints.getPrivateUnreadMsg, data: {user_id: uid, peer_id: peer_uid}, async: true, dataType: "json", success: function (data) { if(handler) handler(data); }, error: function (msg) { if(handler) 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); } }); }, // 获取最近联系人 getRecentContacts: function(userId, days,success,error) { $.ajax({ type: "get", url: endpoints.getRecentContacts, data: {user_id: userId,days: days || 7}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, // 获取医生与居民的IM记录 getDPatientInfo: function (userId, handler, errHandle) { $.ajax({ type: "get", url: endpoints.getDPatientImInfo, data: {user_id: userId}, async: true, dataType: "json", timeout: 5000, success: function (data) { console.log(JSON.stringify(data)); if (handler) handler(data); }, error: function (msg) { console.log('error'); if (errHandle) errHandle(msg); } }); }, // 获取医生与医生的IM记录 getDDoctorImInfo: function (userId, handler, errHandle) { $.ajax({ type: "get", url: endpoints.getDDoctorImInfo, data: {user_id: userId}, async: true, dataType: "json", timeout: 5000, success: function (data) { console.log(JSON.stringify(data)); if (handler) handler(data); }, error: function (msg) { console.log('error'); if (errHandle) errHandle(msg); } }); }, getGroupsMemberAvatars: function(groups,handler, errHandle){ $.ajax({ type: "get", url: endpoints.getGroupsMemberAvatars, data: {groups: groups}, async: true, dataType: "json", timeout: 5000, success: function (data) { console.log(JSON.stringify(data)); if (handler) handler(data); }, error: function (msg) { console.log('error'); if (errHandle) errHandle(msg); } }); }, getSearchPatient: function(userId,userRole,keyWord,handler){ $.ajax({ type: "get", url: endpoints.getSearchPatient, data: {user_id: userId,user_role:userRole,keyword:keyWord}, 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); } }); }, getSearchDoctor: function(userId,keyWord,success,error){ $.ajax({ type: "get", url: endpoints.getSearchDoctor, data: {user_id: userId,keyword:keyWord}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, consultFinish: function(user_id, peer_id, success, error){ $.ajax({ type: "get", url: endpoints.consultFinish, data: {doctor_id: user_id, patient_id:peer_id}, async: true, dataType: "json", success: function (data) { if (success) success(data); }, error: function (msg) { console.log(msg); if (error) error(msg); } }); }, getSearchZKList:function(userId,success,error){ $.ajax({ type: "get", url: endpoints.getSearchZKList, data: {user_id: userId}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getSearchPatientList: function(userId,keyWord,type,userRole,groupId,success,error){ $.ajax({ type: "get", url: endpoints.getSearchPatientList, data: {user_id: userId,keyword:keyWord,type:type,user_role:userRole,group_id:groupId}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getSearchPatientAll: function(userId,keyWord,type,userRole,success,error){ $.ajax({ type: "get", url: endpoints.getSearchPatientAll, data: {user_id: userId,keyword:keyWord,type:type,user_role:userRole}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getSearchDoctorList: function(userId,keyWord,type,groupCode,success,error){ $.ajax({ type: "get", url: endpoints.getSearchDoctorList, data: {user_id: userId,keyword:keyWord,type:type,groupcode:groupCode}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getSearchDoctorAll: function(userId,keyWord,type,success,error){ $.ajax({ type: "get", url: endpoints.getSearchDoctorAll, data: {user_id: userId,keyword:keyWord,type:type}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getWeiDuMessageCount :function(userId,success,error){ $.ajax({ type: "get", url: endpoints.getWeiDuMessageCount, data: {user_id: userId}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, getHealthConsult :function(user,status,page,pagesize,success,error){ $.ajax({ type: "get", url: endpoints.getHealthConsult, data: {user: user,status:status,page:page,pagesize:pagesize}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, updateConsultStatus :function(sessionId,id,status,success,error){ endpoints.updateConsultStatus = endpoints.updateConsultStatus.replace("sessionId",sessionId); $.ajax({ type: "put", url: endpoints.updateConsultStatus, data: {topic_id:id,data:JSON.stringify({status:status})}, async: true, dataType: "json", success: function (data) { console.log(JSON.stringify(data)); if (success) success(data); }, error: function (msg) { console.log('error'); if (error) error(msg); } }); }, }; /* 执行业务接口前,调用此函数判断当前用户是否在线。 */ function checkLogin(callback) { /*sendPost("/doctor/islive", {}, null, function (res) { if (res.status == 200) { callback(); } })*/ callback(); } im.sendGroup = im.sendGroupMsg; im.getGroup = im.getGroupMsg; im.getgroupchatinfo = im.getGroupInfo im.sendOneByOne = im.sendPrivateMsg im.getOneByOneMsg = im.getPrivateMsg im.getBadgeNumber = im.getBadgeNumber im.getDPatientInfo = im.getDPatientInfo im.getDDoctorImInfo = im.getDDoctorImInfo im.getGroupsMemberAvatars = im.getGroupsMemberAvatars im.getSearchPatient = im.getSearchPatient im.getSearchDoctor = im.getSearchDoctor im.getSearchZKList = im.getSearchZKList im.getSearchPatientList = im.getSearchPatientList im.getSearchPatientAll = im.getSearchPatientAll im.getSearchDoctorList = im.getSearchDoctorList im.getSearchDoctorAll = im.getSearchDoctorAll im.getWeiDuMessageCount = im.getWeiDuMessageCount im.getHealthConsult = im.getHealthConsult im.updateConsultStatus = im.updateConsultStatus