|
/* |
|
this is a client run in browser |
|
*/ |
|
|
|
|
|
|
|
(function(){ |
|
|
|
//ticket query days |
|
var due = ['2012-01-15','2012-01-16','2012-01-17','2012-01-18','2012-01-19']; |
|
//form station |
|
var from = '北京西'; |
|
//to station |
|
var to = '陇西'; |
|
//the train number.if set null,query all matched train |
|
var train = null; |
|
//how many tick |
|
var many = 1; |
|
//notification url |
|
var url = 'http://localhost:8080/ticket'; |
|
//query durition |
|
var durition = 30; |
|
//form soft berth, superior soft berth is 8; |
|
var form_set = 9; |
|
//to hard berth |
|
var to_set = 11; |
|
//submit the order form |
|
var submited = true; |
|
window.sid = null; |
|
var win = document.getElementsByTagName('iframe')[0].contentWindow; |
|
|
|
var days = function (){ |
|
var i = 0; |
|
return function(){ |
|
var val = due[i++]; |
|
if(!val){ |
|
i = 0 ; |
|
val = due[i++]; |
|
} |
|
return val; |
|
} |
|
}(); |
|
|
|
Date.prototype.availabe = function(){ |
|
var hours = this.getHours(), |
|
minute = this.getMinutes(); |
|
if(hours === 6 ){ |
|
return minute > 5; |
|
} |
|
return hours >= 6 && hours < 23 |
|
}; |
|
|
|
|
|
setup(); |
|
dispatch(); |
|
|
|
//setup input |
|
function setup(){ |
|
win.$.each(win.array_cities,function(i,v){ |
|
if(v[1] === from){ |
|
win.$('#fromStation').val(v[2]); |
|
} |
|
if(v[1] === to){ |
|
win.$('#toStation').val(v[2]); |
|
} |
|
}); |
|
win.$('#fromStationText').val(from); |
|
win.$('#toStationText').val(to); |
|
train && win.$('#trainCodeText').val(train.toUpperCase()); |
|
} |
|
|
|
|
|
//for safira |
|
if(win.$.browser.webkit){ |
|
win.WdatePicker=function(){}; |
|
win.$("#startdatepicker").attr('readonly',''); |
|
} |
|
|
|
|
|
var _load = win.removeLoadMsg; |
|
win.removeLoadMsg = function(){ |
|
_load(); |
|
var trs = win.$('#gridbox table.obj tr:gt(0)'); |
|
var get = false; |
|
//for each row |
|
trs.each(function(i,tr){ |
|
tr = $(tr); |
|
var eles = tr.children().slice(9,11); |
|
var train = tr.children().slice(0,1).text() |
|
var input = tr.find('input'); |
|
eles.each(function(ind,ele){ |
|
//we have get ticket. |
|
if(get){ |
|
return; |
|
} |
|
var text = $(ele).text(); |
|
if( ~text.indexOf('有') || parseInt(text) >= many ){ |
|
if(submited) { |
|
get = true; |
|
}; |
|
//send notification |
|
if(url){ |
|
var img = new Image; |
|
img.src = url + '?submit=' + (submited ? 1 : 0) + '&train='+ train + '&many=' + text; |
|
} |
|
submited && input.click(); |
|
} |
|
}); |
|
}); |
|
!get && (window.sid = setTimeout(dispatch , durition * 1000)); |
|
} |
|
|
|
|
|
function _method(){ |
|
//'confirmPassengerAction.do'; |
|
return win.location.href.indexOf('querySingleAction.do') ? |
|
'query' : |
|
'order'; |
|
}; |
|
|
|
function dispatch(){ |
|
if(!(new Date).availabe()){ |
|
var img = new Image; |
|
img.src = 'https://dynamic.12306.cn/otsweb/'; |
|
window.sid = setTimeout(dispatch, 3 * 60 * 1000); |
|
}else{ |
|
var method = _method(); |
|
if(method === 'query'){ |
|
setup(); |
|
query() |
|
}else{ |
|
order() |
|
} |
|
} |
|
}; |
|
|
|
function order(){ |
|
//win.$('#showPassengerFilter input').click(); |
|
}; |
|
|
|
function query(){ |
|
//setup start day. |
|
win.$("#startdatepicker").val(days()); |
|
win.$('#submitQuery').click(); |
|
}; |
|
|
|
|
|
//remove annoying alert dialog |
|
String.prototype.replaceAll = win.String.prototype.replaceAll; |
|
win.loadData = function() { |
|
var gridbox = win.$("#gridbox"); |
|
win.showLoadMsg(gridbox); |
|
//异步请求站名 |
|
win.$.ajax( { |
|
url : win.ctx+'/order/querySingleAction.do?method=queryLeftTicket', |
|
type : "GET", |
|
dataType:'text', |
|
data:{ |
|
'orderRequest.train_date' : win.$('#startdatepicker').val(), |
|
'orderRequest.from_station_telecode' : win.$('#fromStation').val(), |
|
'orderRequest.to_station_telecode' : win.$('#toStation').val(), |
|
'orderRequest.train_no' : win.$('#trainCode').val(), |
|
'trainPassType' : win.getTrainPassType(), |
|
'trainClass' : win.getTrainClassString(), |
|
'includeStudent' : win.getIncludeStudent(), |
|
'seatTypeAndNum' : win.getSeanTypeAndNum(), |
|
'orderRequest.start_time_str' : win.$('#startTime').val() |
|
}, |
|
success : function(data, textStatus) { |
|
if(data == "-10"){ |
|
console && console.log('recive redirect'); |
|
return; |
|
} |
|
if(data == "-1") { |
|
console && console.log('server busy.'); |
|
data = ""; |
|
} else if(data !="undefine" &&data.split(",")[0]=="-2"){ |
|
alert(data.split(",")[1]); |
|
data=""; |
|
|
|
}else { |
|
data=data.replaceAll("\\\\n",String.fromCharCode(10)); |
|
} |
|
win.mygrid.clearAll(); |
|
win.mygrid.startFastOperations(); |
|
win.mygrid.parse(data,"csv"); |
|
win.mygrid.stopFastOperations(); |
|
win.dealwithQueryInfo(win.mygrid); |
|
|
|
win.removeLoadMsg(); |
|
}, |
|
error : function(e) { |
|
console && console.error('loading error'); |
|
win.removeLoadMsg(); |
|
win.validQueryButton(); |
|
if(win.isStudentTicketDateValid()){ |
|
win.stu_validQueryButton(); |
|
} |
|
if(clickBuyStudentTicket=='N'){ |
|
renameButton('research_u'); |
|
}else{ |
|
stu_renameButton('research_u'); |
|
} |
|
} |
|
}); |
|
} |
|
}()); |
|
|
|
|