Przeglądaj źródła

feat(oa): add WeChat host page to preview exported OA PDF

Co-authored-by: Cursor <cursoragent@cursor.com>
zlwu 3 miesięcy temu
rodzic
commit
fb625253f6
2 zmienionych plików z 87 dodań i 0 usunięć
  1. 8 0
      mobile-h5/pages.json
  2. 79 0
      mobile-h5/pages/oa/pdf-preview/index.vue

+ 8 - 0
mobile-h5/pages.json

@@ -64,6 +64,14 @@
         "navigationBarTextStyle": "black"
       }
     },
+    {
+      "path": "pages/oa/pdf-preview/index",
+      "style": {
+        "navigationBarTitleText": "文档预览",
+        "navigationBarBackgroundColor": "#ecfdf5",
+        "navigationBarTextStyle": "black"
+      }
+    },
     {
       "path": "pages/oa/drafts/index",
       "style": {

+ 79 - 0
mobile-h5/pages/oa/pdf-preview/index.vue

@@ -0,0 +1,79 @@
+<template>
+  <view class="pdf-preview">
+    <view class="pdf-preview__tip">{{ tip }}</view>
+  </view>
+</template>
+
+<script>
+export default {
+  data() {
+    return {
+      tip: '正在打开文档…',
+      _opened: false
+    }
+  },
+  onLoad(query) {
+    const url = decodeURIComponent(String((query && query.url) || '').trim())
+    const name = decodeURIComponent(
+      String((query && query.name) || 'approval.pdf').trim()
+    )
+    if (!url || !/^https?:\/\//i.test(url)) {
+      this.tip = '文件地址无效'
+      uni.showToast({ title: '文件地址无效', icon: 'none' })
+      return
+    }
+    this.openPdf(url, name)
+  },
+  methods: {
+    openPdf(url, name) {
+      if (this._opened) return
+      this._opened = true
+      uni.downloadFile({
+        url,
+        success: (res) => {
+          if (!res || res.statusCode !== 200 || !res.tempFilePath) {
+            this.tip = '下载失败'
+            uni.showToast({ title: '下载 PDF 失败', icon: 'none' })
+            this._opened = false
+            return
+          }
+          uni.openDocument({
+            filePath: res.tempFilePath,
+            fileType: 'pdf',
+            showMenu: true,
+            success: () => {
+              this.tip = '已打开预览'
+              setTimeout(() => {
+                uni.navigateBack({ fail: () => {} })
+              }, 300)
+            },
+            fail: () => {
+              this.tip = '无法打开文档'
+              uni.showToast({ title: '无法打开 PDF', icon: 'none' })
+              this._opened = false
+            }
+          })
+        },
+        fail: () => {
+          this.tip = '下载失败'
+          uni.showToast({ title: '下载 PDF 失败', icon: 'none' })
+          this._opened = false
+        }
+      })
+    }
+  }
+}
+</script>
+
+<style scoped>
+.pdf-preview {
+  min-height: 100vh;
+  padding: 48rpx 32rpx;
+  background: #f5f7fa;
+}
+.pdf-preview__tip {
+  text-align: center;
+  color: #64748b;
+  font-size: 28rpx;
+}
+</style>