var base64 = base64 || (function () { var base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(''); var base64DeMap = !function () { var result = {}; $.each(base64Map, function (i, e) { result[e] = i; }); return result; }(); var target = {}; target.encode = function (data) { var buf = []; var map = base64Map; var n = data.length; //总字节数 var val; //中间值 var i = 0; /* * 3字节 ==> val ==> 4字符 */ while (i < n) { val = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]); buf.push(map[val >> 18], map[val >> 12 & 63], map[val >> 6 & 63], map[val & 63]); i += 3; } if (n % 3 == 1) //凑两个"=" buf.pop(), buf.pop(), buf.push('=', '='); else //凑一个"=" buf.pop(), buf.push('='); return buf.join(''); }; target.decode = function (str) { var buf = []; var arr = str.split(''); var map = base64DeMap; var n = arr.length; //总字符数 var val; //中间值 var i = 0; /* * 长度异常 */ if (n % 4) return null; /* * 4字符 ==> val ==> 3字节 */ while (i < n) { val = (map[arr[i]] << 18) | (map[arr[i + 1]] << 12) | (map[arr[i + 2]] << 6) | (map[arr[i + 3]]); buf.push(val >> 16, val >> 8 & 0xFF, val & 0xFF); i += 4; } /* * 凑字字符"="个数 */ while (arr[--n] == '=') buf.pop(); return buf; }; target.encodeArrayBuffer = function (arrayBuffer) { /// 将ArrayBuffer转换为Base64字符串 var dv = new DataView(arrayBuffer); var length = dv.byteLength; var data = []; for (var i = 0; i < length; i++) { data.push(dv.getUint8(i)); } return base64.encode(data); }; target.toObjectUrl = function (base64str, type) { /// 获得指定base64字符串的对象链接格式 return "data:" + type + ";base64," + base64str; }; return target; })(); var bootStrap = (function () { var def = $.Deferred(); var isAndroid = typeof (__TicketJavaScriptObject__) !== 'undefined'; var isIos = typeof (window.__ksticket) != 'undefined'; var deviceObject = __TicketJavaScriptObject__ || __ksticket; var isExtension = document.body.dataset["mobileSupportInitialized"] || false; Object.defineProperties(def, { 'isAndroid': { get: function () { return isAndLiebao; } }, 'isIos': function () { return isIos; }, 'device_info': function () { if (deviceObject) return deviceObject.get_device_info(); return null; } }); def.open_url = function (url) { if (deviceObject) { deviceObject.open_url(url); } else { window.open(url); } } def.refresh_start = function () { var args = [].slice.call(arguments); if (deviceObject) deviceObject.refresh_start.apply(this, arguments); $(document).dispatchEvent("refreshStart", args); }; def.refresh_end = function () { var args = [].slice.call(arguments); if (deviceObject) deviceObject.refresh_end.apply(this, arguments); $(document).dispatchEvent("refreshEnd", args); }; def.refresh_success = function () { var args = [].slice.call(arguments); if (deviceObject) deviceObject.refresh_success.apply(this, arguments); $(document).dispatchEvent("refreshSuccess", args); }; var queue = {}; var callid = 0; var ajaxCommon = (function () { var baseUri = "https://kyfw.12306.cn/otn/"; return { getUrl: function (url) { if (url[4] === ":" || url[5] === ":") return url; return baseUri + url; }, getHeaders: function (url, headers) { headers = headers || {}; headers['Origin'] = /(https?:\/\/[^\/]+\/)/i.exec(url)[1]; if (isIos) { var nheaders = {}; $.each(headers, function (k, v) { nheaders["Fish-" + k] = v; }); headers = nheaders; } return headers; } }; })(); var ajaxAndLb = (function () { window.fishXhrLoadCallback = function (obj) { if (typeof (obj) === "string") obj = JSON.parse(obj); var ad = queue[obj.id]; if (!ad) return; //process with json if (ad.rawResultType === "json") { try { obj.result = JSON.parse(obj.result); } catch (e) { obj.success = false; } } delete queue[obj.id]; if (obj.success) { ad.resolve(obj.result, { headers: obj.headers, statusCode: obj.statusCode, statusDescription: obj.statusDescription, id: obj.id }); } else { ad.reject(obj.result, { headers: obj.headers, statusCode: obj.statusCode, statusDescription: obj.statusDescription, id: obj.id }); } }; var ajax = function (method, url, returnType, postdata, refer, headers) { var ad = new $.Deferred(); postdata = postdata || ""; if (typeof (postdata) !== "string") postdata = $.param(postdata); headers = headers || {}; if (refer) { headers = $.extend({}, headers, { Referer: refer }); } ad.rawResultType = returnType || "json"; ad.context = { id: ++callid, url: url, method: method, postdata: postdata, refer: refer, headers: headers || {}, callback: "fishXhrLoadCallback", requestCharset: "UTF-8", returnType: ad.rawResultType === "image" ? "image" : "text" }; queue[ad.context.id] = ad; deviceObject.sendRequest(JSON.stringify(ad.context)); return ad.promise(); }; return { ajax: ajax, get: function () { var arr = [].slice.call(arguments); arr.unshift("GET"); return ajax.apply(this, arr); }, post: function () { var arr = [].slice.call(arguments); arr.unshift("POST"); return ajax.apply(this, arr); }, getImage: function (url, refer) { return ajax("GET", url, "image", null, refer); } } })(); var ajaxIos = (function () { //ios var ajax = function (method, url, returnType, postdata, refer, headers) { var ad = new $.Deferred(); headers = headers || {}; if (refer) { headers = $.extend({}, headers, { Referer: refer }); } $.ajax({ url: url, data: postdata, timeout: 120000, type: method, dataType: returnType, refer: refer, headers: headers }).done(function (result, xhr) { ad.resolve(result, { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); }).fail(function () { ad.reject(result, { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); }); return ad; }; var ajaxLoadImage = function (method, url, postdata, refer, headers) { var ad = new $.Deferred(); var xhr = new window.XMLHttpRequest(); headers = headers || {}; $.each(headers, function (k, v) { xhr.setRequestHeader("Fish-" + k, v); }); xhr.open(method, url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status !== 200) { ad.reject("加载验证码失败,请点击验证码刷新", { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); } else { ad.resolve(base64.toObjectUrl(base64.encodeArrayBuffer(xhr.response), "image/jpeg"), { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); } } }; var prefix = "Fish-"; xhr.responseType = "arraybuffer"; xhr.setRequestHeader(prefix + "Referer", refer || ""); xhr.setRequestHeader(prefix + "User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); xhr.setRequestHeader(prefix + "Origin", /(https?:\/\/[^\/]+\/)/.exec(url)[1]); xhr.send(postdata); return ad; } return { ajax: ajax, get: function () { var arr = [].slice.call(arguments); arr.unshift("GET"); return ajax.apply(this, arr); }, post: function () { var arr = [].slice.call(arguments); arr.unshift("POST"); return ajax.apply(this, arr); }, getImage: function (url, refer) { return ajaxLoadImage("GET", url, null, refer); } } }); var ajaxExtension = (function () { //ios var ajax = function (method, url, returnType, postdata, refer, headers) { var ad = new $.Deferred(); headers = headers || {}; if (refer) { headers = $.extend({}, headers, { Referer: refer }); } var xhrData = { url: url, data: postdata, timeout: 120000, type: method, dataType: returnType, refer: refer, headers: headers }; var e = new CustomEvent("ajaxproxy", { detail: { data: xhrData, index: ++callid }, cancelable: true }); if (!document.dispatchEvent(e)) { queue[e.detail.index] = { done: function (result) { ad.resolve(result, { headers: this.headers, statusCode: this.status, statusDescription: this.statusText, id: this.index }); }, fail: function () { ad.reject(this.text, { headers: this.headers, statusCode: this.status, statusDescription: this.statusText, id: this.index }); } }; } else { document.dispatchEvent(new CustomEvent("requestSupportError")); def.reject("平台错误"); } return ad; }; var ajaxLoadImage = function (method, url, postdata, refer, headers) { var ad = new $.Deferred(); headers = headers || {}; if (refer) { headers = $.extend({}, headers, { Referer: refer }); } var e = new CustomEvent("ajaxLoadVerifyCode", { detail: { method: method, url: url, refer: refer, index: ++callid, headers: headers, data: postdata }, cancelable: true }); if (!document.dispatchEvent(e)) { queue[e.detail.index] = { done: function () { ad.resolve(this.url, { headers: this.headers, statusCode: this.status, statusDescription: this.statusText, id: this.index }); }, fail: function () { ad.reject(this.text, { headers: this.headers, statusCode: this.status, statusDescription: this.statusText, id: this.index }); } }; } else { document.dispatchEvent(new CustomEvent("requestSupportError")); ad.reject("平台错误"); } return addMonthes; } document.addEventListener("ajaxproxyfinished", function (e) { var data = e.detail; if (!queue[data.index]) return; var param = queue[data.index]; delete queue[data.index]; if (data.status === 404) { //404是个比较特殊的错误,这个错误一般是证书有问题 document.dispatchEvent(new CustomEvent("networkOrCertificationError")); } data.success ? param.done.call(data || window, data.model) : param.fail.call(data || window, data.model); }); return { ajax: ajax, get: function () { var arr = [].slice.call(arguments); arr.unshift("GET"); return ajax.apply(this, arr); }, post: function () { var arr = [].slice.call(arguments); arr.unshift("POST"); return ajax.apply(this, arr); }, getImage: function (url, refer) { return ajaxLoadImage("GET", url, null, refer); } } }); var ajaxProxy = (function () { var ajax = function (method, url, returnType, postdata, refer, headers) { var ad = new $.Deferred(); headers = headers || {}; if (refer) { headers = $.extend({}, headers, { Referer: refer }); } headers["Fish-RawUrl"] = url; url = "/proxy.php"; $.ajax({ url: url, data: postdata, timeout: 120000, type: method, dataType: returnType, refer: refer, headers: headers }).done(function (result, xhr) { ad.resolve(result, { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); }).fail(function () { ad.reject(result, { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); }); return ad; }; var ajaxLoadImage = function (method, url, postdata, refer, headers) { var ad = new $.Deferred(); var xhr = new window.XMLHttpRequest(); headers = headers || {}; $.each(headers, function (k, v) { xhr.setRequestHeader("Fish-" + k, v); }); headers["Fish-RawUrl"] = url; url = "/proxy.php"; xhr.open(method, url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status !== 200) { ad.reject("加载验证码失败,请点击验证码刷新", { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); } else { ad.resolve(base64.toObjectUrl(base64.encodeArrayBuffer(xhr.response), "image/jpeg"), { headers: xhr.getAllResponseHeaders(), statusCode: xhr.statusCode, statusDescription: xhr.statusText, id: 0 }); } } }; var prefix = "Fish-"; xhr.responseType = "arraybuffer"; xhr.setRequestHeader(prefix + "Referer", refer || ""); xhr.setRequestHeader(prefix + "User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); xhr.setRequestHeader(prefix + "Origin", /(https?:\/\/[^\/]+\/)/.exec(url)[1]); xhr.send(postdata); return ad; } return { ajax: ajax, get: function () { var arr = [].slice.call(arguments); arr.unshift("GET"); return ajax.apply(this, arr); }, post: function () { var arr = [].slice.call(arguments); arr.unshift("POST"); return ajax.apply(this, arr); }, getImage: function (url, refer) { return ajaxLoadImage("GET", url, null, refer); } } }); def.getAjaxComponent = function() { return (isAndLiebao ? ajaxAndLb : (isIos ? ajaxIos : (isExtension ? ajaxExtension : ajaxProxy))); }; def.ajax = function () { var args = [].slice.call(arguments); if (args[1]) args[1] = ajaxCommon.getUrl(args[1]); if (args[4]) args[4] = ajaxCommon.getUrl(args[4]); if (args[5]) args[5] = ajaxCommon.getHeaders(args[5]); def.getAjaxComponent().ajax.apply(this, args); }; def.get = function () { var args = [].slice.call(arguments); args.unshift("GET"); def.ajax.apply(this, args); }; def.post = function () { var args = [].slice.call(arguments); args.unshift("POST"); def.ajax.apply(this, args); }; def.getImage = function () { var args = [].slice.call(arguments); if (args[1]) args[1] = ajaxCommon.getUrl(args[1]); if (args[3]) args[3] = ajaxCommon.getUrl(args[3]); if (args[5]) args[4] = ajaxCommon.getHeaders(args[4]); def.getAjaxComponent().getImage.apply(this, args); }; $(function () { if (isIos || isExtension || isAndLiebao) { def.resolve(); } else { var timer = setTimeout(function () { def.resolve(); }, 500); document.addEventListener("mobileSupportInitialized", function () { clearTimeout(timer); isExtension = true; def.resolve(); }); } }); return def; })(); $().ready(function () { Public.init(); Login.init(); Query.init(); });