|
@ -2,6 +2,22 @@
|
|
|
* 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"
|
|
@ -55,78 +71,82 @@ var ENDPOINTS = {
|
|
|
Participant: '/sessions/:session_id/participants/:participant_id',
|
|
|
ParticipantAvatar: '/session/:session_id/participants/:participant_id/avatars'
|
|
|
},
|
|
|
Search: {
|
|
|
|
|
|
}
|
|
|
Search: {}
|
|
|
};
|
|
|
|
|
|
// 将API路径加上主机名称
|
|
|
(function regularEndpoints() {
|
|
|
for (var catalog in ENDPOINTS) {
|
|
|
for(var path in catalog){
|
|
|
ENDPOINTS.getProperty(catalog).setProperty(path, server + path);
|
|
|
}
|
|
|
}
|
|
|
})();
|
|
|
/*(function regularEndpoints() {
|
|
|
for (var catalog in ENDPOINTS) {
|
|
|
for(var path in catalog){
|
|
|
ENDPOINTS.getProperty(catalog).setProperty(path, server + path);
|
|
|
}
|
|
|
}
|
|
|
})();*/
|
|
|
|
|
|
var httpClient = {
|
|
|
get: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: endpoint,
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
post: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "post",
|
|
|
url: endpoint,
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
put: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "put",
|
|
|
url: endpoint,
|
|
|
type: "post",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
headers: {
|
|
|
"X-HTTP-Method-Override": "PUT"
|
|
|
},
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
delete: function (endpoint, data, successCallback, errorCallback) {
|
|
|
$.ajax({
|
|
|
type: "delete",
|
|
|
url: endpoint,
|
|
|
type: "get",
|
|
|
url: server + endpoint,
|
|
|
data: data,
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
headers: {
|
|
|
"X-HTTP-Method-Override": "DELETE"
|
|
|
},
|
|
|
success: function (data) {
|
|
|
successCallback(data);
|
|
|
},
|
|
|
error: function (message) {
|
|
|
errorCallback(message);
|
|
|
error: function (xhr, status, error) {
|
|
|
errorCallback(xhr, status, error);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
@ -165,9 +185,9 @@ var imClient = {
|
|
|
Users: {
|
|
|
// 登录
|
|
|
login: function (userId, token, client_id, platform, success, failure) {
|
|
|
plus.storage.setItem(LocalStorageKey.userId, userId);
|
|
|
if (typeof plus !== 'undefined') plus.storage.setItem(LocalStorageKey.userId, userId);
|
|
|
|
|
|
httpClient.post(ENDPOINTS.Login,
|
|
|
httpClient.post(ENDPOINTS.Users.Login,
|
|
|
{user_id: userId, token: token, client_id: client_id, platform: platform},
|
|
|
success,
|
|
|
failure);
|
|
@ -175,9 +195,9 @@ var imClient = {
|
|
|
|
|
|
// 退出
|
|
|
logout: function (userId, success, failure) {
|
|
|
plus.storage.removeItem(LocalStorageKey.userId);
|
|
|
if (typeof plus !== 'undefined') plus.storage.removeItem(LocalStorageKey.userId);
|
|
|
|
|
|
httpClient.delete(ENDPOINTS.Logout,
|
|
|
httpClient.delete(ENDPOINTS.Users.Logout,
|
|
|
{user_id: userId},
|
|
|
success,
|
|
|
failure);
|
|
@ -185,23 +205,10 @@ var imClient = {
|
|
|
|
|
|
// 更新用户状态
|
|
|
updateStatus: function (userId, status, success, failure) {
|
|
|
httpClient.put(ENDPOINTS.UserStatus.replace(UserPath, userId),
|
|
|
httpClient.put(ENDPOINTS.Users.UserStatus.replace(UserPath, userId),
|
|
|
{status: status},
|
|
|
success,
|
|
|
failure);
|
|
|
|
|
|
$.ajax({
|
|
|
type: "get",
|
|
|
url: ENDPOINTS.UserStatus.replace(UserPath, userId),
|
|
|
data: {user_id: userId, status: status},
|
|
|
async: true,
|
|
|
dataType: "json",
|
|
|
success: function (data) {
|
|
|
console.log(JSON.stringify(data));
|
|
|
},
|
|
|
error: function (msg) {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
},
|
|
|
Sessions: {
|
|
@ -384,3 +391,5 @@ var imClient = {
|
|
|
},
|
|
|
Search: {}
|
|
|
};
|
|
|
|
|
|
module.exports = imClient;
|