pdfPreView.vue 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
<template>
  <view class="material-detail">
    <!-- App+html+保存:顶部通栏大按钮 + 可选标题,无返回;其它:返回+标题+右上角保存 -->
    <view class="nav-chrome">
      <template v-if="appHtmlFullSaveMode">

        <view class="nav-app-html-tool">
          <view
            class="nav-app-html-tool__btn"
            hover-class="nav-app-html-tool__btn--hover"
            @click.stop="saveReportImage"
            @tap.stop="saveReportImage"
          >保存图片</view>
        </view>
      </template>
      <view v-else class="nav-bar">
        <view class="nav-leading">
          <view class="back" @click.stop="back" @tap.stop="back">
            <uni-icons type="left" :size="20" color="#000"></uni-icons>
          </view>
        </view>
        <view class="nav-title">{{ title }}</view>
        <view
          v-if="showSaveEntry"
          class="nav-save-hit"
          @click.stop="saveReportImage"
          @tap.stop="saveReportImage"
          @longpress.stop="saveReportImage"
        >
          <view class="nav-save-btn">保存图片</view>
        </view>
      </view>
    </view>
    <web-view
      v-if="fileUrltype === 'html' && isAppClient"
      :src="previewUrl"
      :webview-styles="htmlWebviewInsetStyle"
    ></web-view>
    <view
      v-else-if="fileUrltype === 'html' && !isAppClient"
      class="html-webview-shell"
      :style="htmlWebviewShellStyle"
    >
      <web-view :src="previewUrl"></web-view>
    </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: '',
      /** 实测 .nav-bar 底边到窗口顶的距离(px),用于对齐子 webview,避免 rpx 换算与 App 坐标系不一致出现大块白条 */
      measuredNavBottomPx: 0
    }
  },
  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'
    const rawImg = options.reportImageUrl
    if (rawImg != null && String(rawImg).trim() !== '') {
      try {
        this.reportImageUrl = decodeURIComponent(String(rawImg).trim())
      } catch (e) {
        this.reportImageUrl = String(rawImg).trim()
      }
    } else {
      this.reportImageUrl = ''
    }
    console.log(this.previewUrl)
    // else this.setWv()
  },
  computed: {
    /** 报告管理:showSaveBtn=1 且 reportImageUrl 有值才显示保存 */
    showSaveEntry () {
      return this.showSaveBtn && !!this.reportImageUrl
    },
    /** App+html+保存:顶部通栏大按钮,无返回 icon */
    appHtmlFullSaveMode () {
      return this.showSaveEntry && this.isAppClient && this.fileUrltype === 'html'
    },
    /** 未测到布局前:与 .nav-chrome(含通栏工具条)高度兜底一致 */
    htmlReportNavTopPx () {
      const sys = uni.getSystemInfoSync()
      const r = sys.windowWidth / 750
      const topOffset = Math.ceil(48 * r)
      if (this.appHtmlFullSaveMode) {
        const titleH = this.title ? Math.ceil(72 * r) : 0
        const toolH = Math.ceil((12 + 88 + 16) * r)
        return topOffset + titleH + toolH
      }
      return Math.ceil((48 + 96) * r)
    },
    /** 子 web-view 的 top:优先用实测导航栏底边,消除「预留导航」与真机之间的多余白缝 */
    effectiveHtmlWebviewTopPx () {
      return this.measuredNavBottomPx > 0 ? this.measuredNavBottomPx : this.htmlReportNavTopPx
    },
    isAppClient () {
      return typeof plus !== 'undefined'
    },
    /**
     * 仅 App 生效:传给原生 web-view 的初始位置/尺寸,把内容整体下移,露出上方自定义导航栏。
     * H5/小程序里 plus 不存在,返回 {},改由 htmlWebviewShellStyle 外壳限制区域。
     */
    htmlWebviewInsetStyle () {
      if (this.fileUrltype !== 'html' || !this.isAppClient) {
        return {}
      }
      const sys = uni.getSystemInfoSync()
      const topPx = this.effectiveHtmlWebviewTopPx
      const h = Math.max(0, Math.floor(sys.windowHeight - topPx))
      return {
        // top 必须为 topPx:top=0 时子 web-view 盖住导航栏,保存/返回点击无响应
        top: topPx,
        left: 0,
        width: sys.windowWidth,
        height: h
      }
    },
    /** H5:fixed 外壳把 iframe 限制在导航下方 */
    htmlWebviewShellStyle () {
      if (this.fileUrltype !== 'html' || this.isAppClient) {
        return {}
      }
      const topPx = this.effectiveHtmlWebviewTopPx
      return {
        position: 'fixed',
        left: '0',
        right: '0',
        top: `${topPx}px`,
        bottom: '0',
        zIndex: 1
      }
    }
  },
  onReady () {
    this.measureNavBarForWebview()
  },
  mounted () {
    if (this.fileUrltype === 'html' && this.isAppClient) {
      this.$nextTick(() => {
        setTimeout(() => this.applyHtmlWebviewInset(), 200)
      })
    }
  },
  methods: {
    back () {
      uni.navigateBack({
        delta: 1,
        animationType: 'pop-out',
        animationDuration: 100
      })
    },
    measureNavBarForWebview () {
      if (this.fileUrltype !== 'html') return
      const run = () => {
        uni.createSelectorQuery()
          .in(this)
          .select('.nav-chrome')
          .boundingClientRect(rect => {
            if (!rect || rect.bottom <= 0) return
            const bottom = Math.ceil(rect.bottom)
            if (bottom === this.measuredNavBottomPx) return
            this.measuredNavBottomPx = bottom
            if (this.isAppClient) {
              this.$nextTick(() => this.applyHtmlWebviewInset())
            }
          })
          .exec()
      }
      this.$nextTick(() => {
        run()
        setTimeout(run, 80)
        setTimeout(run, 300)
      })
    },
    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 && res.tempFilePath) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: () => {
                uni.showToast({ title: '已保存到相册', icon: 'none' })
              },
              fail: err => {
                console.warn('saveImageToPhotosAlbum', err)
                uni.showToast({
                  title: (err && err.errMsg) || '保存失败,请检查相册权限',
                  icon: 'none'
                })
              },
              complete: () => {
                uni.hideLoading()
              }
            })
          } else {
            uni.hideLoading()
            uni.showToast({ title: '图片下载失败', icon: 'none' })
          }
        },
        fail: err => {
          uni.hideLoading()
          console.warn('downloadFile', err)
          uni.showToast({
            title: (err && err.errMsg) || '图片下载失败',
            icon: 'none'
          })
        }
      })
    },
    /** App html 报告:原生子 webview 与 htmlWebviewInsetStyle 数值对齐 */
    applyHtmlWebviewInset () {
      if (this.fileUrltype !== 'html' || !this.isAppClient) return
      const sys = uni.getSystemInfoSync()
      const topPx = this.effectiveHtmlWebviewTopPx
      const h = Math.max(0, Math.floor(sys.windowHeight - topPx))
      const trySet = () => {
        try {
          const currentWebview = this.$scope.$getAppWebview()
          const kids = currentWebview.children()
          let wv = null
          for (let i = 0; i < kids.length; i++) {
            const c = kids[i]
            if (c && typeof c.setStyle === 'function' && typeof c.draw === 'function') {
              wv = c
              break
            }
          }
          if (wv) {
            wv.setStyle({
              top: String(topPx),
              left: '0',
              width: String(sys.windowWidth),
              height: String(h)
            })
            return true
          }
        } catch (e) {
          console.warn('applyHtmlWebviewInset', e)
        }
        return false
      }
      const retry = (n = 0) => {
        if (trySet() || n > 30) return
        setTimeout(() => retry(n + 1), 50)
      }
      retry()
    },
    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-chrome {
  position: fixed;
  left: 0;
  top: toRpx(48);
  width: 100%;
  z-index: 999999;
  background-color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.nav-app-html-title {
  padding: toRpx(20) toRpx(40) toRpx(8);
  text-align: center;
  font-size: toRpx(30);
  font-weight: 700;
  color: #1a1a1a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-app-html-tool {
  padding: toRpx(12) toRpx(32) toRpx(20);
}
.nav-app-html-tool__btn {
  width: toRpx(188);
  height: toRpx(88);
  line-height: toRpx(88);
  text-align: center;
  border-radius: toRpx(44);
  background: linear-gradient(0deg, #2c66f7 0%, #48a6ff 100%);
  color: #ffffff;
  font-size: toRpx(28);
  font-weight: 600;
}
.nav-app-html-tool__btn--hover {
  opacity: 0.9;
}
.nav-bar {
  position: relative;
  width: 100%;
  height: toRpx(96);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: toRpx(32);
  color: #1a1a1a;
}
.nav-leading {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  flex-direction: row;
  align-items: center;
  max-width: 56%;
  padding-left: toRpx(8);
  box-sizing: border-box;
}
.back {
  flex-shrink: 0;
  width: toRpx(88);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.nav-title {
  flex: 0 1 auto;
  max-width: 52%;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* 不参与点击检测,避免透明区域盖住右侧「保存」导致 App 上只有保存无响应 */
  pointer-events: none;
}
/* 扩大可点区域并提高层级,避免被标题层或 transform 命中异常挡住 */
.nav-save-hit {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  padding: 0 toRpx(16) 0 toRpx(24);
  box-sizing: border-box;
}
.nav-save-btn {
  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);
}
.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);
}
.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;
}
.html-webview-shell {
  width: 100%;
  height: 100%;
}
.html-webview-shell ::v-deep iframe,
.html-webview-shell ::v-deep .uni-webview--fullscreen {
  width: 100% !important;
  height: 100% !important;
}
.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>