common.js
3.67 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var invokeMethod = function(obj) {
if (window.yohoInterface) {
window.yohoInterface.triggerEvent(obj.success || function () {
}, obj.fail || function () {
}, {
method: obj.method,
arguments: obj.args
});
} else {
obj.fail && obj.fail();
}
};
var image = function(url, width, height, mode, quality) {
mode = !isNaN(Number(mode)) ? mode : 2;
url = url || '';
url = url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
if (url.indexOf('imageView2') > 0) {
quality = quality || 90;
url += '/q/' + quality;
}
return url.replace('quality/80', 'quality/60').replace('http:', '');
};
var linkToMiniApp = function(goUrl, type) {
var url = goUrl || '';
if (url && url.indexOf('http') < 0 && type === 'other') {
url = document.location.protocol + '//' + document.location.host + url;
}
if (url) {
var scene;
if (type === 'product' || type === 'brand') {
scene = url;
} else {
var base_url = decodeURIComponent(url).split('?')[0];
var params = getQueryObj(url);
var paramStr = '';
Object.keys(params).forEach(function(key) {
paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key];
})
scene = '/pages/webview/webview?url=' + base_url + encodeURIComponent(paramStr);
}
wx.miniProgram.navigateTo({url: scene});
return false;
} else {
return true;
}
};
var getQueryObj = function(link) {
var loc = decodeURIComponent(document.location.href);
if (link) {
loc = decodeURIComponent(link);
}
var variables = '';
var variableArr = [];
var finalArr = [];
if (loc.indexOf('?') > 0) {
variables = loc.split('?')[1];
}
if (variables.length > 0) {
variableArr = variables.split('#')[0].split('&');
}
for (var i = 0; i < variableArr.length; i++) {
var obj = {};
obj.name = variableArr[i].split('=')[0];
obj.value = variableArr[i].split('=')[1];
if (variableArr[i].split('=').length > 2) {
for (var j = 2; j < variableArr[i].split('=').length; j++) {
obj.value += '=' + variableArr[i].split('=')[j];
}
}
finalArr.push(obj);
}
var query_obj = {};
for (var i = 0; i < finalArr.length; i++) {
query_obj[finalArr[i].name] = finalArr[i].value;
}
return query_obj;
};
var createLinkButton = function(url, id) {
var a = document.createElement('a');
a.style.position = 'fixed';
a.style.top = 0;
a.style.left = 0;
a.style.border = 'none';
a.style.outline = 'none';
a.style.resize = 'none';
a.style.background = 'transparent';
a.style.color = 'transparent';
a.setAttribute('id', id);
a.setAttribute('href', url);
document.body.appendChild(a);
return a;
};
var isMobile = {
Android() {
return navigator.userAgent.match(/Android/i) ? true : false;
},
BlackBerry() {
return navigator.userAgent.match(/BlackBerry/i) ? true : false;
},
iOS() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
},
Windows() {
return navigator.userAgent.match(/IEMobile/i) ? true : false;
},
any() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};
module.exports = {
invokeMethod: invokeMethod,
getQueryObj: getQueryObj,
linkToMiniApp: linkToMiniApp,
createLinkButton: createLinkButton,
image:image,
isMobile:isMobile
};