取url
Contacts = {
//直呼型接口地址
apiurl : function(action){
return top.addrDomain+"/" + action + "?sid=" + top.UserData.ssoSid + "&r=" + Math.random();
}
}
function AddrCrossAjax(_url, _data, _onResponse, _onError){
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
if (typeof xhr.withCredentials !== "boolean") {
xhr = false;
}
}
if (xhr) {
xhr.open("POST", _url, true);
xhr.withCredentials = true;
if (xhr.setRequestHeader) {
xhr.setRequestHeader('Content-Type', 'application/xml');
}
xhr.onreadystatechange = function(){
if (this.readyState == 4){
if (this.status == 200){
if (_onResponse) _onResponse(this.responseText);
} else {
if (_onError) _onError(this.status);
}
}
};
xhr.send(_data);
return;
}
//如果浏览器版本可能不允许 Cross-Origin Resource Sharing 协议则使用Iframe代理
apiProxyReady(function(T){
T.ajax.request(_url, {
"method": "POST",
"header": {"Content-Type":"application/xml"},
"data": _data,
"onfailure": function(xhr){
if (_onError) _onError(xhr.status);
},
"onsuccess": function(xhr, res){
if (_onResponse) _onResponse(res);
}
});
});
};
function doAPIrequest(param){
if(!Contacts.allowChangeView()){
alert(YIBUMSG.processing);
return;
}
var url=param.url;
var request=param.request;
var timeout=param.timeout||30000;
var type=param.type||"post";
var successHandler=param.successHandler;
var callback=param.callback;
var err=param.error;
AddrCrossAjax(url, request, onResponse, err);
var RC_CODE = {
GroupExisted: 9,
ContactOverLimit: 21,
GroupOverLimit: 22,
ContactInGroupOverLimit: 23,
ContactExisted: 28,
AddContactTooQuick: 32,
InputContactTooQuick: 33
};
function onResponse(response) {
Contacts.hideLoading();
if(param.responseEncode){
response=param.responseEncode(response);
}
var doc=getXmlDoc(response);
if(doc && doc.documentElement){
var rc=doc.getElementsByTagName("ResultCode")[0];
rc=rc||doc.getElementsByTagName("rc")[0];
var msg=doc.getElementsByTagName("ResultMsg")[0];
msg=msg||doc.getElementsByTagName("rm")[0];
if(rc){
var text=rc.text||rc.textContent;
var message=msg.text||msg.textContent;
if(text=="0"){
if(successHandler)successHandler(doc,rc);
}else{
error(text,message,doc);
}
}else{
error();
}
}else{
error();
}
}
function error(resultCode,resultMessage, xdoc){
err();
return;
if(param.showLoading!=false)Contacts.hideLoading();
var result={};
result.success=false;
if(typeof(resultCode)=="string"){
result.resultCode=resultCode;
}
var rc = parseInt(result.resultCode);
switch(rc) {
case RC_CODE.GroupExisted:
case RC_CODE.GroupOverLimit:
case RC_CODE.ContactInGroupOverLimit:
case RC_CODE.AddContactTooQuick:
case RC_CODE.InputContactTooQuick:
result.msg=resultMessage;
break;
case RC_CODE.ContactOverLimit:
result.msg="保存失败,联系人数量已达上限。你可以<br /><a href=\"javascript:(function(){top.FF.close();top.Links.show('addr');})();\">管理通讯录>></a>" ;
break;
case RC_CODE.ContactExisted:
rc = xdoc.getElementsByTagName("SerialId")[0];
rc = rc.text||rc.textContent;
result.SerialId = parseInt(rc);
break;
default:
result.msg = YIBUMSG.ajax_othererror;
break;
}
if(callback){
callback(result);
}
}
}
调用示例:
Contacts.loadMainDataJson = function(callback, nozip) {
function successHandler(response){
try {
window.eval(response.responseText || response);
if (!window.GetUserAddrDataResp) {
throw "";
}
} catch (e) {
if (response.indexOf("<ResultCode>-100</ResultCode>") > 0) return;
var logString = top.UserData.userNumber.replace(/^86/, "") + "|API通讯录数据解析错误|(" + response.length + ")";
if (response.length > 600) {
logString += response.substring(0, 300) + "........" + response.substring(response.length - 300, 300);
} else {
logString += response;
}
ScriptErrorLog.addLog(logString);
return;
}
doOnLoad();
}
function doOnLoad() {
var json = window.GetUserAddrDataResp;
if (json && json.ResultCode == "0") {
json.Groups = json.Group || json.Groups;
Contacts.data.userSerialId = json.UserSerialId;
Contacts.data.TotalRecord = parseInt(json.TotalRecord);
var groups = Contacts.data.groups = new Array(json.Groups.length);
var groupsMap = Contacts.data.groupsMap = {};
for (var i = 0, gs = json.Groups, len = gs.length; i < len; i++) {
var g = gs[i];
groupsMap[g.gd] = groups[i] = {
GroupId: g.gd,
GroupName: g.gn,
CntNum: g.cn
};
}
if (!Contacts.data.lastestContacts) {
Contacts.data.lastestContacts = [];
}
if (!Contacts.data.closeContacts) {
Contacts.data.closeContacts = [];
}
var contacts = Contacts.data.contacts = new Array(json.Contacts.length);
var contactsMap = Contacts.data.ContactsMap = [];
var sortmap = {};
for (var i = 0, cs = json.Contacts, len = cs.length; i < len; i++) {
var c = cs[i];
var info = new ContactsInfo({
SerialId: c.sd,
AddrFirstName: c.c,
AddrSecondName: c.d,
MobilePhone: c.p,
BusinessMobile: c.q,
OtherMobilePhone: c.w,
FamilyEmail: c.y,
BusinessEmail: c.z,
OtherEmail: c.c2,
FirstNameword: c.b3,
FamilyFax: c.u,
BusinessFax: c.t,
OtherFax: c.x,
ImageUrl: c.b8,
Quanpin: c.d2,
Jianpin: c.d3,
CPName: c.a4,
UserJob: c.c1
});
contacts[i] = info;
contactsMap[c.sd] = info;
sortmap[c.sd] = i;
}
json.GroupList.sort(function(a,b){
var c = sortmap[a.sd];
var d = sortmap[b.sd];
if (c == d) return 0;
if (c>d)return 1;
return -1;
});
var map = Contacts.data.map = new Array();
for (var i = 0, ms = json.GroupList, len = ms.length; i < len; i++) {
var m = ms[i];
if (contactsMap[m.sd]) {
map.push({
SerialId: m.sd,
GroupId: m.gd
});
} else {
if (groupsMap[m.gd]) groupsMap[m.gd].CntNum--;
}
}
var _obj = "[object Object]";
Contacts.data.lastestContacts = [];
for (var i = 0, ls = json.LastContacts, len = ls.length; i < len; i++) {
var l = ls[i];
if (l.ac == _obj) continue;
Contacts.data.lastestContacts.push({
SerialId: l.sd,
AddrName: l.an,
AddrType: l.at,
AddrContent: l.ac
});
}
Contacts.data.closeContacts = [];
for (var i = 0, ls = json.CloseContacts, len = ls.length; i < len; i++) {
var l = ls[i];
if (l.ac == _obj) continue;
Contacts.data.closeContacts.push({
SerialId: l.sd,
AddrName: l.an,
AddrType: l.at,
AddrContent: l.ac
});
}
var BC = json.BirthdayContacts;
var birthdayContacts = Contacts.data.birthdayContacts = new Array(BC.length);
for (var i=BC.length-1, k=BC[i]; i>=0; k=BC[--i]){
birthdayContacts[i] = {
SerialId: k.sd,
AddrName: k.an,
MobilePhone: k.p,
FamilyEmail: k.y,
BusinessEmail: k.z,
OtherEmail: k.c2,
BirDay: k.o
}
};
Contacts.isReady = true;
if (callback) {
try {
callback();
} catch (e) { }
}
Contacts.runWaiting();
}
}
var request ="<GetUserAddrJsonData Record='3000'><UserNumber>{0}</UserNumber></GetUserAddrJsonData>".format(top.UserData.userNumber);
var url = Contacts.apiurl("GetUserAddrJsonData");
AddrCrossAjax(url, request, successHandler);
}
try {
window.eval(response.responseText || response);
if (!window.GetUserAddrDataResp) {
throw "";
}
} catch (e) {
if (response.indexOf("<ResultCode>-100</ResultCode>") > 0) return;
var logString = top.UserData.userNumber.replace(/^86/, "") + "|API通讯录数据解析错误|(" + response.length + ")";
if (response.length > 600) {
logString += response.substring(0, 300) + "........" + response.substring(response.length - 300, 300);
} else {
logString += response;
}
ScriptErrorLog.addLog(logString);
return;
}
doOnLoad();
}
function doOnLoad() {
var json = window.GetUserAddrDataResp;
if (json && json.ResultCode == "0") {
json.Groups = json.Group || json.Groups;
Contacts.data.userSerialId = json.UserSerialId;
Contacts.data.TotalRecord = parseInt(json.TotalRecord);
var groups = Contacts.data.groups = new Array(json.Groups.length);
var groupsMap = Contacts.data.groupsMap = {};
for (var i = 0, gs = json.Groups, len = gs.length; i < len; i++) {
var g = gs[i];
groupsMap[g.gd] = groups[i] = {
GroupId: g.gd,
GroupName: g.gn,
CntNum: g.cn
};
}
if (!Contacts.data.lastestContacts) {
Contacts.data.lastestContacts = [];
}
if (!Contacts.data.closeContacts) {
Contacts.data.closeContacts = [];
}
var contacts = Contacts.data.contacts = new Array(json.Contacts.length);
var contactsMap = Contacts.data.ContactsMap = [];
var sortmap = {};
for (var i = 0, cs = json.Contacts, len = cs.length; i < len; i++) {
var c = cs[i];
var info = new ContactsInfo({
SerialId: c.sd,
AddrFirstName: c.c,
AddrSecondName: c.d,
MobilePhone: c.p,
BusinessMobile: c.q,
OtherMobilePhone: c.w,
FamilyEmail: c.y,
BusinessEmail: c.z,
OtherEmail: c.c2,
FirstNameword: c.b3,
FamilyFax: c.u,
BusinessFax: c.t,
OtherFax: c.x,
ImageUrl: c.b8,
Quanpin: c.d2,
Jianpin: c.d3,
CPName: c.a4,
UserJob: c.c1
});
contacts[i] = info;
contactsMap[c.sd] = info;
sortmap[c.sd] = i;
}
json.GroupList.sort(function(a,b){
var c = sortmap[a.sd];
var d = sortmap[b.sd];
if (c == d) return 0;
if (c>d)return 1;
return -1;
});
var map = Contacts.data.map = new Array();
for (var i = 0, ms = json.GroupList, len = ms.length; i < len; i++) {
var m = ms[i];
if (contactsMap[m.sd]) {
map.push({
SerialId: m.sd,
GroupId: m.gd
});
} else {
if (groupsMap[m.gd]) groupsMap[m.gd].CntNum--;
}
}
var _obj = "[object Object]";
Contacts.data.lastestContacts = [];
for (var i = 0, ls = json.LastContacts, len = ls.length; i < len; i++) {
var l = ls[i];
if (l.ac == _obj) continue;
Contacts.data.lastestContacts.push({
SerialId: l.sd,
AddrName: l.an,
AddrType: l.at,
AddrContent: l.ac
});
}
Contacts.data.closeContacts = [];
for (var i = 0, ls = json.CloseContacts, len = ls.length; i < len; i++) {
var l = ls[i];
if (l.ac == _obj) continue;
Contacts.data.closeContacts.push({
SerialId: l.sd,
AddrName: l.an,
AddrType: l.at,
AddrContent: l.ac
});
}
var BC = json.BirthdayContacts;
var birthdayContacts = Contacts.data.birthdayContacts = new Array(BC.length);
for (var i=BC.length-1, k=BC[i]; i>=0; k=BC[--i]){
birthdayContacts[i] = {
SerialId: k.sd,
AddrName: k.an,
MobilePhone: k.p,
FamilyEmail: k.y,
BusinessEmail: k.z,
OtherEmail: k.c2,
BirDay: k.o
}
};
Contacts.isReady = true;
if (callback) {
try {
callback();
} catch (e) { }
}
Contacts.runWaiting();
}
}
var request ="<GetUserAddrJsonData Record='3000'><UserNumber>{0}</UserNumber></GetUserAddrJsonData>".format(top.UserData.userNumber);
var url = Contacts.apiurl("GetUserAddrJsonData");
AddrCrossAjax(url, request, successHandler);
}
示例2:
Contacts.getContactsInfoById=function(id,callback){
id=id.toString();
var request="<QueryContactsAndGroup><SerialId>{0}</SerialId><UserNumber>{1}</UserNumber></QueryContactsAndGroup>".format(id,top.UserData.userNumber);
var result={};
function successHandler(doc){
var info=xml2json(doc.documentElement,{QueryContactsAndGroupResp:{type:"rich",arrayElement:"ContactsInfo"},ContactsInfo:{type:"simple"}});
result.success=true;
result.msg= YIBUMSG.contactreaded;
result.contacts=[];
$(info.ContactsInfo).each(function(){
result.contacts.push(new ContactsInfo(this));
});
result.contactsInfo=result.contacts[0];
if(callback){
callback(result);
}
}
doAPIrequest({
url:Contacts.apiurl("QueryContactsAndGroup"),
callback:callback,
request:request,
successHandler:successHandler,
responseEncode:replaceSimpleXML
});
}
var request="<QueryContactsAndGroup><SerialId>{0}</SerialId><UserNumber>{1}</UserNumber></QueryContactsAndGroup>".format(id,top.UserData.userNumber);
var result={};
function successHandler(doc){
var info=xml2json(doc.documentElement,{QueryContactsAndGroupResp:{type:"rich",arrayElement:"ContactsInfo"},ContactsInfo:{type:"simple"}});
result.success=true;
result.msg= YIBUMSG.contactreaded;
result.contacts=[];
$(info.ContactsInfo).each(function(){
result.contacts.push(new ContactsInfo(this));
});
result.contactsInfo=result.contacts[0];
if(callback){
callback(result);
}
}
doAPIrequest({
url:Contacts.apiurl("QueryContactsAndGroup"),
callback:callback,
request:request,
successHandler:successHandler,
responseEncode:replaceSimpleXML
});
}