$.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 () {
/// 触发原生的点击
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) {
/// 表示一个时间差值对象
this.TicksCount = count;
this.getSeconds = function () {
/// 获得差值的秒数
///
return this.TicksCount / 1000;
};
this.getMinutes = function () {
/// 获得差值的分钟数
///
return this.getSeconds() / 60;
};
this.getHours = function () {
/// 获得差值的小时数
///
return this.getMinutes() / 60;
};
this.getDays = function () {
/// 获得差值的天数
///
return this.getHours() / 24;
};
this.getYears = function () {
/// 获得差值的年数(大致)
///
return this.getDays() / 365;
};
return this;
}
Date.prototype.isValid = function (count) {
/// 判断当前的日期时间是否合法
return !isNaN(this.getFullYear());
};
Date.prototype.addDays = function (count) {
/// 将日期添加上指定的天数
/// 要添加的天数
return new Date(this.getFullYear(), this.getMonth(), this.getDate() + count);
};
Date.prototype.addMonthes = function (count) {
/// 将日期添加上指定的月数
/// 要添加的月数
return new Date(this.getFullYear(), this.getMonth() + count, this.getDate());
};
Date.prototype.addYears = function (count) {
/// 将日期添加上指定的年数
/// 要添加的天数
return new Date(this.getFullYear() + count, this.getMonth(), this.getDate());
};
Date.prototype.subtract = function (subdate) {
/// 将日期减去另一个日期,得到二者的差值
///
if (subdate instanceof Date) {
return new DateDifference(this - subdate);
}
return null;
};
Date.prototype.format = function (format) {
/// 格式化指定日期
/// 格式化字符串
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 () {
/// 返回当前的日期和时间中的日期部分
return new Date(this.getFullYear(), this.getMonth(), this.getDate());
};
//#endregion