previewHetong.vue 2.2 KB

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