var passengerTypeCode = { "1": "成人", "2": "儿童", "3": "学生", "4": "残疾军人、伤残人民警察" }; var idTypeCode = { "1": "二代身份证", "2": "一代身份证", "C": "港澳通行证", "G": "台湾通行证", "B": "护照" }; function EventObject() { var e = this; this.handles = {}; this.on = function (name, callback) { var handleQueue = e.handles[name] || []; e.handles[name] = handleQueue; handleQueue.push(callback); }; this.off = function (name, callback) { var handleQueue = e.handles[name] || []; if (callback) { for (var i = handleQueue.length - 1; i >= 0; i++) { if (handleQueue[i] == callback) handleQueue.splice(i, 1); } } else { handleQueue.length = 0; } e.handles[name] = handleQueue; }; this.fireEvent = function (name, args) { var handleQueue = e.handles[name]; if (!handleQueue) return; for (var i in handleQueue) { var handle = handleQueue[i]; if (!handle) continue; if (handle.apply(this, args) === false) break; } }; return this; } function Profile(impdata) { var __ = this; this.name = "当前模式"; this.reset = function () { __.selectedSeatType = ['O', '3', '1']; __.selectedTrain = []; __.selectSeatFirst = true; __.hideNotInListTrain = false; __.hideNoTicket = false; __.hideNotSameFrom = false; __.hideNotSameTo = false; __.autoWaitToSell = true; __.timeRangeDepFrom = 0; __.timeRangeDepTo = 23; __.timeRangeArrFrom = 0; __.timeRangeArrTo = 23; __.passengers = []; __.dateloop = []; __.partialSubmitEnabled = false; //是否允许部分提交 __.autoRefreshDelay = null; //null使用默认值 __.autoSubmitEnabled = false; //是否启用自动提交订单 __.studentTicket = false; //学生票? __.fromText = ""; //始发站名称 __.toText = ""; //到站名称 __.fromCode = ""; //始发站电报码 __.toCode = ""; //到站电报码 __.depDate = ""; //出发日期 }; __.reset(); this.save = function () { /// 请求保存配置 __.fireEvent("requireSave"); }; if (impdata) { $.extend(this, _.omit(impdata, "passengers", "selectedSeatType", "selectedTrain", "dateloop")); var extAtt = ["selectedSeatType", "selectedTrain", "dateloop"]; _.each(extAtt, function (e) { var att = e + ''; __[att] = _.clone(impdata[att]); }); if (impdata.passengers && impdata.passengers.every(function (p) { return p.times > 0; })) $.each(impdata.passengers, function () { __.passengers.push(new Passenger(this.name, this.type, this.typename, this.idtype, this.idtypeName, this.id, this.firstLetter, this.times)); }); } return this; } Profile.prototype = new EventObject(); function ProfileList(imptlist) { var __ = this; this.list = []; this.add = function (p) { if (!p || !(p instanceof Profile)) return; p.on("requireSave", function () { __.save(); }); __.list.push(p); __.fireEvent("added", [p]); __.save(); }; this.remove = function (p) { var idx = __.list.indexOf(p); if (idx == -1) return; __.list.splice(idx, 1); __.fireEvent("removed", [p]); __.save(); }; this.save = function () { __.fireEvent("requireSave"); }; this.find = function (name) { return _.find(__.list, function (e) { return e.name == name; }); }; if (imptlist) { $.each(imptlist, function () { __.list.push(new Profile(this)); }); } return this; } ProfileList.prototype = new EventObject(); function Passenger(name, type, typename, idtype, idtypename, id, firstLetter, times) { this.name = name; this.type = type; this.idtype = idtype; this.id = id; this.key = type + "$" + name + "$" + idtype + "$" + this.id; this.save = false; this.firstLetter = firstLetter; this.idtypeName = idtypename; this.typename = typename; this.times = times; var __ = this; this.toString = function () { return name; }; this.toHtml = function (removeFlag) { return ""; }; //e.passengers.push(new Passenger(this.passenger_name, this.passenger_type, this.passenger_id_type_code, this.passenger_id_no, this.first_letter)); this.toRawPassenger = function () { return { passenger_name: __.name, passenger_type: __.type, passenger_id_type_code: __.idtype, passenger_id_no: __.id, passenger_first_letter: __.firstLetter, mobile_no: "", passenger_id_type_name: __.idtypeName, passenger_type_name: __.typename }; }; Object.defineProperty(this, "canOrder", { get: function () { if (__.idtype === "C" || __.idtype === "G" || __.idtype === "B") return true; if (__.idtype === "2") return false; return __.times == "93" || __.times == "95" || __.times == "97" || __.times == "99"; } }); return this; } Passenger.prototype = new EventObject(); function LoginUser(name) { var e = this; var storagekey = "12306_user_" + name; this.username = name || ""; this.dispname = name || ""; this.passengers = []; this.savedProfile = new ProfileList(); this.currentProfile = null; this.rawPassenger = []; this.options = { showMore: false }; this.save = function () { localStorage[storagekey] = JSON.stringify(e); e.fireEvent("userSaved"); }; //加载乘客信息 if (window.localStorage[storagekey]) { var data = JSON.parse(window.localStorage[storagekey]); $.extend(e, _.omit(data, "savedProfile", "passengers", "currentProfile")); e.savedProfile = new ProfileList(data.savedProfile.list); e.currentProfile = new Profile(data.currentProfile); if (data.passengers && data.passengers.every(function (s) { return s.times > 0; })) $.each(data.passengers, function () { e.passengers.push(new Passenger(this.name, this.type, this.typename, this.idtype, this.idtypeName, this.id, this.firstLetter, this.times)); }); } //重新加载乘客 this.reloadPassengers = function (rawPassenger) { e.passengers = []; e.rawPassenger = rawPassenger || []; if (!ISOTN) e.reloadPassengersOld(); else { if (rawPassenger) { $.each(rawPassenger, function () { e.passengers.push(new Passenger(this.passenger_name, this.passenger_type, this.passenger_type_name, this.passenger_id_type_code, this.passenger_id_type_name, this.passenger_id_no, this.first_letter, this.total_times)); }); e.fireEvent("passengerLoaded"); e.save(); } else { e.reloadPassengersOtn(); } } }; this.reloadPassengersOtn = function () { otsweb.ajaxGet(message.host + "otn/confirmPassenger/getPassengerDTOs", message.host + "otn/leftTicket/init", null, "json").done(function (json) { if (!json.status) { alert("联系人加载失败,请重试。"); } else { $.each(json.data.normal_passengers, function () { e.rawPassenger.push(this); e.passengers.push(new Passenger(this.passenger_name, this.passenger_type, this.passenger_type_name, this.passenger_id_type_code, this.passenger_id_type_name, this.passenger_id_no, this.first_letter, this.total_times)); }); e.fireEvent("passengerLoaded"); e.save(); } }).fail(function () { alert("联系人加载失败,请重试。"); }); }; this.reloadPassengersOld = function () { var pageIndex = 0; var entry = function () { otsweb.ajaxGet("loginAction.do?method=initForMy12306", "loginAction.do?method=initForMy12306", null, "text", function () { setTimeout(function () { otsweb.ajaxGet("passengerAction.do?method=initUsualPassenger12306", "loginAction.do?method=initForMy12306", null, "text", function () { setTimeout(loadPage, 1000); }); }, 1000); }); }; var loadPage = function () { otsweb.ajaxPost("passengerAction.do?method=getPagePassengerAll", "passengerAction.do?method=initUsualPassenger12306", { pageSize: 10, pageIndex: pageIndex }, "json", function (json) { $.each(json.rows, function () { e.rawPassenger.push(this); e.passengers.push(new Passenger(this.passenger_name, this.passenger_type, this.passenger_type_name, this.passenger_id_type_code, this.passenger_id_type_name, this.passenger_id_no, this.first_letter, this.total_times)); }); if (e.passengers.length >= json.recordCount) { e.fireEvent("passengerLoaded"); e.save(); } else { pageIndex++; setTimeout(loadPage, 1000); } }, function (xhr) { if (xhr.status == 200) { alert("联系人加载失败,请刷新当前页面重试。"); } else { setTimeout(loadPage, 2000); } }); }; entry(); }; //绑定事件 this.savedProfile.on("requireSave", e.save); this.resetCurrentProfile = function (p) { if (p) { e.currentProfile = new Profile(p); } else { e.currentProfile = e.currentProfile || new Profile(); } e.currentProfile.on("requireSave", e.save); }; this.resetCurrentProfile(); return this; } LoginUser.prototype = new EventObject(); var USERS = (function () { function UserCollection() { var self = this; self.list = {}; this.add = function (username, dispname, password) { self.list[username] = { dispname: dispname, password: password }; self.save(); }; this.find = function (username) { return self.list[username]; }; this.remove = function (username) { if (self.list[username]) { delete self.list[username]; self.save(); } localStorage.removeItem("12306_user_" + username); }; this.save = function () { localStorage["12306_userlist"] = JSON.stringify(self.list); }; this.load = function () { var list = localStorage["12306_userlist"] || "{}"; self.list = JSON.parse(list); }; this.load(); return this; } UserCollection.prototype = new EventObject(); return new UserCollection(); })();