/** * 航线,主要用于检测、查询 * @class Flight * @constructor * @param {Object} query 查询信息:{from: '北京', to: '上海', date: [1365552000000, 1365724800000], extra: [1365552000000, 1365724800000]} */ var Flight = function(query) { this.query = query; }; Flight.prototype.getId = function() { return '__' + CityMap.name2code[this.query.from] + '-' + CityMap.name2code[this.query.to] + '|' + this.query.date[0] + '-' + this.query.date[1] + '|' + this.query.extra[0] + '-' + this.query.extra[1]; }; Flight.prototype.getUrl = function() { if (this.url === undefined) { var queryDateStart = this.query.date[0], queryDateEnd = this.query.date[1]; this.url = 'http://api.liebao.cn/flight-extension/qunar/?c=flight_tickets' + '&from=' + encodeURIComponent(this.query.from) + '&to=' + encodeURIComponent(this.query.to) + '&start=' + Util.date.format(queryDateStart) + '&end=' + Util.date.format(queryDateEnd); } return this.url; }; Flight.prototype.setData = function(data) { this.data = data; return this; }; Flight.prototype.getData = function(forceTimes, async, asyncCallback, asyncErrorHandler, finallyHandler) { var result = {extra: []}, data, response, dataTimestamp, url = this.getUrl(), others = [], i, j, lowestTemp, _theFlight = this; if (arguments[0] === undefined) { forceTimes = 0; } if (forceTimes === true) { forceTimes = 1; } if (!forceTimes && this.data !== undefined) { return this.data; } else if (!forceTimes) { forceTimes = 1; } if (arguments[1] === undefined) { async = false; } var _responseHandler = function(response) { _theFlight.simpleCheck(); if (response.price === undefined) { throw { name: 'QueryResultError', message: '没有找到此条航线' }; } result.main = response; result.lowestPrice = parseInt(response.price, 10); _theFlight.data = result; return result; }; if (async) { var _url = this.getUrl(), _asyncRequest = function(forceTimes) { $.ajax({ url: _url, async: true, error: function() { if (!--forceTimes) { asyncErrorHandler && asyncErrorHandler({ name: 'RequestError', message: '连接失败' }); finallyHandler && finallyHandler(); } else { _asyncRequest(forceTimes); } }, success: function(response) { try { var data = _responseHandler(response); asyncCallback && asyncCallback(data); } catch(e) { if (!--forceTimes) { asyncErrorHandler && asyncErrorHandler(e); } else { _asyncRequest(forceTimes); } } finally { finallyHandler && finallyHandler(); } } }); }; _asyncRequest(forceTimes); } else { while (forceTimes) { try { response = JSON.parse($.ajax({url: this.getUrl(), async: false}).responseText); return _responseHandler(response); } catch (e) { if (!--forceTimes) { throw e; } } } } }; Flight.prototype.simpleCheck = function() { if (CityMap.name2code[this.query.from] === undefined) { throw { name: 'QueryFromError', message: '对不起,不支持该出发地' }; } if (CityMap.name2code[this.query.to] === undefined) { throw { name: 'QueryToError', message: '对不起,不支持该目的地' }; } if (this.query.date[1] < Util.date.floor()) { throw { name: 'QueryDateError', message: '对不起,这张机票你已经错过了' }; } if (this.query.date[0] > this.query.date[1]) { throw { name: 'QueryDateError', message: '起始日期应小于结束日期' }; } }; var FlightFactory = { getFromKeeper: function() { var info = InputKeeper.getInfo(), query = { from: info.from, to: info.to, date: [info.dateFrom, info.dateTo], extra: [] }, start = Math.max(info.date - 172800000, Util.date.floor()), flight; if (info.dateTo - info.dateFrom < 86400000 * (config.monitor.extraLeast - 1)) { query.extra[0] = Math.max( Util.date.floor(), Util.date.floor(info.dateFrom + (info.dateTo - info.dateFrom) / 2 - 86400000 * (config.monitor.extraLeast - 1) / 2) ); query.extra[1] = query.extra[0] + 86400000 * (config.monitor.extraLeast - 1); } else { query.extra = query.date; } flight = new Flight(query); if (info.data !== undefined) { flight.setData(JSON.parse(info.data)); } return flight; }, getFromId: function(id) { var info = id.substring(2).split('|'), cityInfo = info[0].split('-'), dateInfo = info[1].split('-'), extraInfo = info[2].split('-'); return new Flight({ from: CityMap.code2name[cityInfo[0]], to: CityMap.code2name[cityInfo[1]], date: [parseInt(dateInfo[0], 10), parseInt(dateInfo[1], 10)], extra: [parseInt(extraInfo[0], 10), parseInt(extraInfo[1], 10)] }); }, getFromShort: function(shortQuery) { return new Flight({ from: CityMap.code2name[shortQuery.shortFrom], to: CityMap.code2name[shortQuery.shortTo], date: shortQuery.date, extra: shortQuery.date }); }, getFromCtrip: function(from, to, date) { var timestamp = Util.date.parse(date), extra = []; extra[0] = Math.min(timestamp - 86400000 * 2, Util.date.floor()); extra[1] = extra[0] + 86400000 * 4; return new Flight({ from: CityMap.code2name[from], to: CityMap.code2name[to], date: [timestamp, timestamp], extra: extra }); } };