目录

Wap2App常用配置

<template>
	<web-view src="https://bjsubway.shanyexia.top"></web-view>
</template>
1
2
3

# 适配状态栏

onLoad() {
    let height = 0;
    let statusbar = 0;
    uni.getSystemInfo({
        success: (sysinfo) => {
            statusbar = sysinfo.statusBarHeight;
            height = sysinfo.windowHeight;
        }
    });
    let currentWebview = this.$scope.$getAppWebview();
    setTimeout(function() {
        var wv = currentWebview.children()[0];
        wv.setStyle({
            top: statusbar,
            height: height - statusbar,
        })
    }, 200);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 适配返回健

data() {
    return {
        lastBackTime: 0
    }
},
onBackPress() {
    let currentWebview = this.$scope.$getAppWebview();
    var wv = currentWebview.children()[0];
    if (wv) {
        // 检查是否可以返回
        wv.canBack((e) => {
            if (e.canBack) {
                // 如果可以返回,执行webview的返回
                wv.back()
            } else {
                // 如果不可以返回,检查是否是双击返回
                const now = Date.now()
                if (now - this.lastBackTime < 2000) {
                    // 如果是双击,退出应用
                    plus.runtime.quit()
                } else {
                    // 如果是第一次点击,提示再按一次退出
                    uni.showToast({
                        title: '再按一次退出应用',
                        icon: 'none',
                        duration: 2000,
                        position: 'bottom'
                    })
                    this.lastBackTime = now
                }
            }
        })
        // 总是阻止默认返回行为,因为我们自己处理返回逻辑
        return true
    }
    return false
}
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

# 强制横屏

onLoad() {
    plus.screen.lockOrientation('landscape-primary');
}
1
2
3
上次更新: 2025-04-30 00:27:16