web-view.uvue 6.5 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2
  <scroll-view class="uni-flex-item">
3 4 5 6
    <web-view id="web-view" class="uni-flex-item" :style="{ 'pointer-events': pointerEvents }" :src="src"
      :webview-styles="webview_styles" :horizontalScrollBarAccess="horizontalScrollBarAccess"
      :verticalScrollBarAccess="verticalScrollBarAccess" @message="message" @error="error" @loading="loading"
      @load="load" @download="download" @touchstart="touchstart" @tap="tap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
7
    </web-view>
H
hdx 已提交
8
    <!-- #ifdef APP -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9
    <view class="uni-padding-wrap uni-common-mt">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
10
      <view class="uni-btn-v">
11
        <input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" maxlength="-1"/>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
12 13
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
14 15
        <button class="uni-flex-item" type="primary" :disabled="!canGoBack" @click="back">后退</button>
        <button class="margin-left-5 uni-flex-item" type="primary" :disabled="!canGoForward" @click="forward">前进</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
16 17
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18
        <button class="uni-flex-item" type="primary" @click="reload">重新加载</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
19
        <button class="margin-left-5 uni-flex-item" type="primary" @click="stop">停止加载</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
20 21 22 23
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="nativeToWeb">原生和Web通信</button>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24
      <!-- #ifdef APP-ANDROID -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
25 26 27
      <view class="uni-row uni-btn-v">
        <view class="uni-row uni-flex-item align-items-center">
          <text>显示横向滚动条</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28
          <switch :checked="true" @change="changeHorizontalScrollBarAccess"></switch>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
29 30 31
        </view>
        <view class="uni-row uni-flex-item align-items-center">
          <text>显示竖向滚动条</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32
          <switch :checked="true" @change="changeVerticalScrollBarAccess"></switch>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33
        </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
34 35
      </view>
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
36
    </view>
H
hdx 已提交
37
    <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38
  </scroll-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39 40 41
</template>

<script>
42
  import { canWebViewGoBack, canWebViewGoForward, hasNativeView} from '@/uni_modules/uts-get-native-view';
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
43 44 45
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
47 48 49 50 51
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
52
        webviewContext: null as WebviewContext | null,
W
wanganxp 已提交
53
        webviewElement: null as UniWebViewElement | null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54 55
        loadError: false,
        horizontalScrollBarAccess: true,
56
        verticalScrollBarAccess: true,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
57 58
        canGoBack: false,
        canGoForward: false,
59 60
        // 自动化测试
        autoTest: false,
雪洛's avatar
雪洛 已提交
61 62
        eventLoading: null as UTSJSONObject | null,
        eventLoad: null as UTSJSONObject | null,
63 64 65 66
        eventError: null as UTSJSONObject | null,
        eventTouchstart: null as UTSJSONObject | null,
        eventTap: null as UTSJSONObject | null,
        pointerEvents: 'auto'
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
67 68 69
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
70 71
      // #ifdef APP
      // TODO web 实现createWebviewContext
W
wanganxp 已提交
72 73 74 75
      this.webviewContext = uni.createWebviewContext('web-view',this)
      this.webviewElement = uni.getElementById('web-view') as UniWebViewElement //推荐使用element,功能更丰富
      // console.log('url: ',this.webviewElement?.getAttribute("src"));
      // this.webviewElement?.setAttribute("src","https://ext.dcloud.net.cn/")
雪洛's avatar
雪洛 已提交
76
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
77 78 79 80 81 82 83 84 85 86
    },
    methods: {
      back() {
        this.webviewContext?.back();
      },
      forward() {
        this.webviewContext?.forward();
      },
      reload() {
        this.webviewContext?.reload();
W
wanganxp 已提交
87
        // this.webviewElement?.reload();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
88 89 90 91 92 93 94
      },
      stop() {
        this.webviewContext?.stop();
      },
      nativeToWeb() {
        this.webviewContext?.evalJS("alert('hello uni-app x')");
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
95
      message(event : UniWebViewMessageEvent) {
96
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
97
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
99
        this.loadError = true
100 101 102 103 104 105 106 107 108 109 110 111
        console.log(JSON.stringify(event.detail));
        if (this.autoTest) {
          this.eventError = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "errCode": event.detail.errCode,
            "errMsg": event.detail.errMsg,
            "url": event.detail.url,
            "fullUrl": event.detail.fullUrl,
            "src": event.detail.src
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
112
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
113
      loading(event : UniWebViewLoadingEvent) {
114 115 116 117 118 119 120 121
        console.log(JSON.stringify(event.detail));
        if (this.autoTest) {
          this.eventLoading = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "src": event.detail.src
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
122
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
123
      load(event : UniWebViewLoadEvent) {
124
        console.log(JSON.stringify(event.detail));
125 126
        this.canGoBack = canWebViewGoBack('web-view');
        this.canGoForward = canWebViewGoForward('web-view');
127 128 129 130 131 132 133
        if (this.autoTest) {
          this.eventLoad = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "src": event.detail.src
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
134
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
135
      download(event : UniWebViewDownloadEvent) {
136
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
137 138 139 140
        uni.showModal({
          content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
          showCancel: false
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
141
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
142
      confirm(event : UniInputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
143 144 145 146 147
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
148
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
149 150
      changeHorizontalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.horizontalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
151
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
152 153
      changeVerticalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.verticalScrollBarAccess = event.detail.value;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      },
      // 自动化测试
      touchstart(event : UniTouchEvent) {
        if (this.autoTest) {
          this.eventTouchstart = {
            clientX: Math.ceil(event.touches[0].clientX),
            clientY: Math.ceil(event.touches[0].clientY)
          };
        }
      },
      tap(event : UniPointerEvent) {
        if (this.autoTest) {
          this.eventTap = {
            clientX: Math.ceil(event.clientX),
            clientY: Math.ceil(event.clientY)
          };
        }
      },
      getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
174 175 176 177
      },
      //自动化测试专用
      checkNativeWebView(): boolean {
        return hasNativeView('web-view')
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
178 179 180
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
181 182 183
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
184 185
  .margin-left-5 {
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
186
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
187

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
188 189
  .align-items-center {
    align-items: center;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
190
  }
shutao-dc's avatar
shutao-dc 已提交
191
</style>