Light12306/Web12306/js/platform/parser.js
2015-03-13 19:25:08 +08:00

85 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

define(function (require, exports) {
var utility = require("../utility.js");
var data = require("../data.js");
exports.getError = function (json) {
/// <summary>获得指定返回数据中的错误信息</summary>
if (json.messages && json.messages instanceof Array) {
return { message: data.parseErrorMessage(json.messages.join(";")) };
}
if (json.data && json.data.isRelogin) {
return { message: "12306取消了您的登录请重新登录。", relogin: true };
}
return { message: "未知错误信息" };
};
exports.canPassageAddToOrder = function (p) {
if (p.passenger_id_type_code === "C" || p.passenger_id_type_code === "G" || p.passenger_id_type_code === "B")
return true;
if (p.passenger_id_type_code === "2")
return false;
return p.total_times == "93" || p.total_times == "95" || p.total_times == "97" || p.total_times == "99";
};
exports.getAvailableTicketType = function (p, stu) {
var a = [];
var pt = p.passenger_type;
if (pt === "3" && stu) {
a.push({ id: 3, name: "学生票" });
} else {
a.push({ id: 1, name: "成人票" });
a.push({ id: 2, name: "儿童票" });
if (pt === "3")
a.push({ id: 3, name: "学生票" });
}
if (pt === "4") {
a.push({ id: 4, name: "残军票" });
}
return a;
};
exports.processPassenger = function (list) {
list.forEach(function (e) {
e.key = e.passenger_type + "$" + e.passenger_name + "$" + e.passenger_id_type_code + "$" + e.passenger_id_no;
});
};
exports.getSeats = function (trainSeats) {
var seats = _.filter(_.clone(trainSeats), function (t) { return t.count > 0; });
//排序
seats.sort(function (x, y) {
return x.price - y.price;
});
//如果有硬座/二等座和无座,则隐藏无座
var removeNonSeat = _.findWhere(seats, { code: "1" }) || _.findWhere(seats, { code: "O" }) || false;
if (removeNonSeat) {
for (var i = 0; i < seats.length; i++) {
if (seats[i].code === "0") {
seats.splice(i, 1);
break;
}
}
} else {
//有无座的时候,则设置无座的席别代码
var emptySeatCode = data.getEmptySeatCode(trainSeats);
var emptyItem = _.findWhere(seats, { code: "0" });
if (emptyItem)
emptyItem.code = emptySeatCode;
}
return seats;
};
exports.sortCity = function (cities) {
/// <summary>对城市进行排序</summary>
cities.sort(function (x, y) {
if (x.s !== y.s)
return x.s - y.s;
return x.c > y.c ? 1 : -1;
});
};
});