chenhaozhe
3 天以前 00ed7e51cecfc5e97e88bf2c9838c87362ea396d
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<template>
  <!-- <a-modal :visible="visible" :maskClosable="false" @cancel="hideModal" :width="width + 'mm'">
    <a-spin :spinning="spinning" style="min-height: 100px">
      <div id="preview_content_design"></div>
    </a-spin>
    <template slot="title">
      <a-space>
        <div style="margin-right: 20px">打印预览</div>
        <a-button :loading="waitShowPrinter" type="primary" icon="printer" @click.stop="print">打印</a-button>
        <a-button type="primary" icon="printer" @click.stop="toPdf">pdf</a-button>
      </a-space>
    </template>
<template slot="footer">
      <a-button key="close" type="info" @click="hideModal">
        关闭
      </a-button>
    </template>
</a-modal> -->
  <el-dialog title="打印预览" :visible.sync="visible" :close-on-click-modal="false" :close-on-press-escape="false"
    append-to-body @closed="onDialogClosed">
    <!-- 替换 el-spin → 使用 Ruoyi 支持的 v-loading -->
    <div style="min-height: 100px; position:relative;" v-loading="spinning" element-loading-text="加载中...">
      <div id="preview_content_design"></div>
    </div>
 
    <span slot="title" class="dialog-title-wrapper">
      <span>打印预览</span>
      <span class="dialog-title-btn">
        <el-button :loading="waitShowPrinter" type="primary" icon="el-icon-printer" @click.stop="print">
          打印
        </el-button>
        <el-button type="primary" icon="el-icon-document" @click.stop="toPdf">
          PDF
        </el-button>
      </span>
    </span>
 
    <div slot="footer" class="dialog-footer">
      <el-button key="close" type="info" @click="hideModal">
        关闭
      </el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import { template } from 'lodash'
import * as vuePluginHiprint from '../../../hiprintVue/index'
import $ from 'jquery'
window.jQuery = window.$ = $
const hiprint = vuePluginHiprint.hiprint
const defaultElementTypeProvider = vuePluginHiprint.defaultElementTypeProvider
 
// 全局挂载(preview 必须)
window.hiprint = hiprint
window.defaultElementTypeProvider = defaultElementTypeProvider
 
export default {
  name: "printPreview",
  props: {},
  data() {
    return {
      visible: false,
      spinning: true,
      waitShowPrinter: false,
      isInited: false,
      // 纸张宽 mm
      width: 0,
      // 模板
      hiprintTemplate: {},
      // 数据
      printData: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.initHiPrintInPreview()
  },
  mounted() {
  },
  methods: {
    hideModal() {
      this.visible = false
    },
    // ======================
    // 异步初始化方法
    // ======================
    async initHiPrintAsync() {
      return new Promise((resolve) => {
        // 如果已经初始化过,直接返回
        if (this.isInited) {
          console.log("✅ preview 内部初始化 hiprint 完成")
          return resolve()
        }
 
        // 执行初始化
        window.hiprint.init({
          providers: [new window.defaultElementTypeProvider()],
          autoConnect: false
        })
 
        // 等待DOM/资源加载(保险等待 100ms)
        setTimeout(() => {
          this.isInited = true
          console.log("✅ preview 内部初始化 hiprint 完成")
          resolve()
        }, 100)
      })
    },
    initHiPrintInPreview() {
      if (hiprint) return
 
      // 从插件获取实例
      hiprint = vuePluginHiprint.hiprint
      defaultElementTypeProvider = vuePluginHiprint.defaultElementTypeProvider
 
      // 内部初始化!
      hiprint.init({
        providers: [new defaultElementTypeProvider()],
        autoConnect: false
      })
 
      console.log("✅ preview 内部初始化 hiprint 完成")
    },
    async show(hiprintTemplate, printData, width = '210') {
      await this.initHiPrintAsync()
      this.visible = true
      this.spinning = true
      this.width = hiprintTemplate.editingPanel ? hiprintTemplate.editingPanel.width : width;
      this.hiprintTemplate = hiprintTemplate
      this.printData = printData
      let hiprintTemplateInstance = new window.hiprint.PrintTemplate({
        template: hiprintTemplate,
        settingContainer: $('#preview_content_design'),
        fields: printData
      });
 
      // hiprintTemplateInstance.getJson(tempContainer)
      setTimeout(() => {
        // eslint-disable-next-line no-undef
        $("#preview_content_design").html(hiprintTemplateInstance.getHtml(printData))
        this.spinning = false
        this.hiprintTemplate = hiprintTemplateInstance
      }, 1000)
    },
    print() {
      this.waitShowPrinter = true
      this.hiprintTemplate.print(this.printData, {}, {
        callback: () => {
          console.log('callback')
          this.waitShowPrinter = false
        }
      })
    },
    toPdf() {
      this.hiprintTemplate.toPdf(this.printData, '打印预览', { scale: 4 });
    },
    // 关闭弹窗
    hideModal() {
      this.visible = false;
    },
    // 弹窗完全关闭后 → 强制移走焦点(修复你之前的浏览器报错!)
    onDialogClosed() {
      document.body.focus();
    }
  }
}
 
</script>
 
 
<style lang="less" scoped>
// /deep/ .ant-modal-body {
//   padding: 0px;
// }
 
// /deep/ .ant-modal-content {
//   margin-bottom: 24px;
// }
 
/* 打印预览弹窗标题样式 */
.dialog-title-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}
 
.dialog-title-btn {
  display: flex;
  gap: 8px;
}
</style>