web-view.uvue 7.4 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 -->
36 37 38 39 40 41 42
      <!-- #ifdef APP-IOS -->
      <view class="uni-row uni-btn-v" v-if="isProd() === false">
        <view class="uni-row uni-flex-item align-items-center">
          <text>前进、后退功能在Windows端需要打自定义基座,MAC端需要配置Xcode环境后进行真机运行或者打自定义基座</text>
        </view>
      </view>
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
43
    </view>
H
hdx 已提交
44
    <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45
  </scroll-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46 47 48
</template>

<script>
49
  // #ifdef APP
50
  import { canWebViewGoBack, canWebViewGoForward, hasNativeView} from '@/uni_modules/uts-get-native-view';
51
  // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52 53 54
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
55
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
56 57 58 59 60
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
61
        webviewContext: null as WebviewContext | null,
W
wanganxp 已提交
62
        webviewElement: null as UniWebViewElement | null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
63 64
        loadError: false,
        horizontalScrollBarAccess: true,
65
        verticalScrollBarAccess: true,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66 67
        canGoBack: false,
        canGoForward: false,
68 69
        // 自动化测试
        autoTest: false,
雪洛's avatar
雪洛 已提交
70 71
        eventLoading: null as UTSJSONObject | null,
        eventLoad: null as UTSJSONObject | null,
72 73 74 75
        eventError: null as UTSJSONObject | null,
        eventTouchstart: null as UTSJSONObject | null,
        eventTap: null as UTSJSONObject | null,
        pointerEvents: 'auto'
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
76 77 78
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
79 80
      // #ifdef APP
      // TODO web 实现createWebviewContext
W
wanganxp 已提交
81 82 83 84
      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
雪洛 已提交
85
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86 87
    },
    methods: {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      getPackageName() : string {
        const res = uni.getAppBaseInfo();
        let packageName : string = ""

        // #ifdef APP-IOS
        packageName = res.bundleId
        // #endif

        return packageName
      },
      isProd(): boolean {
        if (this.getPackageName() == 'io.dcloud.hellouniappx') {
          return true
        }
        return false
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
104 105 106 107 108 109 110 111
      back() {
        this.webviewContext?.back();
      },
      forward() {
        this.webviewContext?.forward();
      },
      reload() {
        this.webviewContext?.reload();
W
wanganxp 已提交
112
        // this.webviewElement?.reload();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
113 114 115 116 117 118 119
      },
      stop() {
        this.webviewContext?.stop();
      },
      nativeToWeb() {
        this.webviewContext?.evalJS("alert('hello uni-app x')");
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
120
      message(event : UniWebViewMessageEvent) {
121
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
122
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
123
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
124
        this.loadError = true
125 126 127 128 129 130 131 132 133 134 135 136
        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 已提交
137
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
138
      loading(event : UniWebViewLoadingEvent) {
139 140 141 142 143 144 145 146
        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 已提交
147
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
148
      load(event : UniWebViewLoadEvent) {
149
        console.log(JSON.stringify(event.detail));
150
        // #ifdef APP
151 152
        this.canGoBack = canWebViewGoBack('web-view');
        this.canGoForward = canWebViewGoForward('web-view');
153
        // #endif
154 155 156 157 158 159 160
        if (this.autoTest) {
          this.eventLoad = {
            "tagName": event.target?.tagName,
            "type": event.type,
            "src": event.detail.src
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
161
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
162
      download(event : UniWebViewDownloadEvent) {
163
        console.log(JSON.stringify(event.detail));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
164 165 166 167
        uni.showModal({
          content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
          showCancel: false
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
168
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
169
      confirm(event : UniInputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
170 171 172 173 174
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
175
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
176 177
      changeHorizontalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.horizontalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
178
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
179 180
      changeVerticalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.verticalScrollBarAccess = event.detail.value;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      },
      // 自动化测试
      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();
201 202 203
      },
      //自动化测试专用
      checkNativeWebView(): boolean {
雪洛's avatar
雪洛 已提交
204
        // #ifdef APP
205
        return hasNativeView('web-view')
雪洛's avatar
雪洛 已提交
206 207 208 209
        // #endif
        // #ifdef WEB
        return true
        // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
210 211 212
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
213 214 215
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
216 217
  .margin-left-5 {
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
218
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
219

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
220 221
  .align-items-center {
    align-items: center;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
222
  }
shutao-dc's avatar
shutao-dc 已提交
223
</style>