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

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
188 189
  .margin-left-5 {
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
190
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
191

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
192 193
  .align-items-center {
    align-items: center;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
194
  }
shutao-dc's avatar
shutao-dc 已提交
195
</style>