define(function (require, exports, module) { var sessMgr = require("../account/sessionMgr.js"); var data = require("../data.js"); var cp = sessMgr.currentProfile; var container = $("#book_sell_tip"); var utility = require("../utility.js"); var stationData = require("../station/station_data.js"); var lastCheckKey = null; var mp = require("./widget_message_popup.js"); var query = require("../otn/queryticket.js"); var port = require("../platform/extensionPort.js"); var wxData = null; require("./widget_modalDialog.js"); 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; var days = utility.getDaysDifference(new Date(), time); if (days <= data.maxSellDays) return; //预售期内 if (cp.studentTicket && data.isStudentTicketEnabled(time)) return; //学生票 //起售期 var bs = utility.addDays(time, -data.maxSellDays); var sellTimes = stationData.sellTime[code] || "----"; var times = container.find("time"); times.filter(":eq(0)").html(utility.formatDate(time, "MM月dd日")); times.filter(":eq(1)").html(utility.formatDate(bs, "MM月dd日")); times.filter(":eq(2)").html(utility.format24hTo12h(sellTimes).join(" / ")); container.find("span:eq(0)").html(cp.fromText + "站"); container[0].dataset.sellday = bs.getTime(); container[0].dataset.timeset = sellTimes; container.show(); $("#result").empty(); }; 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(); 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) .done(function () { tip.setState("ok", "查询成功!"); tip.close(); showAlarmSetUi(this); }).fail(function () { tip.setState("error", "查询失败,请重试"); tip.delayClose(); }); }; 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 + "起售提醒"; var taskdata = _.pick(p, "fromText", "fromCode", "toCode", "toText", "depDate"); //当天 tasks.push( { time: baseTime.getTime(), text: "今天起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "车票 ,请留意哦!", group: taskGroup, data: taskdata, point: 0 } ); 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, data: taskdata, point: 30, type: grouptype } ); //提示十五分钟提示一次 tasks.push( { time: timepoint - 15 * 60 * 1000, text: "十五分钟后起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "的" + grouptype + "车票 ,请留意哦!", group: taskGroup, data: taskdata, point: 15, type: grouptype } ); //提示五分钟提示一次 tasks.push( { time: timepoint - 5 * 60 * 1000, text: "五分钟后起售" + utility.formatDate(p.depDate, 'MM月dd日') + p.fromText + "到" + p.toText + "的" + grouptype + "车票 ,请留意哦!", group: taskGroup, data: taskdata, point: 5, type: grouptype } ); } var args = [0, 0, 0]; wxData = { from: cp.fromCode, to: cp.toCode, fromName: cp.fromText, toName: cp.toText, date: cp.depDate, tasks: [] }; 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")) { args[2] = 1; //普通车次 assignTimeGroup(baseTime.getTime() + sellTime, "普通车次"); wxData.tasks.push({ type: 1, time: container[0].dataset.timeset }); } port.sendMessage({ action: "setAlarmTask", detail: tasks }); port.track(data.trackTypes.OPEN_SELL_NOTIFICATION, args); return true; }; var showAlarmSetUi = function (result) { var hasDc = 0, hasG = 0, hasC = 0, total = 0; if (!result.original.length) { //没有车次 mp.alert("没有查到车次喔!"); return; } _.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)); dlg.find("li").show(); 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(); showWx(); } }, type: "primary" } ] }); port.track(data.trackTypes.SHOW_NOTIFICATION_SELECT); }; 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' }); } }); }; var init = function () { sessMgr.on("save", function () { checkTime(); }); if (cp) checkTime(); container.find(">header>a").click(function () { container.hide(); }); container.find(".act-openalarm").click(function () { initShowAlarmUi(); }); $("#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); }); }; init(); return { check: checkTime }; });