index.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view class="web-view-page">
  3. <web-view v-if="webViewUrl" :src="webViewUrl" />
  4. </view>
  5. </template>
  6. <script setup lang="ts">
  7. import { ref } from 'vue'
  8. import { onLoad } from '@dcloudio/uni-app'
  9. interface PageQuery {
  10. url?: string
  11. title?: string
  12. }
  13. const DEFAULT_TITLE = '详情'
  14. const webViewUrl = ref('')
  15. const safeDecodeURIComponent = (value: string): string => {
  16. try {
  17. return decodeURIComponent(value)
  18. } catch (error) {
  19. console.error('decodeURIComponent error:', error)
  20. return value
  21. }
  22. }
  23. const setPageTitle = (title?: string): void => {
  24. const pageTitle = title ? safeDecodeURIComponent(title) : DEFAULT_TITLE
  25. uni.setNavigationBarTitle({
  26. title: pageTitle,
  27. })
  28. }
  29. onLoad((query?: PageQuery) => {
  30. setPageTitle(query?.title)
  31. if (!query?.url) {
  32. uni.showToast({
  33. title: '链接地址不存在',
  34. icon: 'none',
  35. })
  36. uni.navigateBack({
  37. delta: 1,
  38. })
  39. return
  40. }
  41. webViewUrl.value = safeDecodeURIComponent(query.url)
  42. })
  43. </script>
  44. <style lang="scss" scoped>
  45. .web-view-page {
  46. width: 100%;
  47. height: 100vh;
  48. }
  49. </style>