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

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
41 42 43
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45 46 47 48 49
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
50
        webviewContext: null as WebviewContext | null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51 52
        loadError: false,
        horizontalScrollBarAccess: true,
53 54 55
        verticalScrollBarAccess: true,
        // 自动化测试
        autoTest: false,
雪洛's avatar
雪洛 已提交
56 57 58
        eventLoading: null as UTSJSONObject | null,
        eventLoad: null as UTSJSONObject | null,
        eventError: null as UTSJSONObject | null
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
59 60 61
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
62 63
      // #ifdef APP
      // TODO web 实现createWebviewContext
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64
      this.webviewContext = uni.createWebviewContext('web-view', this)
雪洛's avatar
雪洛 已提交
65
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    },
    methods: {
      back() {
        this.webviewContext?.back();
      },
      forward() {
        this.webviewContext?.forward();
      },
      reload() {
        this.webviewContext?.reload();
      },
      stop() {
        this.webviewContext?.stop();
      },
      nativeToWeb() {
        this.webviewContext?.evalJS("alert('hello uni-app x')");
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83
      message(event : UniWebViewMessageEvent) {
84
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
85
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
87
        this.loadError = true
88 89 90 91 92 93 94 95 96 97 98 99
        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 已提交
100
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
101
      loading(event : UniWebViewLoadingEvent) {
102 103 104 105 106 107 108 109
        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 已提交
110
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
111
      load(event : UniWebViewLoadEvent) {
112 113 114 115 116 117 118 119
        console.log(JSON.stringify(event.detail));
        if (this.autoTest) {
          this.eventLoad = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "src": event.detail.src
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
120
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
121
      download(event : UniWebViewDownloadEvent) {
122
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
123 124 125 126
        uni.showModal({
          content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
          showCancel: false
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
127
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
128
      confirm(event : UniInputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
129 130 131 132 133
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
134
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
135 136
      changeHorizontalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.horizontalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
137
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
138 139
      changeVerticalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.verticalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
140 141 142
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
143 144 145
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
146 147
  .margin-left-5 {
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
148
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
149

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
150 151
  .align-items-center {
    align-items: center;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
152
  }
shutao-dc's avatar
shutao-dc 已提交
153
</style>