Light12306/RwTicketAssistantV2/app/infobar/scripts/core.js
2014-09-05 18:34:16 +08:00

291 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$.fn.extend({
searchDelay: function (opts) {
var SearchDelay = function (e, opt) {
var __ = this;
this.$element = e;
this.timer = null;
this.opt = $.extend(SearchDelay.defaults, opt);
this.$element.keyup(function () {
__.reset();
__.timer = setTimeout(function () {
__.search.apply(__);
}, __.opt.delay);
});
};
SearchDelay.defaults = {
delay: 300
};
SearchDelay.prototype.search = function () {
this.$element.trigger("search.fish.searchdelay", this.$element.val());
};
SearchDelay.prototype.reset = function () {
if (this.timer)
clearTimeout(this.timer);
};
SearchDelay.prototype.cancel = function () {
this.reset();
this.$element.val("");
this.search();
};
this.each(function () {
var key = "fish.searchdelay";
var $ele = $(this);
var target = $ele.data(key);
if (!target) {
target = new SearchDelay($ele, opts);
$ele.data(key, target);
}
if (typeof (opts) === "string") {
target[opts].apply(target, $.makeArray(arguments).slice(1));
}
});
return this;
},
nativeClick: function () {
/// <summary>触发原生的点击</summary>
this.each(function () {
this.click && this.click();
});
return this;
}
});
var otsweb = (function () {
var fun = function () {
var e = this;
var queryUrl = "http://kyfw.12306.cn/otn/leftTicket/query";
var getTicketInfo = function (v) {
var data = {}, info = v.indexOf("#") === -1 ? v : /getSelected\(['"](.*?)['"]\)/i.exec(v)[1].split('#')[11],
match = info.match(/([A-Z\d])0*?([\*\d]{5})0*?(\d{4})/gi);
for (var j in match) {
var m = /([A-Z\d])0*?([\*\d]{5})0*?(\d{4})/i.exec(match[j]);
var sc = m[1];
var sp = m[2][0] == '*' ? null : parseInt(m[2], 10);
var st = parseInt(m[3], 10);
if (st < 3000) {
data[sc] = st;
data["_" + sc] = sp;
//一等软座 7 二等软座 8
if (sc == "7") {
data['M'] = st;
data["_M"] = sp;
}
else if (sc == "8") {
data['O'] = st;
data['_O'] = sp;
}
} else {
data['0'] = st - 3000;
data['_0'] = sp;
}
};
return data;
};
this.baseurl = "http://dynamic.12306.cn/otsweb/";
this.getUrl = function (url) {
if (!url) return null;
if (url.indexOf("://") != -1)
return url;
return e.baseurl + url;
};
this.ajax = function (method, url, refer, data, type, done, fail) {
url = e.getUrl(url);
var opt = {
crossDomain: false,
data: data,
dataType: type || "json",
headers: {
"Fish-Referer": e.getUrl(refer)
},
type: method || "GET",
timeout: 10000
};
return $.ajax(url, opt).done(done).fail(fail);
};
this.ajaxGet = function (url, refer, data, type, done, fail) {
return e.ajax("GET", url, refer, data, type, done, fail);
};
this.ajaxPost = function (url, refer, data, type, done, fail) {
return e.ajax("POST", url, refer, data, type, done, fail);
};
this.queryTicket = function () {
if (ISOTN) e.queryTicketOtn.apply(this, $.makeArray(arguments));
else e.queryTicketDynamic.apply(this, $.makeArray(arguments));
};
this.queryTicketOtn = function (from, to, date, id, code, callback) {
var data = {
"leftTicketDTO.train_date": date,
"leftTicketDTO.from_station": from,
"leftTicketDTO.to_station": to,
"purpose_codes": code == "00" ? "ADULT" : code
};
e.ajaxGet(queryUrl, "http://kyfw.12306.cn/otn/leftTicket/init", data, "json", function (response) {
if (!response) {
callback(null);
return;
}
if (response.c_url) {
queryUrl = "http://kyfw.12306.cn/otn/" + response.c_url;
e.queryTicketOtn.apply(e, [].slice.call(arguments));
return;
}
if (!response.status) {
callback(null);
return;
}
//查找车次
var train = _.find(response.data, function (e) {
return e.queryLeftNewDTO["train_no"] == id;
});
if (!train || train.queryLeftNewDTO.canWebBuy !== 'Y')
callback(null);
else {
callback(getTicketInfo(train.queryLeftNewDTO.yp_info));
}
});
};
this.queryTicketDynamic = function (from, to, date, id, code, callback) {
var queryLeftData = {
'orderRequest.train_date': date,
'orderRequest.from_station_telecode': from,
'orderRequest.to_station_telecode': to,
'orderRequest.train_no': id,
'trainPassType': 'QB',
'trainClass': 'QB#D#Z#T#K#QT#',
'includeStudent': '00',
'seatTypeAndNum': '',
'orderRequest.start_time_str': '00:00--24:00'
};
e.ajaxGet("order/querySingleAction.do?method=queryLeftTicket", "order/querySingleAction.do?method=init", queryLeftData, "text", function (text) {
if (text.indexOf("btn130_2") === -1) {
callback(null);
return;
}
callback(getTicketInfo(text));
});
};
return this;
};
return new fun();
})();
//#region Date 扩展
function DateDifference(count) {
/// <summary>表示一个时间差值对象</summary>
this.TicksCount = count;
this.getSeconds = function () {
/// <summary>获得差值的秒数</summary>
/// <returns type="float" />
return this.TicksCount / 1000;
};
this.getMinutes = function () {
/// <summary>获得差值的分钟数</summary>
/// <returns type="float" />
return this.getSeconds() / 60;
};
this.getHours = function () {
/// <summary>获得差值的小时数</summary>
/// <returns type="float" />
return this.getMinutes() / 60;
};
this.getDays = function () {
/// <summary>获得差值的天数</summary>
/// <returns type="float" />
return this.getHours() / 24;
};
this.getYears = function () {
/// <summary>获得差值的年数(大致)</summary>
/// <returns type="float" />
return this.getDays() / 365;
};
return this;
}
Date.prototype.isValid = function (count) {
/// <summary>判断当前的日期时间是否合法</summary>
return !isNaN(this.getFullYear());
};
Date.prototype.addDays = function (count) {
/// <summary>将日期添加上指定的天数</summary>
/// <param name="count" type="int">要添加的天数</param>
return new Date(this.getFullYear(), this.getMonth(), this.getDate() + count);
};
Date.prototype.addMonthes = function (count) {
/// <summary>将日期添加上指定的月数</summary>
/// <param name="count" type="int">要添加的月数</param>
return new Date(this.getFullYear(), this.getMonth() + count, this.getDate());
};
Date.prototype.addYears = function (count) {
/// <summary>将日期添加上指定的年数</summary>
/// <param name="count" type="int">要添加的天数</param>
return new Date(this.getFullYear() + count, this.getMonth(), this.getDate());
};
Date.prototype.subtract = function (subdate) {
/// <summary>将日期减去另一个日期,得到二者的差值</summary>
/// <returns type="DateDifference" />
if (subdate instanceof Date) {
return new DateDifference(this - subdate);
}
return null;
};
Date.prototype.format = function (format) {
/// <summary>格式化指定日期</summary>
/// <param name="format" type="String">格式化字符串</param>
format = format || "yyyy-MM-dd";
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};
Date.prototype.trimToDay = function () {
/// <summary>返回当前的日期和时间中的日期部分</summary>
return new Date(this.getFullYear(), this.getMonth(), this.getDate());
};
//#endregion