pdfPreView.vue 5.3 KB
<template>
  <view class="material-detail">
    <view class="nav-bar">
      <view class="back" @click.stop="back">
        <uni-icons type="left" :size="20" color="#000"></uni-icons>
      </view>
      <view class="nav-title">{{ title }}</view>
      <!-- 从报告管理跳转时,右上角显示保存按钮 -->
      <view v-if="showSaveBtn && reportImageUrl" class="nav-save-btn" @click.stop="saveReportImage">保存图片</view>
    </view>
    <!-- html 报告:统一用 web-view 展示内容,按钮放在导航栏,避免被覆盖 -->
    <web-view v-if="fileUrltype === 'html'" :src="previewUrl"></web-view>
    <view v-else-if="fileUrltype !== 'pdf'">
      <lk-preview-office :url="previewUrl" :fileType="fileType"></lk-preview-office>
    </view>
    <view v-else>
      <lk-preview-office :url="previewUrl" fileType="pdf"></lk-preview-office>
    </view>
  </view>
</template>

<script type="text/ecmascript-6">
import { report } from '@/mixin/index.js'
export default {
  name: '',
  mixins: [report],
  data () {
    return {
      previewUrl: '',
      title: '',
      fileType: 'pdf',
      Signature: true,
      fileUrltype:'pdf',
      showSaveBtn: false,
      reportImageUrl: ''
    }
  },
  onLoad (options) {
    console.log(options)
    this.fileUrltype = options.fileUrltype || 'pdf'
    this.fileType = options.fileType || 'pdf'
    this.Signature = options.Signature === 'false' ? false : (options.Signature || true);
    if(this.fileType === 'docx') {
      this.previewUrl = options.fileUrl
    }else if(!this.Signature) {
      this.previewUrl = options.fileUrl
    }else {
      this.previewUrl = options.fileUrl + '&OSSAccessKeyId='+ options.OSSAccessKeyId + '&Signature=' + options.Signature
    }

    this.title = options.fileName
    // 来自报告管理的额外参数:是否显示保存按钮 & 报告图片地址
    this.showSaveBtn = options.showSaveBtn === '1'
    this.reportImageUrl = options.reportImageUrl ? decodeURIComponent(options.reportImageUrl) : ''
    console.log(this.previewUrl)
    // else this.setWv()
  },
  mounted () {
  },
  methods: {
    back () {
      uni.navigateBack({
        delta: 1,
        animationType: 'pop-out',
        animationDuration: 100
      })
    },
    // 保存报告图片到相册
    saveReportImage () {
      if (!this.reportImageUrl) {
        uni.showToast({ title: '暂无可保存的图片', icon: 'none' })
        return
      }
      uni.showLoading({ title: '保存中...', mask: true })
      uni.downloadFile({
        url: this.reportImageUrl,
        success: res => {
          if (res.statusCode === 200) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: () => {
                uni.showToast({ title: '已保存到相册', icon: 'none' })
              },
              fail: () => {
                uni.showToast({ title: '保存失败', icon: 'none' })
              },
              complete: () => {
                uni.hideLoading()
              }
            })
          } else {
            uni.hideLoading()
            uni.showToast({ title: '下载失败', icon: 'none' })
          }
        },
        fail: () => {
          uni.hideLoading()
          uni.showToast({ title: '下载失败', icon: 'none' })
        }
      })
    },
    setWebviewStyle () {
      const setWebviewStyleWithRetry = () => {
        const currentWebview = this.$scope.$getAppWebview()
        let [wv] = currentWebview.children(),
          wvFlag = false
        if (wv) {
          wv.setStyle({
            left: '71.9%',
            top: '17.18%',
            height: '82.82%',
            width: '28.3%'
          })
          wvFlag = true;
        }
        return wvFlag
      }
      const retrySetWebviewStyle = () => {
        if (!setWebviewStyleWithRetry()) {
          // 如果未成功,继续重试
          setTimeout(retrySetWebviewStyle, 50);
        } else {
          // 成功设置样式,结束循环
          console.log('Webview样式设置成功');
        }
      };

      setTimeout(retrySetWebviewStyle, 100);
    }
  }
}
</script>

<style scoped lang="scss">
.nav-bar {
  position: fixed;
  width: 100%;
  height: toRpx(96);
  left: 0;
  top: toRpx(48);
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: toRpx(32);
  color: #1a1a1a;
  .back {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    aspect-ratio: 1/1;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.nav-title {
  max-width: 60%;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-save-btn {
  position: absolute;
  right: toRpx(24);
  top: 50%;
  transform: translateY(-50%);
  padding: 0 toRpx(24);
  height: toRpx(64);
  line-height: toRpx(64);
  border-radius: toRpx(32);
  background: linear-gradient(0deg, #2c66f7 0%, #48a6ff 100%);
  color: #ffffff;
  font-size: toRpx(24);
}
.file-box {
  position: fixed;
  top: 5.2vh;
  height: 94.8vh;
  left: 0;
  width: 100vw;
}
.has-ai {
  width: 71.36vw;
}
.AI-window {
  width: 28.3vw;
  height: 90.63vh;
  position: fixed;
  top: 9.37vh;
  right: 0;
  background: #f5f9ff;
  .AI-title {
    height: 5.73vh;
    padding-top: 2.08vh;
    font-size: toRpx(34);
    font-weight: 500;
    justify-content: center;
    color: #333333;
  }
}
</style>