258 lines
7.5 KiB
JavaScript
258 lines
7.5 KiB
JavaScript
var cn12306 = {
|
||
coreVersion: "1.0.0.0",
|
||
baseUri: "https://kyfw.12306.cn/otn/",
|
||
queryBaseUri: "http://dynamic.12306.cn/otsquery/",
|
||
queryUri:'leftTicket/query',
|
||
ajaxQueue: {},
|
||
ajaxCount: 0,
|
||
getFullUri: function(url) {
|
||
return url[4] === ":" || url[5] === ":" ? url : cn12306.baseUri + url;
|
||
},
|
||
recode: function() {
|
||
|
||
$('#randcodeimg').attr('src', $('#randcodeimg').attr('data-loading'));
|
||
$('#randcode').val('').focus();
|
||
|
||
cn12306.loadRandCode('sjrand', function(data) {
|
||
$('#randcodeimg').attr('src', data);
|
||
}, "login/init");
|
||
},
|
||
/**
|
||
* 获取验证码
|
||
* @param {String} flag 验证码类型(sjrand|randp)
|
||
* @param {Function} callback 获取后回调
|
||
* @param {String} refer Header refer
|
||
* @return {Img uri} base64图片url
|
||
*/
|
||
loadRandCode: function(flag, callback, refer) {
|
||
var url = cn12306.getFullUri(flag[4] === ":" || flag[5] === ":" ? flag : (function(f) {
|
||
if (flag == "sjrand") {
|
||
return "passcodeNew/getPassCodeNew?module=login&rand=sjrand";
|
||
} else if (flag == "randp") {
|
||
return "passcodeNew/getPassCodeNew?module=passenger&rand=randp";
|
||
}
|
||
Public.alert("不支持的验证码类型!");
|
||
})(flag));
|
||
|
||
bootStrap.getImage(url, refer).done(callback).fail(function() {
|
||
Public.alert("加载验证码失败,请点击验证码刷新");
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 获取ajax url
|
||
* @param {String} url ajax url
|
||
* @return {String} ajax url
|
||
*/
|
||
getAjaxUrl: function(url) {
|
||
// return (isAndLiebao || isIos || isWebApp) ? cn12306.getFullUri(url) : baseProxyUrl;
|
||
return (isAndLiebao || isIos) ? cn12306.getFullUri(url) : baseProxyUrl;
|
||
},
|
||
|
||
/**
|
||
* 获取请求头
|
||
* @param {String} url ajax url
|
||
* @param {String} refer Header refer
|
||
* @return {Object} 所有header
|
||
*/
|
||
getHeaders: function(url, refer) {
|
||
var url = cn12306.getFullUri(url),
|
||
refer = cn12306.getFullUri(refer),
|
||
headers = {},
|
||
prefix = isAndLiebao ? "" : "Fish-";
|
||
|
||
headers[prefix + "User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
|
||
headers[prefix + "Origin"] = /(https?:\/\/[^\/]+\/)/.exec(url)[1],
|
||
headers[prefix + "RawUrl"] = url
|
||
headers[prefix + "Referer"] = refer;
|
||
// if (!(isAndLiebao || isIos || isWebApp)) {
|
||
if (!(isAndLiebao || isIos)) {
|
||
headers["Fish-RawUrl"] = url;
|
||
}
|
||
return headers;
|
||
},
|
||
|
||
/**
|
||
* 验证验证码
|
||
* @param {String} type 验证码类型
|
||
* @param {String} code 需要验证的验证码
|
||
* @param {Function} success 成功回调
|
||
* @param {Function} error 失败回调
|
||
* @param {String} refer Header请求头refer
|
||
* @return {Ajax Callback} 执行回调
|
||
*/
|
||
checkRandCode: function(type, code, success, error, refer) {
|
||
var url = cn12306.getFullUri("passcodeNew/checkRandCodeAnsyn"),
|
||
refer = cn12306.getFullUri(refer),
|
||
headers = cn12306.getHeaders(url, refer);
|
||
|
||
bootStrap.post("passcodeNew/checkRandCodeAnsyn", null, null, refer).done(function(data) {
|
||
if (typeof success == 'function') {
|
||
success(data);
|
||
}
|
||
}).fail(function(xhr, type) {
|
||
if (typeof error == 'function') {
|
||
error(xhr, type);
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 测试指定用户是否已经登录
|
||
* @param {function} loginedCallback 已经登录回调
|
||
* @param {Function} unloginCallback 未登录回调
|
||
* @param {Function} errorCallback 错误回调
|
||
*/
|
||
isUserLogined: function(loginedCallback, unloginCallback, errorCallback) {
|
||
var url = "login/checkUser",
|
||
refer = 'leftTicket/init';
|
||
|
||
bootStrap.post("login/checkUser", "json", {
|
||
"_json_att": ""
|
||
}, "leftTicket/init").done(function(json) {
|
||
if (json && json.data && json.data.flag) {
|
||
if (json.attributes) {
|
||
sessionStorage["useratts"] = json.attributes;
|
||
};
|
||
if (!cn12306.checking) {
|
||
cn12306.checkeUser();
|
||
};
|
||
(typeof loginedCallback == "function") && loginedCallback();
|
||
} else {
|
||
(typeof unloginCallback == "function") && unloginCallback();
|
||
}
|
||
}).fail(function(xhr, type) {
|
||
(typeof errorCallback == "function") && errorCallback();
|
||
});
|
||
},
|
||
|
||
ajax: function(o) {
|
||
var obj = {
|
||
"type": 'POST',
|
||
"dataType": 'json'
|
||
}
|
||
|
||
$.extend(obj, o || {});
|
||
|
||
obj["headers"] = cn12306.getHeaders(obj["url"], obj["refer"]);
|
||
|
||
if (typeof obj["url"] != 'undefined') {
|
||
obj["url"] = cn12306.getAjaxUrl(cn12306.getFullUri(obj["url"]));
|
||
}
|
||
if (typeof obj["refer"] != 'undefined') {
|
||
obj["refer"] = cn12306.getAjaxUrl(obj["refer"]);
|
||
}
|
||
$.ajax(obj);
|
||
},
|
||
checking: false,
|
||
checkeUser: function() {
|
||
cn12306.checking = true;
|
||
bootStrap.post('login/checkUser', 'json', null, 'login/init').done(function() {
|
||
setTimeout(cn12306.checkeUser, 60000);
|
||
}).fail(function() {
|
||
setTimeout(cn12306.checkeUser, 60000);
|
||
});
|
||
},
|
||
loginOut: function(success, error) {
|
||
var loading = Public.showLoading('正在退出');
|
||
bootStrap.post('login/loginOut', 'html', null, 'login/init').done(function() {
|
||
$('.loading',loading).html('退出成功');
|
||
setTimeout(function(){
|
||
Public.hideLoading(loading);
|
||
},1000);
|
||
if (success) {
|
||
sessionStorage.clear();
|
||
success();
|
||
}
|
||
}).fail(function(e) {
|
||
$('.loading',loading).html('退出失败');
|
||
setTimeout(function(){
|
||
Public.hideLoading(loading);
|
||
},1000);
|
||
if (error) {
|
||
error(e);
|
||
}
|
||
});
|
||
},
|
||
queryTicket:function(data,success,error){
|
||
bootStrap.get(cn12306.queryUri, "json", data, "leftTicket/init").done(function(json) {
|
||
if (json["status"] == true && json["httpstatus"] == 200 && json['data'].length > 0) {
|
||
cn12306.listGetTrainNo(json['data']);
|
||
}
|
||
|
||
if (json["status"] == false && !!json["c_url"]) {
|
||
cn12306.queryUri = json['c_url'];
|
||
cn12306.queryTicket(data,success,error);
|
||
}else if (typeof success == 'function') {
|
||
success(json);
|
||
}
|
||
}).fail(function(xhr, type) {
|
||
if (typeof success == 'function') {
|
||
error(xhr, type);
|
||
}
|
||
});
|
||
},
|
||
byTrainNos:{},
|
||
setLocalTrainNos:function(key,data){
|
||
if(!cn12306.byTrainNos[key]){
|
||
cn12306.byTrainNos[key] = data;
|
||
localStorage.setItem('localTrainNos',JSON.stringify(cn12306.byTrainNos));
|
||
}
|
||
},
|
||
getLocalTrainNos:function(){
|
||
var nos = localStorage.getItem('localTrainNos');
|
||
if(nos){
|
||
cn12306.byTrainNos = JSON.parse(nos);
|
||
}
|
||
},
|
||
listGetTrainNo:function(list){
|
||
if(!!list && list.length > 0){
|
||
for (var i = 0,len = list.length; i < list.length; i++) {
|
||
cn12306.queryByTrainNos({
|
||
'train_no':list[i]['queryLeftNewDTO']['train_no'],
|
||
'from_station_telecode':list[i]['queryLeftNewDTO']['from_station_telecode'],
|
||
'to_station_telecode':list[i]['queryLeftNewDTO']['to_station_telecode'],
|
||
'depart_date':list[i]['queryLeftNewDTO']['start_train_date'].replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3")
|
||
});
|
||
};
|
||
}
|
||
},
|
||
loadingInfo:[],
|
||
queryByTrainNos:function(formData,success,error){
|
||
|
||
// train_no=240000G6670A
|
||
// from_station_telecode=BXP
|
||
// to_station_telecode=LLF
|
||
// depart_date=2014-08-30
|
||
if(!!cn12306.byTrainNos[formData['train_no']]){
|
||
if(typeof success == 'function'){
|
||
success(cn12306.byTrainNos[formData['train_no']]);
|
||
}
|
||
return ;
|
||
}
|
||
|
||
if(cn12306.loadingInfo.indexOf(formData['train_no']) != -1){
|
||
return ;
|
||
}
|
||
|
||
cn12306.loadingInfo.push(formData['train_no']);
|
||
|
||
bootStrap.get('czxx/queryByTrainNo', null, formData, 'leftTicket/init').done(function(data) {
|
||
var idx = cn12306.loadingInfo.indexOf(formData['train_no']);
|
||
if(idx != -1){
|
||
cn12306.loadingInfo.splice(idx,1);
|
||
}
|
||
|
||
if(data['status']){
|
||
cn12306.setLocalTrainNos(formData['train_no'],data['data']['data']);
|
||
}
|
||
if (typeof success == 'function') {
|
||
success(data);
|
||
}
|
||
}).fail(function(xhr, type) {
|
||
if (typeof error == 'function') {
|
||
error(xhr, type);
|
||
}
|
||
});
|
||
}
|
||
} |