utils.js
4.92 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import wx from 'weixin-js-sdk';
const linkToMiniApp = (event) => {
let element = event.currentTarget || event.target.parentNode;
let url = element.dataset.url || '';
let type = element.dataset.type || 'h5';
if (url && url.indexOf('http') < 0 && type === 'h5') {
url = document.location.protocol + '//' + document.location.host + url;
}
console.log('jumpToUrl=' + url);
if (url) {
let base_url = decodeURIComponent(url).split('?')[0];
let params = getQueryObj(url);
let paramStr = '';
Object.keys(params).forEach(key => {
paramStr += paramStr === '' ? '?' + key + '=' + params[key] : '&' + key + '=' + params[key];
})
let mini_url = '/pages/webview/webview?url=' + base_url;
let scene = mini_url + encodeURIComponent(paramStr);
if (type === 'miniApp') {
mini_url = url;
scene = mini_url;
}
console.log('newUrl:', mini_url, scene);
wx.miniProgram.navigateTo({url: scene});
return false;
} else {
return true;
}
};
const getQueryObj = (link) => {
let loc = decodeURIComponent(document.location.href);
if (link) {
loc = decodeURIComponent(link);
}
console.log('loc:', loc);
let variables = '';
let variableArr = [];
let finalArr = [];
if (loc.indexOf('?') > 0) {
variables = loc.split('?')[1];
}
if (variables.length > 0) {
variableArr = variables.split('#')[0].split('&');
}
for (let i = 0; i < variableArr.length; i++) {
let obj = {};
obj.name = variableArr[i].split('=')[0];
obj.value = variableArr[i].split('=')[1];
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;
};
let emptyFn = function() {};
const invokeMethod = (obj) => {
let appInterface = window.yohoInterface;
if (appInterface) {
appInterface.triggerEvent(obj.success || emptyFn, obj.fail || emptyFn, {
method: obj.method,
arguments: obj.args
});
} else {
console.log('暂不支持,请在YOHO!BUY应用中打开');
}
}
const getEnv = () =>{
return new Promise((resolve)=>{
wx.miniProgram.getEnv(function (res,err){
if(err){
resolve(err)
}
return resolve(res.miniprogram);
})
})
}
const parseUrl= (url)=> {
let query = {},
hashs,
hash,
i;
url = (url || '').split('?');
hashs = (url[1] || '').split('&');
if (hashs && hashs.length) {
for (i = 0; i < hashs.length; i++) {
hash = hashs[i].split('=');
query[hash[0]] = hash[1];
}
}
return {
path: url[0],
query: query
};
}
const image = (url, width, height, mode) => {
mode = !isNaN(mode) ? mode : 2;
url = url || '';
url = url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
if (url.indexOf('imageView2') > 0) {
url += '/q/70';
}
return url.replace('http:', '');
};
const image2 = (imageUrl, opts) => {
if (imageUrl) {
let params = opts.hash;
let urls = imageUrl.split('?');
let query = urls[1] || '';
let uri = urls[0];
if (uri.indexOf('http:') === 0) {
uri = uri.replace('http:', '');
}
if (query) {
query = query.replace(/{width}/g, params.w)
.replace(/{height}/g, params.h)
.replace(/{mode}/g, (params.mode || 2));
if (query.indexOf('imageView2') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else {
query += '/q/' + params.q;
}
} else if (query.indexOf('imageMogr2') === 0) {
if (params.q && query.indexOf('/quality/') > 0) {
query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
} else {
query += '/quality/' + params.q;
}
} else if (query.indexOf('imageView/') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else {
query += '/q/' + params.q;
}
if (params.mode) {
query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
}
}
} else {
query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
}
return uri + '?' + query;
} else {
return imageUrl;
}
};
export {
image,
image2,
getEnv,
invokeMethod,
getQueryObj,
linkToMiniApp,
parseUrl
};