web-view.uvue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <script setup lang='ts'>
  2. import { ref, onMounted, onUnload } from 'vue'
  3. import Loading from '@/components/loading.uvue'
  4. import { router } from '@/.cool'
  5. const progress = ref(1)
  6. const webviewSrc = ref('')
  7. const showWebview = ref(false)
  8. var isLoading = ref(true)
  9. onMounted(() => {
  10. webviewSrc.value = router.query().src
  11. progress.value = router.query().progress
  12. showWebview.value = true
  13. })
  14. function handleMessage(e) {
  15. var pages = getCurrentPages();
  16. const prevPage = pages[pages.length - 2];
  17. (prevPage as any).status = "success"
  18. }
  19. function handleLoad() {
  20. isLoading.value = false
  21. }
  22. function onWebError() {
  23. const t = Date.now()
  24. webviewSrc.value = router.query().src + '?t=' + t
  25. console.log('onWebError')
  26. }
  27. // iOS WKWebView 在页面销毁后音频不会自动停止,需要先销毁 webview
  28. onUnload(() => {
  29. showWebview.value = false
  30. webviewSrc.value = ''
  31. })
  32. </script>
  33. <template>
  34. <Loading v-show="isLoading" />
  35. <web-view v-if="showWebview" :src="webviewSrc" id="web-view" class="w-full h-full" @message="handleMessage"
  36. @load="handleLoad" @error="onWebError"></web-view>
  37. </template>
  38. <style lang="less" scoped></style>