ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { ref } from 'vue';
 
import { useGlobSetting } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import dayjs from 'dayjs';
import $ from 'jquery';
 
import { getPreviewInfo } from '#/api/system/printDev';
import logoImg from '#/assets/images/jnpf.png';
import { newHiprintPrintTemplate } from '#/components/PrintDesign/PrintDesign/utils/template-helper';
import { $t } from '#/locales';
 
defineOptions({ name: 'PrintPreview' });
 
const globSetting = useGlobSetting();
const [registerModal, { closeModal, changeLoading }] = useModalInner(init);
const hiprintTemplate = ref();
const fullName = ref('');
 
/**
 * 初始化
 */
async function init(data) {
  fullName.value = data.fullName || '';
  $('#previewContentDesign').html(null);
  changeLoading(true);
  const { id, printTplPanels = '' } = data;
  try {
    let realPanels = {};
    if (id) {
      const res = await getPreviewInfo(id);
      realPanels = JSON.parse(res?.data?.printTemplate);
    } else {
      realPanels = JSON.parse(printTplPanels);
    }
    hiprintTemplate.value = newHiprintPrintTemplate('printPreview', { template: realPanels });
    initHinnn();
    $('#previewContentDesign').html(hiprintTemplate.value?.getHtml() || '模板加载失败');
  } catch {
    $('#previewContentDesign').html('<div class="tpl-invalid">模板已失效,请重新设计</div>');
  } finally {
    changeLoading(false);
  }
}
// 重写hinnn
function initHinnn() {
  if (!(window as any).hinnn) return;
  (window as any).hinnn.apiUrl = globSetting.apiURL;
  (window as any).hinnn.dateFormat = function (date, format) {
    if (!date) return '';
    if (!Number.isNaN(date) && typeof date === 'string') date = Number(date);
    format = format.replaceAll('y', 'Y').replaceAll('d', 'D');
    return dayjs(date).format(format);
  };
}
/**
 * 浏览器打印
 */
function handlePrint() {
  const options = { leftOffset: 0, topOffset: 0 };
  // 调用浏览器打印
  hiprintTemplate.value?.print({}, options, {
    styleHandler: () => {
      return '<link rel="stylesheet" href="/css/print-lock.css" />';
    },
  });
}
// 导出PDF
function handleDownload() {
  if (!hiprintTemplate.value) return;
  hiprintTemplate.value?.toPdf({}, `${fullName.value}_${dayjs().format('YYYYMMDDHHmmss')}`, { isDownload: true }).then(() => {});
}
</script>
 
<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    default-fullscreen
    :footer="null"
    :closable="false"
    :keyboard="false"
    destroy-on-close
    class="jnpf-full-modal full-modal designer-modal jnpf-print-preview-modal">
    <template #title>
      <div class="jnpf-full-modal-header">
        <div class="header-title">
          <img :src="logoImg" class="header-logo" />
          <span class="header-dot"></span>
          <p class="header-txt">打印预览</p>
        </div>
        <a-space class="options" :size="10">
          <a-button type="primary" @click="handleDownload()">导出PDF</a-button>
          <a-button type="primary" @click="handlePrint()">{{ $t('common.printText') }}</a-button>
          <a-button @click="closeModal()">{{ $t('common.cancelText') }}</a-button>
        </a-space>
      </div>
    </template>
    <div id="previewContentDesign"></div>
  </BasicModal>
</template>
 
<style lang="scss">
.jnpf-full-modal.jnpf-print-preview-modal {
  .ant-modal-body > .scrollbar {
    padding: 0 0 10px !important;
  }
 
  .scrollbar__view {
    overflow: hidden auto !important;
  }
 
  .hiprint-printPaper {
    margin: 10px auto;
    background-color: #fff;
  }
 
  .tpl-invalid {
    box-sizing: border-box;
    width: 210mm;
    height: 296mm;
    padding-top: 20px;
    margin: 0 auto;
    font-size: 16px;
    text-align: center;
    background: #fff;
  }
}
 
#hiwprint_iframe {
  border: 0 !important;
}
</style>