index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view class="document-viewer-page">
  3. <!-- 使用 web-view 全屏展示文档 -->
  4. <web-view v-if="documentUrl" :src="documentUrl"></web-view>
  5. <!-- 错误状态 -->
  6. <view v-else class="error-state">
  7. <text class="error-text">{{ errorMessage || '文档地址无效' }}</text>
  8. <button class="retry-btn" @click="handleBack">返回</button>
  9. </view>
  10. </view>
  11. </template>
  12. <script setup>
  13. import { ref, onMounted } from 'vue'
  14. // 文档信息
  15. const documentName = ref('')
  16. const documentUrl = ref('')
  17. const ossId = ref('')
  18. const errorMessage = ref('')
  19. onMounted(() => {
  20. // 获取页面参数
  21. const pages = getCurrentPages()
  22. const currentPage = pages[pages.length - 1]
  23. documentName.value = decodeURIComponent(currentPage.options.name || '文档预览')
  24. documentUrl.value = decodeURIComponent(currentPage.options.url || '')
  25. ossId.value = currentPage.options.ossId || ''
  26. // 设置导航栏标题
  27. if (documentName.value) {
  28. uni.setNavigationBarTitle({
  29. title: documentName.value
  30. })
  31. }
  32. // 如果没有URL但有ossId,可以通过ossId构建URL
  33. if (!documentUrl.value && ossId.value) {
  34. // TODO: 根据ossId构建文档访问URL
  35. // documentUrl.value = `${BASE_URL}/file/preview/${ossId.value}`
  36. }
  37. // 验证URL
  38. if (!documentUrl.value) {
  39. errorMessage.value = '文档地址无效'
  40. }
  41. })
  42. // 返回
  43. const handleBack = () => {
  44. uni.navigateBack({
  45. fail: () => {
  46. uni.reLaunch({
  47. url: '/pages/home/index'
  48. })
  49. }
  50. })
  51. }
  52. </script>
  53. <style lang="scss" scoped>
  54. .document-viewer-page {
  55. width: 100%;
  56. height: 100vh;
  57. background-color: #ffffff;
  58. web-view {
  59. width: 100%;
  60. height: 100%;
  61. }
  62. // 错误状态
  63. .error-state {
  64. width: 100%;
  65. height: 100vh;
  66. display: flex;
  67. flex-direction: column;
  68. align-items: center;
  69. justify-content: center;
  70. gap: 40rpx;
  71. padding: 60rpx;
  72. background-color: #f5f5f5;
  73. .error-text {
  74. font-size: 28rpx;
  75. color: #999999;
  76. text-align: center;
  77. }
  78. .retry-btn {
  79. width: 200rpx;
  80. height: 72rpx;
  81. line-height: 72rpx;
  82. background: #1ec9c9;
  83. color: #ffffff;
  84. border-radius: 36rpx;
  85. font-size: 28rpx;
  86. border: none;
  87. &:active {
  88. opacity: 0.8;
  89. }
  90. &::after {
  91. border: none;
  92. }
  93. }
  94. }
  95. }
  96. </style>