123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- IM客户端SDK。此SDK可以连接开发、测试或生产环境,根据需要配置环境参数以连接到不同的服务器。
- */
- var isProd = false; // 连接生产环境
- var isDemo = false; // 连接公司演示或开发测试环境
- // 服务器列表
- var servers = {
- dev: "http://172.19.103.76:3000",
- demo: "http://ehr.yihu.com",
- prod: "http://120.41.253.95:3000"
- };
- // REST接口相对路径
- var restPath = {
- login: "/user/login.im",
- logout: "/user/logout.im",
- updateStatus: "/user/updatestatus.im",
- sendGroup: "/group/sendmsg.im",
- getGroupMsg: "/group/getmsg.im",
- sendOneByOne: "/p2p/sendmsg.im",
- getOneByOneMsg: "/p2p/getmsg.im",
- getNewMsgCount: "/statistic/getgroupchatinfo.im",
- getBadgeNumber: "/statistic/getbadgenum.im"
- };
- 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,
- sendGroup: actualServer + restPath.sendGroupMsg,
- getGroupMsg: actualServer + restPath.getGroupMsg,
- sendOneByOne: actualServer + restPath.sendPrivateMsg,
- getOneByOneMsg: actualServer + restPath.getPrivateMsg,
- getNewMsgCount: actualServer + restPath.getNewMsgCount,
- getBadgeNumber: actualServer + restPath.getBadgeNumber
- };
- // 本地临时缓存Key
- var plusStorageKey = {
- userId: "im.user.id"
- };
- 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: "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) {
- // 更新用户在线状态
- $.ajax({
- type: "get",
- url: endpoints.updateStatus,
- 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: "get",
- url: endpoints.sendGroupMsg,
- data: {from_uid: userId, to_gid: groupId, content: content, type: type, group_type: group_type},
- 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: {uid: uid, gid: groupId, start: 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.getNewMsgCount,
- data: {uid: uid, gid: 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: "get",
- url: endpoints.sendPrivateMsg,
- data: {from_uid: from_uid, to_uid: to_uid, content: content, type: 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: "get",
- url: endpoints.sendPrivateMsg,
- data: {from_uid: from_uid, to_uid: to_uid, content: content, 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: {gid: groupId, start: start, count: count},
- async: true,
- dataType: "json",
- success: function (data) {
- console.log(JSON.stringify(data));
- handler(data);
- },
- error: function (msg) {
- handler(msg);
- }
- });
- },
- getPagedPrivateMsg: function (uid, peer_uid, start, count, handler) {
- $.ajax({
- type: "get",
- url: endpoints.getPrivateMsg,
- data: {uid: uid, peer_uid: peer_uid, start: 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: {uid: 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();
- }
- })
- }
|