DeductShow.vue 3.44 KB
<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="扣款单"
    width="700px"
    :bodyStyle="{ height: '240px' }"
    @ok="handleOk"
  >
    <a-list item-layout="horizontal" :data-source="itemArray">
      <template #renderItem="{ item }">
        <a-list-item>
          <a-list-item-meta>
            <template #title>
              <!-- <a :href="item.url" target="_blank" rel="noopener noreferrer">{{ item.name }}</a> -->
              <!-- <a @click="openPic(item.url)">{{ item.name }}</a> -->
              <a
                v-if="isImageUrl(item.url)"
                @click="openPic(item.url)"
                style="display: flex; align-items: center"
              >
                <img
                  :src="item.url"
                  alt="Image"
                  style="max-width: 150px; max-height: 150px; margin-right: 10px"
                />
                {{ item.name }}
              </a>
              <a v-else @click="openPic(item.url)">{{ item.name }}</a>
            </template>
          </a-list-item-meta>
        </a-list-item>
      </template>
    </a-list>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { computed, ref } from 'vue';
  import type { UploadProps, UploadChangeParam } from 'ant-design-vue';
  import { InboxOutlined } from '@ant-design/icons-vue';
  import { message } from 'ant-design-vue';
  import { getInvoiceDeductUrlById } from '@/api/project/invoice';
  import { view } from '@/utils/pdfShow';

  interface Item {
    name: string;
    url: string;
  }

  const list = ref();
  const id = ref();
  const itemArray = ref<Item[]>([]);

  const [register, { closeModal }] = useModalInner(async (data) => {
    itemArray.value = [];
    const res = await getInvoiceDeductUrlById({ id: data.data.id });
    for (let item in res) {
      const url = res[item];
      const name = item;
      // 将 name 和 url 放入对象并添加到数组中
      itemArray.value.push({ name, url });
    }
  });

  async function handleOk() {
    itemArray.value = [];
    closeModal();
  }
  // function openPic(url) {
  //   window.open('', '', '').document.write(`<!DOCTYPE html>
  //         <html>
  //           <body
  //               style="display: flex;
  //               justify-content: center;
  //               align-items: center;">
  //             <img src='${url}' width="500px" height="500px"/>
  //           </body>
  //         </html>`);
  // }
  // 新增的函数:判断 URL 是否为图片格式
  function isImageUrl(url: string): boolean {
    const imageExtensions = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'svg'];
    const baseUrl = url.split('?')[0];
    return imageExtensions.some((ext) => baseUrl.toLowerCase().endsWith(ext));
  }
  // 检查 URL 是否为 PDF 格式
  function isPdfUrl(url: string): boolean {
    return url.toLowerCase().endsWith('.pdf');
  }
  // 打开图片或 PDF
  function openPic(url: string) {
    const baseUrl = url.split('?')[0]; // 获取问号前的部分
    if (isImageUrl(baseUrl)) {
      window.open('', '', '').document.write(`<!DOCTYPE html>
      <html>
        <body style="display: flex; justify-content: center; align-items: center;">
          <img src='${url}' width="500px" height="500px"/>
        </body>
      </html>`);
    } else if (isPdfUrl(baseUrl)) {
      view(url); // 新标签页打开 PDF
    } else {
      console.log('不支持的文件类型');
    }
  }
</script>