移动接口提交

This commit is contained in:
iFish 2014-08-21 17:00:33 +08:00
parent 5ef29d16a7
commit db350af2b5
3 changed files with 668 additions and 14 deletions

View File

@ -1,5 +1,628 @@
$().ready(function(){
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) {
/// <summary>将ArrayBuffer转换为Base64字符串</summary>
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) {
/// <summary>获得指定base64字符串的对象链接格式</summary>
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();
Login.init();
Query.init();
});

View File

@ -110,11 +110,21 @@ $(function () {
};
//set header
data.headers = {
"Fish-Referer": data.refer || null,
"Fish-User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Fish-Origin": /(https?:\/\/[^/]+\/)/i.exec(data.url)[1]
};
data.headers = data.headers || {};
if (data.headers) {
var nheader = {};
$.each(data.headers, function (k, v) {
nheader["Fish-" + k] = v;
});
data.headers = nheader;
}
if (data.refer)
data.headers["Fish-Referer"] = data.refer;
data.headers["Fish-User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
data.headers["Fish-Origin"] = /(https?:\/\/[^\/]+\/)/i.exec(data.url)[1];
if (!data.headers["Fish-Referer"])
data.headers["Fish-Referer"] = null;
$.ajax(data).done(function (result, status, xhr) {
handle.success = true;
@ -122,6 +132,7 @@ $(function () {
handle.statusText = xhr.statusText;
handle.text = xhr.responseText;
handle.model = result;
handle.headers = handle.getAllResponseHeaders();
notifyAjaxComplete(handle);
}).fail(function (xhr) {
@ -130,6 +141,7 @@ $(function () {
handle.statusText = xhr.statusText || "";
handle.text = xhr.responseText || "";
handle.model = null;
handle.headers = handle.getAllResponseHeaders();
notifyAjaxComplete(handle);
});
@ -150,18 +162,37 @@ $(function () {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", { detail: { url: null, success: false, index: ctx.index } }));
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", {
detail: {
url: null,
success: false,
index: ctx.index,
headers: xhr.getAllResponseHeaders()
}
}));
} else {
var imgUrl = base64.toObjectUrl(base64.encodeArrayBuffer(xhr.response), "image/jpeg");
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", { detail: { url: imgUrl, success: true, index: ctx.index } }));
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", {
detail: {
url: imgUrl,
success: true,
index: ctx.index,
headers: xhr.getAllResponseHeaders()
}
}));
}
}
};
xhr.responseType = "arraybuffer";
xhr.setRequestHeader("Fish-Referer", ctx.refer);
xhr.setRequestHeader("Fish-User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
xhr.setRequestHeader("Fish-Origin", /(https?:\/\/[^/]+\/)/.exec(ctx.url)[1]);
xhr.send(null);
xhr.setRequestHeader("Fish-Origin", /(https?:\/\/[^\/]+\/)/.exec(ctx.url)[1]);
if (ctx.headers) {
$.each(ctx.headers, function (k, v) {
xhr.setRequestHeader("Fish-" + k, v);
});
}
xhr.send(ctx.data || null);
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();

View File

@ -41,7 +41,7 @@ define(function (require, exports, module) {
done && done.apply(this, args);
def.resolveWith(this, args);
},
fail: function() {
fail: function () {
var args = [].slice.call(arguments);
failed && failed.apply(this, args);
def.rejectWith(this, args);
@ -105,7 +105,7 @@ define(function (require, exports, module) {
document.dispatchEvent(new CustomEvent("networkOrCertificationError"));
}
data.success ? param.done.call(data, data.model) : param.fail.call(data, data.model);
data.success ? param.done.call(data || window, data.model) : param.fail.call(data || window, data.model);
});
Object.defineProperties(this, {