previewHetong.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view class="phone-hetong-page">
  3. <view class="phone-navBar-box">
  4. <view @click="goUpPage" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">合同</text>
  6. <view></view>
  7. </view>
  8. <template v-if="tId">
  9. <view class="pdf-box">
  10. <img v-for="item in imgList" mode="aspectFit" :src="`data:image/png;base64,${item}`"
  11. @click="previewBase64Image(`data:image/png;base64,${item}`)" class="pdf-img" />
  12. </view>
  13. <!-- 加载提示 -->
  14. <view class="pdf-tip-box" v-if="isLoading">
  15. <view class="tip-img-box">
  16. <view class="tip-text">加载中...</view>
  17. </view>
  18. </view>
  19. </template>
  20. </view>
  21. </template>
  22. <script setup>
  23. import {
  24. ref
  25. } from "vue";
  26. import * as httpApi from "@/api/hetong.js"
  27. import {
  28. onLoad
  29. } from "@dcloudio/uni-app"
  30. import {getSanfangPreview} from "../../../api/sanfang";
  31. const tId = ref(null)
  32. const info = ref({})
  33. const show = ref(false)
  34. const imgList = ref([])
  35. const isLoading = ref(true)
  36. const timer1 = ref(null)
  37. onLoad((options) => {
  38. tId.value = options.id;
  39. init();
  40. })
  41. function init() {
  42. getSanfangPreview({
  43. id: tId.value
  44. }).then(res => {
  45. info.value = res.data;
  46. imgList.value = res.data.base64List;
  47. }).finally(() => {
  48. isLoading.value = false;
  49. })
  50. }
  51. async function previewBase64Image(base64Data) {
  52. try {
  53. // 关键步骤:将 Base64 字符串转换为本地临时路径
  54. // 此处以使用 image-tools 的 base64ToPath 为例
  55. const localPath = await base64ToPath(base64Data);
  56. // 调用 UniApp 的图片预览 API
  57. uni.previewImage({
  58. urls: [localPath], // 注意:urls 参数需要是数组,即使只预览一张图
  59. current: 0, // 当前显示图片在 urls 数组中的索引
  60. });
  61. } catch (error) {
  62. // 如果出现错误(如转换失败),隐藏加载提示并告知用户
  63. console.error('预览失败:', error);
  64. uni.showToast({
  65. title: '预览失败',
  66. icon: 'none'
  67. });
  68. }
  69. }
  70. function goUpPage() {
  71. uni.navigateBack()
  72. clearTimeout(timer1.value);
  73. timer1.value = null;
  74. }
  75. function handleGoLishi() {
  76. uni.redirectTo({
  77. url: '/pages/admin/Hetong/HetongList'
  78. })
  79. }
  80. </script>
  81. <style scoped>
  82. </style>