open-miniapp.js
1.32 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
import {transToMiniappPath} from './miniapp-path-rules';
const jumpAction = ['go.productDetail', 'go.list', 'go.shop', 'go.poollist'];
let retry = 6;
function goMiniapp(apppath) {
retry--;
if (!window.wx) {
if (retry > 0) {
setTimeout(function() {
goMiniapp(apppath);
}, 300);
}
return;
}
let transData = transToMiniappPath(apppath);
if (transData.path && jumpAction.indexOf(transData.action) >= 0) {
window.wx.miniProgram.redirectTo({url: transData.path});
}
}
function init() {
if (window.__wxjs_environment !== 'miniprogram') {
return;
}
// 添加小程序webview页面标识
const bodyClass = 'miniapp-body';
const $body = document.getElementsByTagName('body')[0];
let classNames = ($body.className || '').split(' ');
if (classNames.indexOf(bodyClass) < 0) {
classNames.push(bodyClass);
}
$body.className = classNames.join(' ');
// 自动跳转至小程序对应页面
const appInfo = document.getElementById('main-wrap').dataset;
if (appInfo.miniautojump && appInfo.apppath) {
goMiniapp(appInfo.apppath);
}
}
if (!window.WeixinJSBridge || !window.WeixinJSBridge.invoke) {
document.addEventListener('WeixinJSBridgeReady', init, false);
} else {
init();
}