web-view-local.uvue 2.4 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
2 3
  <web-view id="web-view" ref="web-view" class="web-view" src="/hybrid/html/local.html" @message="message" @error="error"
    @loading="loading" @load="load" @download="download">
H
hdx 已提交
4
  </web-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
5 6 7
</template>

<script>
8 9 10
  export default {
    data() {
      return {
11
        loadError: false,
12 13 14
        loadFinish: false,
        // 自动化测试
        autoTest: false,
雪洛's avatar
雪洛 已提交
15 16
        eventMessage: null as UTSJSONObject | null,
        eventDownload: null as UTSJSONObject | null
17 18 19
      }
    },
    methods: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
20
      message(event : UniWebViewMessageEvent) {
21
        console.log(JSON.stringify(event.detail));
lizhongyi_'s avatar
lizhongyi_ 已提交
22
        let contentStr = JSON.stringify(event.detail);
雪洛's avatar
雪洛 已提交
23 24 25
        // #ifdef APP-IOS
        contentStr = JSON.stringify(event.detail.data[0]);
        // #endif
W
微调  
wanganxp 已提交
26
        uni.showModal({
lizhongyi_'s avatar
lizhongyi_ 已提交
27
          content: contentStr,
W
微调  
wanganxp 已提交
28 29
          showCancel: false
        });
30 31 32 33 34 35 36
        if (this.autoTest) {
          this.eventMessage = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "data": event.detail.data
          };
        }
37
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
39
        this.loadError = true
40
        console.log(JSON.stringify(event.detail));
41
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42
      loading(event : UniWebViewLoadingEvent) {
43
        console.log(JSON.stringify(event.detail));
44
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45
      load(event : UniWebViewLoadEvent) {
46
        this.loadFinish = true;
47 48 49 50 51 52 53 54 55 56 57 58 59
        console.log(JSON.stringify(event.detail));
      },
      download(event : UniWebViewDownloadEvent) {
        console.log(JSON.stringify(event.detail));
        if (this.autoTest) {
          const arr = event.detail.userAgent.split(' ');
          this.eventDownload = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "url": event.detail.url,
            "userAgent": arr[arr.length - 1],
            "contentDisposition": event.detail.contentDisposition,
            "mimetype": event.detail.mimetype,
60
            "isContentLengthValid": (event.detail.contentLength / 1024 / 1024).toInt() > 1
61 62 63 64 65 66 67 68 69
          };
        }
      },
      // 自动化测试
      testEventDownload() {
        uni.createWebviewContext('web-view')?.evalJS("document.getElementsByTagName('a')[0].click()");
      },
      testEventMessage() {
        uni.createWebviewContext('web-view')?.evalJS("document.getElementById('postMessage').click()");
70 71 72
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
73 74 75
</script>

<style>
H
hdx 已提交
76 77 78
.web-view {
  flex: 1;
}
W
微调  
wanganxp 已提交
79
</style>