linkTo.js
2.88 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
const getQueryObj = function(link) {
let loc = decodeURIComponent(document.location.href);
if (link) {
loc = decodeURIComponent(link);
}
let letiables = '';
let letiableArr = [];
let finalArr = [];
if (loc.indexOf('?') > 0) {
letiables = loc.split('?')[1];
}
if (letiables.length > 0) {
letiableArr = letiables.split('#')[0].split('&');
}
for (let i = 0; i < letiableArr.length; i++) {
let obj = {};
obj.name = letiableArr[i].split('=')[0];
obj.value = letiableArr[i].split('=')[1];
if (letiableArr[i].split('=').length > 2) {
for (let j = 2; j < letiableArr[i].split('=').length; j++) {
obj.value += '=' + letiableArr[i].split('=')[j];
}
}
finalArr.push(obj);
}
let query_obj = {};
for (let i = 0; i < finalArr.length; i++) {
query_obj[finalArr[i].name] = finalArr[i].value;
}
return query_obj;
};
const linkToMiniApp = function(goUrl, type) {
let url = goUrl || '';
if (url && url.indexOf('http') < 0 && type === 'other') {
url = document.location.protocol + '//' + document.location.host + url;
}
if (url) {
let scene;
if (type === 'product' || type === 'brand') {
scene = url;
} else {
let base_url = decodeURIComponent(url).split('?')[0];
let params = getQueryObj(url);
let paramStr = '';
Object.keys(params).forEach(function(key) {
paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key];
});
scene = '/pages/webview/webview?url=' + base_url + encodeURIComponent(paramStr);
}
window.wx.miniProgram.navigateTo({url: scene});
return false;
} else {
return true;
}
};
const createLinkButton = function(url, id) {
let 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;
};
let env = '';
const getEnv = function() {
let envFlag = window.__wxjs_environment;
if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
env = 'app';
document.addEventListener('deviceready', function() {
});
} else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag === 'miniprogram')) {
env = 'miniprogram';
} else if (location.origin === 'https://www.yohobuy.com') {
env = 'pc';
} else {
env = 'h5';
}
return env;
};
module.exports = {
getQueryObj,
linkToMiniApp,
createLinkButton,
getEnv
};