Light12306/Web12306/js/ui/ui-autorefresh.js

269 lines
8.6 KiB
JavaScript
Raw Normal View History

2014-07-11 18:34:59 +08:00
define(function (require, exports, module) {
var sessMgr = require("../account/sessionMgr.js");
var session = sessMgr.current;
var currentProfile = sessMgr.currentProfile;
var expdata = require("../data.js");
var inAutoRefresh = false;
var media = require("../platform/media.js");
var query = require("../otn/queryticket.js");
2014-07-31 21:30:48 +08:00
var trainSuggest = require("../otn/trainstationsuggest.js");
2014-08-13 00:14:00 +08:00
var port = require("../platform/extensionPort.js");
2014-08-27 23:05:50 +08:00
var utility = require("../utility.js");
2014-07-11 18:34:59 +08:00
sessMgr.on("sessionChanged", function () {
session = sessMgr.current;
});
sessMgr.on("currentProfileChanged", function () {
currentProfile = sessMgr.currentProfile;
});
var processTrains = function (data) {
if (!currentProfile || (!session.options.showMoreOpt && !inAutoRefresh))
2014-07-11 18:34:59 +08:00
return;
//预置参数
data.enableAuto = session.options.showMoreOpt;
data.inAuto = inAutoRefresh;
2014-07-31 21:30:48 +08:00
var count = Math.max(!currentProfile.partialSubmitEnabled && currentProfile.passengers ? currentProfile.passengers.length : 0, 1);
2014-07-11 18:34:59 +08:00
var seat = null, train = null, entry = null;
if (data.original.length && !data.include.length && currentProfile.selectedTrain && currentProfile.selectedTrain.length && !currentProfile.byAuto) {
//TODO 没有查到任何车次
}
2014-07-31 21:30:48 +08:00
var trainRegCache = _.map(expdata.translateTrain(currentProfile.selectedTrain || []), function (s) { return new RegExp("^" + s + "$", "i"); });
2014-07-11 18:34:59 +08:00
if (!trainRegCache || !trainRegCache.length) {
trainRegCache = [/.*/];
}
//搜索车次
2014-07-29 21:19:06 +08:00
var st = currentProfile.selectedSeatType;
if (!st || !st.length)
st = expdata.seatDisplayOrder;
2014-08-27 23:05:50 +08:00
var selectResult = utility.selectTrain(data.available, trainRegCache, st, currentProfile.selectSeatFirst, count);
if (selectResult) {
seat = selectResult.seat;
train = selectResult.train.code;
2014-08-30 01:32:27 +08:00
entry = selectResult.train;
2014-07-11 18:34:59 +08:00
}
//查找结果
if (seat) {
//乘客
var ticketCount = _.findWhere(entry.tickets, { code: seat }).count;
var pcount = Math.min(ticketCount, currentProfile.passengers ? currentProfile.passengers.length : 0);
2014-07-11 18:34:59 +08:00
data.auto = {
train: train,
2014-09-01 20:24:57 +08:00
seat: seat === "0" ? (entry.ticketMap['1'] ? "1" : "O") : seat,
passengers: _.first(currentProfile.passengers || [], pcount),
2014-07-11 18:34:59 +08:00
data: entry
};
2014-09-01 20:24:57 +08:00
data.auto.passengers.forEach(function (p) { p.seat = data.auto.seat; });
2014-09-01 22:09:51 +08:00
if (currentProfile.submitStuAsCommon && !currentProfile.studentTicket) {
data.auto.passengers.forEach(function (p) { if (p.passenger_type == "3") p.passenger_type = "1"; });
}
2014-07-11 18:34:59 +08:00
} else {
data.auto = null;
}
2014-08-27 23:05:50 +08:00
if (data.noAction)
return;
2014-07-11 18:34:59 +08:00
//判断是否需要继续刷新
if (data.auto !== null) {
//找到车次了
data.nextTime = null;
if (inAutoRefresh) {
media.play();
media.notify("可以订票了!", "终于等到可以订的 " + data.auto.train + "次列车" + expdata.toSeatTypeName(data.auto.seat) + "");
2014-08-08 20:46:37 +08:00
//track
2014-08-14 21:33:47 +08:00
port.track(expdata.trackTypes.VALID_AUTOREFRESH);
2014-07-11 18:34:59 +08:00
}
2014-07-31 21:30:48 +08:00
if (currentProfile.autoSubmitEnabled && data.enableAuto && expdata.isAutoBookEnabled(currentProfile)) {
auto.fireEvent("performAutoSubmit", data.auto);
2014-08-08 20:46:37 +08:00
//track
2014-08-14 21:33:47 +08:00
port.track(expdata.trackTypes.SUBMIT_AUTO);
2014-07-11 18:34:59 +08:00
}
} else {
//规则:如果只有待售的,那么等到指定的时间
//如果没有且在整点附近的,那么等到整点
//否则就按常规时间
var now = new Date();
if (data.available.length && currentProfile.autoWaitToSell && data.include.length && _.every(data.include, function (x) { return x.available === -1; })) {
var next = _.min(_.map(data.include, function (x) {
return x.selltime;
}));
data.nextTime = now.getDate() === next.getDate() && next > now ? (next - now) / 1000 + 15 : currentProfile.autoRefreshDelay;
} else if (now.getMinutes() >= 59) {
data.nextTime = 61 - now.getSeconds();
} else {
data.nextTime = currentProfile.autoRefreshDelay;
}
}
};
var ev = require("../platform/EventObject.js");
var AutoRefresh = function () {
ev.apply(this);
var that = this;
var refreshDiv = $("#auto_refresh");
var refreshInfoSpan = $("section.auto-refresh-info span");
var refreshInfoP = $("section.auto-refresh-info>p");
var refreshTimer = null;
var countdownTime = null;
var refreshStartTime = null;
var refreshElapseTimer = null;
var refreshCount = 0;
2014-09-02 21:41:17 +08:00
//var currentSelectedDate;
2014-07-11 18:34:59 +08:00
var currentDateLoopIndex = -1;
2014-08-13 00:14:00 +08:00
var init = function () {
2014-07-29 21:19:06 +08:00
query.events.on("processTrains", function (e, d) {
2014-07-11 18:34:59 +08:00
processTrains(d);
2014-07-31 21:30:48 +08:00
if (inAutoRefresh)
trainSuggest.setQueryResult(d, refreshCount);
2014-07-11 18:34:59 +08:00
2014-08-27 23:05:50 +08:00
if (!d.noAction) {
if (!d.auto && (d.nextTime && inAutoRefresh)) {
countdownTime = d.nextTime;
that.start();
d.inAutoRefresh = inAutoRefresh;
} else if (d.auto) {
that.stop();
}
2014-07-11 18:34:59 +08:00
}
}).on("requestFailed", function () {
//失败?不做任何处理
});
that.on("performAutoSubmit", function (e, d) {
2014-07-11 18:34:59 +08:00
that.dispatchEvent("requireSubmitOrder", d);
});
};
this.start = function () {
if (!inAutoRefresh) {
2014-09-02 21:41:17 +08:00
//currentSelectedDate = sessMgr.currentProfile.depDate;
2014-07-11 18:34:59 +08:00
refreshCount = 0;
refreshStartTime = new Date();
inAutoRefresh = true;
refreshDiv.height();
refreshDiv.addClass("final");
refreshElapseTimer = setInterval(that.showElapsedTime, 1000);
that.showElapsedTime();
that.updateDisplay();
$("#result>table").addClass("auto-refresh");
location.hash = "#result";
2014-08-30 01:32:27 +08:00
trainSuggest.isSuggestLoopDisabled = false;
2014-07-31 21:30:48 +08:00
trainSuggest.clearQueryResultCache();
2014-08-08 20:46:37 +08:00
//track
2014-09-02 23:15:40 +08:00
var cp = sessMgr.currentProfile;
port.track(expdata.trackTypes.START_AUTOREFRESH, [cp.fromCode, cp.fromText, cp.toCode, cp.toText, cp.depDate, cp.studentTicket + '']);
2014-07-11 18:34:59 +08:00
}
inAutoRefresh = true;
if (!countdownTime)
countdownTime = currentProfile.autoRefreshDelay;
refreshTimer = setInterval(that.countdown, 100);
refreshInfoP.eq(2).hide();
refreshInfoP.eq(1).show();
that.showCountDownInfo();
};
this.stop = function () {
if (!inAutoRefresh)
return;
inAutoRefresh = false;
refreshTimer && (clearInterval(refreshTimer), refreshTimer = null);
refreshElapseTimer && (clearInterval(refreshElapseTimer), refreshElapseTimer = null);
refreshStartTime = null;
refreshDiv.removeClass("final");
$("#date_loop_editor>span.selected").removeClass("selected");
$("#result>table").removeClass("auto-refresh");
currentDateLoopIndex = -1;
2014-09-02 21:41:17 +08:00
//currentSelectedDate && (sessMgr.currentProfile.depDate = currentSelectedDate);
sessMgr.currentProfile.depDate = $("#dep_date").val();
//currentSelectedDate = null;
2014-07-31 21:30:48 +08:00
trainSuggest.clearQueryResultCache();
$("tr.train-needauto").show();
2014-08-30 01:32:27 +08:00
//隐藏监控
$("#suggestion td.train-action").hide();
2014-08-08 20:46:37 +08:00
//track
2014-08-14 21:33:47 +08:00
port.track(expdata.trackTypes.STOP_AUTOREFRESH);
2014-07-11 18:34:59 +08:00
};
this.updateDisplay = function () {
refreshInfoSpan.eq(0).html(refreshCount);
};
this.showElapsedTime = function () {
var ed = (new Date() - refreshStartTime) / 1000;
var m = Math.floor(ed / 60);
var s = Math.floor(ed % 60);
refreshInfoSpan.eq(1).html(m);
refreshInfoSpan.eq(2).html(s);
};
this.countdown = function () {
countdownTime -= 0.1;
that.showCountDownInfo();
if (countdownTime <= 0) {
clearInterval(refreshTimer);
refreshTimer = null;
refreshInfoP.eq(1).hide();
refreshInfoP.eq(2).show();
refreshCount++;
that.updateDisplay();
//已经在刷新状态了?日期轮询
2014-09-02 21:41:17 +08:00
if (sessMgr.currentProfile.dateloop && sessMgr.currentProfile.dateloop && sessMgr.currentProfile.dateloop.length) {
2014-07-11 18:34:59 +08:00
currentDateLoopIndex++;
$("#date_loop_editor>span.selected").removeClass("selected");
if (currentDateLoopIndex > sessMgr.currentProfile.dateloop.length - 1) {
2014-09-02 21:41:17 +08:00
sessMgr.currentProfile.depDate = $("#dep_date").val();
2014-07-11 18:34:59 +08:00
currentDateLoopIndex = -1;
} else {
sessMgr.currentProfile.depDate = sessMgr.currentProfile.dateloop[currentDateLoopIndex];
$("#date_loop_editor>span:eq(" + currentDateLoopIndex + ")").addClass("selected");
}
}
that.fireEvent("requestQueryTicket");
}
};
this.showCountDownInfo = function () {
2014-08-05 20:54:33 +08:00
var str = (Math.round(countdownTime * 10) / 10) + "";
if (str.indexOf(".") === -1)
str += ".0";
refreshInfoSpan.eq(3).html(str);
2014-07-11 18:34:59 +08:00
};
Object.defineProperties(this, {
inAutoRefresh: {
get: function () {
return inAutoRefresh;
}
}
});
$(document).on("click", "#btn_start_autorefresh", function () {
$(this).closest("tr").hide();
that.start();
});
$("#btn_stop_refresh").click(this.stop);
2014-08-13 00:14:00 +08:00
init();
2014-07-11 18:34:59 +08:00
return this;
};
AutoRefresh.prototype = Object.create(ev);
AutoRefresh.constructor = AutoRefresh;
var auto = new AutoRefresh();
return auto;
2014-07-11 18:34:59 +08:00
});