Light12306/Web12306/js/ui/widget_sell_notification.js

266 lines
7.8 KiB
JavaScript
Raw Normal View History

2014-08-05 20:54:33 +08:00
define(function (require, exports, module) {
var sessMgr = require("../account/sessionMgr.js");
var data = require("../data.js");
2014-08-06 20:56:47 +08:00
var cp = sessMgr.currentProfile;
var container = $("#book_sell_tip");
var utility = require("../utility.js");
var stationData = require("../station/station_data.js");
var lastCheckKey = null;
2014-08-26 21:29:58 +08:00
var mp = require("./widget_message_popup.js");
var query = require("../otn/queryticket.js");
var port = require("../platform/extensionPort.js");
2014-09-03 23:20:12 +08:00
var wxData = null;
2014-08-26 21:29:58 +08:00
require("./widget_modalDialog.js");
2014-08-06 20:56:47 +08:00
var checkTime = function () {
cp = sessMgr.currentProfile;
if (!cp) {
container.hide();
return;
}
var time = cp.depDate;
var code = cp.fromCode;
if (!time || !code) {
container.hide();
return;
}
if (lastCheckKey === time + code)
return;
container.hide();
lastCheckKey = time + code;
2014-12-01 02:17:13 +08:00
var days = data.getBeginSellDate(time);
var nowDate = utility.trimToDay(new Date());
if (days <= nowDate)
2014-08-06 20:56:47 +08:00
return; //预售期内
if (cp.studentTicket && data.isStudentTicketEnabled(time))
return; //学生票
//起售期
2014-12-01 02:17:13 +08:00
var bs = days;
2014-08-06 20:56:47 +08:00
var sellTimes = stationData.sellTime[code] || "----";
var times = container.find("time");
2014-08-27 23:05:50 +08:00
times.filter(":eq(0)").html(utility.formatDate(time, "MM月dd日"));
times.filter(":eq(1)").html(utility.formatDate(bs, "MM月dd日"));
2015-02-08 23:31:05 +08:00
times.filter(":eq(2)").html(utility.format24hTo12h(sellTimes).join(" / "));
2014-08-27 23:05:50 +08:00
container.find("span:eq(0)").html(cp.fromText + "站");
2014-08-06 20:56:47 +08:00
2014-08-26 21:29:58 +08:00
container[0].dataset.sellday = bs.getTime();
container[0].dataset.timeset = sellTimes;
2014-08-06 20:56:47 +08:00
container.show();
2014-08-30 01:32:27 +08:00
$("#result").empty();
2014-08-06 20:56:47 +08:00
};
2014-08-26 21:29:58 +08:00
var initShowAlarmUi = function () {
if (!sessMgr.currentProfile.fromCode || !sessMgr.currentProfile.toCode || !sessMgr.currentProfile.depDate) {
mp.showMessagePopup("error", "亲,还没有设置查询的条件哦。");
return;
}
var tip = new mp.MessagePopup("loading", "正在查询中,请稍等...");
tip.show();
2014-09-13 15:58:20 +08:00
query.queryTicket(sessMgr.currentProfile.fromCode, sessMgr.currentProfile.fromText, sessMgr.currentProfile.toCode, sessMgr.currentProfile.toText, utility.formatDate(data.maxDate, 'yyyy-MM-dd'), sessMgr.currentProfile.studentTicket, sessMgr.currentProfile.resign || false, true)
2014-08-26 21:29:58 +08:00
.done(function () {
tip.setState("ok", "查询成功!");
tip.close();
showAlarmSetUi(this);
}).fail(function () {
tip.setState("error", "查询失败,请重试");
2014-09-11 22:19:33 +08:00
tip.delayClose();
2014-08-26 21:29:58 +08:00
});
};
var parseSellTime = function (point) {
point = point.split('|')[0];
var args = /0?(\d+):0?(\d+)/.exec(point);
return parseInt(args[2]) * 60 * 1000 + parseInt(args[1]) * 3600 * 1000;
};
var confirmAlarm = function () {
var baseTime = new Date(parseInt(container[0].dataset.sellday));
var sellTime = parseSellTime(container[0].dataset.timeset);
var tasks = [];
var p = sessMgr.currentProfile;
var taskGroup = utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "起售提醒";
2014-08-30 01:32:27 +08:00
var taskdata = _.pick(p, "fromText", "fromCode", "toCode", "toText", "depDate");
2014-08-26 21:29:58 +08:00
//当天
tasks.push(
{
time: baseTime.getTime(),
text: "今天起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "车票 ,请留意哦!",
group: taskGroup,
2014-09-03 23:20:12 +08:00
data: taskdata,
point: 0
2014-08-26 21:29:58 +08:00
}
);
var assignTimeGroup = function (timepoint, grouptype) {
//提前三十分钟提示一次
tasks.push(
{
time: timepoint - 30 * 60 * 1000,
text: "三十分钟后起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "的" + grouptype + "车票 ,请留意哦!",
group: taskGroup,
2014-09-03 23:20:12 +08:00
data: taskdata,
point: 30,
type: grouptype
2014-08-26 21:29:58 +08:00
}
);
//提示十五分钟提示一次
tasks.push(
{
time: timepoint - 15 * 60 * 1000,
text: "十五分钟后起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "的" + grouptype + "车票 ,请留意哦!",
group: taskGroup,
2014-09-03 23:20:12 +08:00
data: taskdata,
point: 15,
type: grouptype
2014-08-26 21:29:58 +08:00
}
);
//提示五分钟提示一次
tasks.push(
{
time: timepoint - 5 * 60 * 1000,
text: "五分钟后起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "的" + grouptype + "车票 ,请留意哦!",
group: taskGroup,
2014-09-03 23:20:12 +08:00
data: taskdata,
point: 5,
type: grouptype
2014-08-26 21:29:58 +08:00
}
);
}
2014-09-02 23:15:40 +08:00
var args = [0, 0, 0];
2015-02-08 23:31:05 +08:00
wxData = {
from: cp.fromCode,
to: cp.toCode,
fromName: cp.fromText,
toName: cp.toText,
date: cp.depDate,
tasks: []
};
2014-12-01 02:17:13 +08:00
//if ($("#selltip_selection :checkbox:eq(2)").is(":checked:visible")) {
// args[0] = 1;
// //动车
// assignTimeGroup(baseTime.getTime() + 11 * 60 * 60 * 1000, "动车/城铁");
// wxData.tasks.push({ type: 3, time: '11:00' });
//}
//if ($("#selltip_selection :checkbox:eq(1)").is(":checked:visible")) {
// args[1] = 1;
// //高铁
// assignTimeGroup(baseTime.getTime() + 14 * 60 * 60 * 1000, "高铁");
// wxData.tasks.push({ type: 2, time: '14:00' });
//}
//if ($("#selltip_selection :checkbox:eq(3)").is(":checked:visible")) {
2014-12-01 21:50:08 +08:00
args[2] = 1;
//普通车次
assignTimeGroup(baseTime.getTime() + sellTime, "全部车次");
wxData.tasks.push({ type: 1, time: container[0].dataset.timeset });
2014-12-01 02:17:13 +08:00
//}
2014-08-26 21:29:58 +08:00
port.sendMessage({ action: "setAlarmTask", detail: tasks });
2014-09-02 23:15:40 +08:00
port.track(data.trackTypes.OPEN_SELL_NOTIFICATION, args);
2014-08-26 21:29:58 +08:00
return true;
};
var showAlarmSetUi = function (result) {
var hasDc = 0, hasG = 0, hasC = 0, total = 0;
2014-09-02 23:15:40 +08:00
if (!result.original.length) {
//没有车次
mp.alert("没有查到车次喔!");
return;
}
2014-08-26 21:29:58 +08:00
_.each(result.original, function (t) {
if (t.code[0] === 'D' || t.code[0] === 'C')
hasDc++;
else if (t.code[0] === 'G')
hasG++;
else hasC++;
total++;
});
var p = sessMgr.currentProfile;
var date = new Date(parseInt(container[0].dataset.sellday));
var dlg = $("#selltip_selection");
dlg.find("span:eq(0)").html(total);
dlg.find("span:eq(1)").html(hasG);
dlg.find("span:eq(2)").html(hasDc);
dlg.find("span:eq(3)").html(hasC);
dlg.find("time").html(utility.format24hTo12h(container[0].dataset.timeset));
2014-09-02 21:41:17 +08:00
dlg.find("li").show();
2014-08-26 21:29:58 +08:00
if (!hasG)
dlg.find("li:eq(1)").hide();
if (!hasDc)
dlg.find("li:eq(2)").hide();
if (!hasC)
dlg.find("li:eq(3)").hide();
dlg.find(":checkbox").prop("checked", true);
dlg.showModalDialog({
title: utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "车票 " + utility.formatDate(date, 'MM月dd日') + "起售",
buttons: [
{
text: "订阅提醒",
callback: function (e) {
if (confirmAlarm()) {
e.hide();
2014-09-03 23:20:12 +08:00
showWx();
2014-08-26 21:29:58 +08:00
}
},
type: "primary"
}
]
});
2014-09-02 23:15:40 +08:00
port.track(data.trackTypes.SHOW_NOTIFICATION_SELECT);
2014-09-03 23:20:12 +08:00
};
2015-02-08 23:31:05 +08:00
var showWx = function () {
$.post("http://12306.liebao.cn/index.php?r=Api/SendDataToKey", {
data: JSON.stringify(wxData)
}).done(function (str) {
if (str.resCode === 0) {
$("#wx_sell_notification .code span").html(str.dataKey);
$("#wx_sell_notification").showModalDialog({
title: "成功订阅提醒",
width: 'auto'
});
}
});
2014-08-26 21:29:58 +08:00
};
var init = function () {
sessMgr.on("save", function () {
2014-08-13 00:14:00 +08:00
checkTime();
});
if (cp)
checkTime();
2014-08-26 21:29:58 +08:00
container.find(">header>a").click(function () {
2014-08-13 00:14:00 +08:00
container.hide();
});
2014-08-27 23:05:50 +08:00
container.find(".act-openalarm").click(function () {
2014-12-01 02:17:13 +08:00
//initShowAlarmUi();
if (confirmAlarm()) {
showWx();
}
2014-08-26 21:29:58 +08:00
});
$("#selltip_selection :checkbox:eq(0)").change(function () {
$("#selltip_selection :checkbox:gt(0)").prop("checked", this.checked);
});
$("#selltip_selection :checkbox:gt(0)").change(function () {
$("#selltip_selection :checkbox:eq(0)").prop("checked", !$("#selltip_selection li:visible :checkbox:gt(0):not(:checked)").length);
});
2014-08-13 00:14:00 +08:00
};
init();
2014-08-06 20:56:47 +08:00
return {
check: checkTime
};
2014-08-05 20:54:33 +08:00
});