previewHetong.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import {
  32. base64ToPath
  33. } from "image-tools";
  34. const tId = ref(null)
  35. const info = ref({})
  36. const show = ref(false)
  37. const imgList = ref([])
  38. const isLoading = ref(true)
  39. const timer1 = ref(null)
  40. onLoad((options) => {
  41. tId.value = options.id;
  42. init();
  43. })
  44. function init() {
  45. getSanfangPreview({
  46. id: tId.value
  47. }).then(res => {
  48. info.value = res.data;
  49. imgList.value = res.data.base64List;
  50. }).finally(() => {
  51. isLoading.value = false;
  52. })
  53. }
  54. async function previewBase64Image(base64Data) {
  55. try {
  56. // 关键步骤:将 Base64 字符串转换为本地临时路径
  57. // 此处以使用 image-tools 的 base64ToPath 为例
  58. const localPath = await base64ToPath(base64Data);
  59. // 调用 UniApp 的图片预览 API
  60. uni.previewImage({
  61. urls: [localPath], // 注意:urls 参数需要是数组,即使只预览一张图
  62. current: 0, // 当前显示图片在 urls 数组中的索引
  63. });
  64. } catch (error) {
  65. // 如果出现错误(如转换失败),隐藏加载提示并告知用户
  66. console.error('预览失败:', error);
  67. uni.showToast({
  68. title: '预览失败',
  69. icon: 'none'
  70. });
  71. }
  72. }
  73. function goUpPage() {
  74. uni.navigateBack()
  75. clearTimeout(timer1.value);
  76. timer1.value = null;
  77. }
  78. function handleGoLishi() {
  79. uni.redirectTo({
  80. url: '/pages/admin/Hetong/HetongList'
  81. })
  82. }
  83. </script>
  84. <style scoped>
  85. </style>