cross.js
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* 跨域发送信息工具库
*/
var util = require('./util');
var config = require('./config');
var yasPath = config.yasPath;
var yasImgDomain = config.yasImgDomain;
var yasMobileDomain = config.yasMobileDomain;
var createCORSRequest = function(method, url) {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (typeof XDomainRequest != "undefined") {
// IE8 & IE9
xhr = new XDomainRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;
};
//发送图片方式
exports.imgSend = function(param, callback) {
var appVer = util.getAppVersion();
if (window.appBaseLogs || window.yohoInterface || appVer >= '5.2.1') {
// 从app中打开的wap页场合,走app的上报逻辑
return;
}
var image = new Image(1, 1);
image.src = yasImgDomain + yasPath + '?' + param;
image.onload = function() {
image.onload = null;
if (callback) {
callback();
}
};
};
// app中打开wap页 数据上报
exports.appSend = function(data, callback) {
if (!window.appBaseLogs) {
return;
}
var xhr = createCORSRequest();
if (!xhr) return;
xhr.ontimeout = function(e) {
console.log("timeout: ", JSON.stringify(e));
};
xhr.onerror = function(e) {
console.log("error: ", JSON.stringify(e));
};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 && callback) {
callback();
}
}
};
xhr.open('post', (document.location.protocol === 'https:' ? 'https:' : 'http:') + yasMobileDomain, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
// 下载条 上报
exports.downloadSend = function(url, data, callback) {
var xhr = createCORSRequest();
if (!xhr) return;
xhr.ontimeout = function(e) {
console.log("timeout: ", JSON.stringify(e));
};
xhr.onerror = function(e) {
console.log("error: ", JSON.stringify(e));
};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200 && callback) {
callback();
}
}
};
xhr.open('post', (document.location.protocol === 'https:' ? 'https:' : 'http:') + url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}