local.html 4.3 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2
<!DOCTYPE html>
<html>
H
hdx 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>本地网页</title>
    <style type="text/css">
      .btn {
        display: block;
        margin: 20px auto;
        padding: 5px;
        background-color: #007aff;
        border: 0;
        color: #ffffff;
        height: 40px;
        width: 200px;
      }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18

H
hdx 已提交
19 20 21
      .btn-red {
        background-color: #dd524d;
      }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
22

H
hdx 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
      .desc {
        padding: 10px;
        color: #999999;
      }
    </style>
  </head>
  <body>
    <p class="desc">web-view 组件加载本地 html 示例,仅在 App 环境下生效。点击下列按钮,跳转至其它页面。</p>
    <div class="btn-list">
      <button class="btn" type="button" data-action="navigateTo">navigateTo</button>
      <button class="btn" type="button" data-action="redirectTo">redirectTo</button>
      <button class="btn" type="button" data-action="navigateBack">navigateBack</button>
      <button class="btn" type="button" data-action="reLaunch">reLaunch</button>
      <button class="btn" type="button" data-action="switchTab">switchTab</button>
37
      <button class="btn" type="button" data-action="getEnv">getEnv</button>
W
wanganxp 已提交
38
      <!-- <button class="btn" type="button" data-action="getTheme">获取主题</button> -->
H
hdx 已提交
39 40 41 42 43
    </div>
    <p class="desc">网页向应用发送消息。注意:小程序端应用会在此页面后退时接收到消息。</p>
    <div class="btn-list">
      <button class="btn btn-red" type="button" id="postMessage">postMessage</button>
    </div>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44 45 46 47 48 49
    <p class="desc">文件选择</p>
    <div style="padding: 0 10px;">
      <p style="font-size: 14px;">&lt;input type="file" /&gt;</p>
      <input type="file" />
      <p style="font-size: 14px;">&lt;input type="file" accept="image/*" multiple /&gt;</p>
      <input type="file" accept="image/*" multiple />
W
wanganxp 已提交
50 51
      <p style="font-size: 14px;">普通input</p>
      <input placeholder="底部输入框"/>
52 53
      <br/>
      <a href="https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniappx.apk" download>hello uni-app x apk下载(自动化测试使用)</a>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54
    </div>
H
hdx 已提交
55 56 57
    <!-- uni 的 SDK -->
    <script type="text/javascript" src="uni.webview.1.5.5.js"></script>
    <script type="text/javascript">
雪洛's avatar
雪洛 已提交
58
      const isWeb = location.href && /^https?:/.test(location.href)
H
hdx 已提交
59 60
      // document.addEventListener('UniAppJSBridgeReady', function() {
      document.querySelector('.btn-list').addEventListener('click', function(evt) {
雪洛's avatar
雪洛 已提交
61 62 63 64
        if(isWeb) {
          alert('web端暂不支持此功能')
          return
        }
H
hdx 已提交
65 66 67
        var target = evt.target;
        if (target.tagName === 'BUTTON') {
          var action = target.getAttribute('data-action');
W
微调  
wanganxp 已提交
68
          console.log(action);
H
hdx 已提交
69 70
          switch (action) {
            case 'switchTab':
71
              uni.webView.switchTab({
72
                url: '/pages/tabBar/API'
H
hdx 已提交
73 74 75
              });
              break;
            case 'reLaunch':
76
              uni.webView.reLaunch({
H
hdx 已提交
77 78 79 80
                url: '/pages/tabBar/component'
              });
              break;
            case 'navigateBack':
81
              uni.webView.navigateBack({
H
hdx 已提交
82 83 84
                delta: 1
              });
              break;
85
            case 'getEnv':
86
              uni.webView.getEnv((res) => {
87 88 89 90 91
                uni.postMessage({
                  data: res
                })
              });
              break;
W
wanganxp 已提交
92 93 94
            /* case 'getTheme':
              alert(getThemePreference())
              break; */
H
hdx 已提交
95
            default:
96
              uni.webView[action]({
H
hdx 已提交
97 98 99 100 101 102 103 104
                url: '/pages/component/button/button'
              });
              break;
          }
        }
      })
      // });
      document.querySelector("#postMessage").addEventListener('click', function() {
雪洛's avatar
雪洛 已提交
105 106 107 108
        if(isWeb) {
          alert('web端暂不支持此功能')
          return
        }
109
        uni.webView.postMessage({
H
hdx 已提交
110 111 112
          data: {
            action: 'message'
          }
113
        })
H
hdx 已提交
114
      })
W
wanganxp 已提交
115 116 117 118 119 120 121 122 123

      /* function getThemePreference() {
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
          return 'dark';
        } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
          return 'light';
        }
        return 'unknown';
      } */
H
hdx 已提交
124 125 126
    </script>
  </body>
</html>