yh-hiprint 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -766,6 +766,7 @@ function tableColumnCancel() {
766
766
  function init() {
767
767
  try {
768
768
  hiprint.init({
769
+ host: 'http://localhost:17521',
769
770
  providers: [new defaultElementTypeProvider()],
770
771
  });
771
772
  hiprint.setConfig();
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## yh-hiprint
2
2
 
3
- 力控远海技术中心封装的 hiprint
3
+ 力控元海技术中心封装的 hiprint
4
4
 
5
5
  当前教程匹配 2+ 版本
6
6
 
@@ -16,17 +16,26 @@ import {onMounted, ref} from 'vue';
16
16
  import {useRoute} from 'vue-router';
17
17
  import axios from '@/libs/api.request.js';
18
18
  import {hiprint} from 'yh-hiprint';
19
+ import {getPrintTemplate} from 'yh-hiprint/libs/index.js';
19
20
 
20
21
  function getQuery() {
21
22
  let query = {};
22
23
  try {
23
- decodeURIComponent(location.hash)
24
+ location.hash
24
25
  .split('?')[1]
25
26
  .split('&')
26
27
  .forEach((str) => {
27
28
  let index = str.indexOf('=');
28
29
  let key = str.substring(0, index);
29
- let value = str.substring(index + 1);
30
+ let value = decodeURIComponent(str.substring(index + 1));
31
+ switch (key) {
32
+ case 'returnHtml':
33
+ value = value === '1';
34
+ break;
35
+ case 'isCustom':
36
+ value = value === '1';
37
+ break;
38
+ }
30
39
  query[key] = value;
31
40
  });
32
41
  } catch (error) {
@@ -62,77 +71,79 @@ let loading = ElLoading.service({
62
71
  text: '正在加载数据……',
63
72
  });
64
73
 
65
- function getData(query) {
66
- let {code, params, data, isCustom} = query;
74
+ async function getData(query) {
75
+ let {code, params, data, isCustom, returnHtml} = query;
67
76
  loading.setText('获取打印配置和数据');
68
77
  let reqParams = params ? JSON.parse(params) : [];
69
-
70
- axios
71
- .request({
72
- url: `/printTemplate/data/${code}`,
73
- method: 'post',
74
- type: 'json',
75
- data: reqParams,
76
- })
77
- .then(async (res) => {
78
- loading.setText('格式化数据,初始化打印插件');
79
- let {list, json} = res.data;
80
- if (json) {
81
- let hasData = false;
82
- if (Array.isArray(list) && list.length > 0) {
83
- list = list.map((item, index) => {
84
- let printData = {};
85
- let datas = Object.entries(item);
86
- datas.forEach((arr) => {
87
- if (Array.isArray(arr[1])) {
88
- hasData = arr[1].length > 0;
89
- printData[arr[0]] = arr[1];
90
- } else {
91
- if (arr[1] !== null) {
92
- hasData = true;
93
- let itemsEntries = Object.entries(arr[1]);
94
- itemsEntries.forEach((cArr) => {
95
- printData[`$${arr[0]}[${cArr[0]}]`] = cArr[1];
96
- });
97
- }
98
- }
99
- });
100
- return printData;
101
- });
102
- } else {
103
- list = [];
104
- }
105
- let hiprintTemplate = new hiprint.PrintTemplate({template: JSON.parse(json)});
106
- // 如若有本地数据,那么将本地数据替换远端数据。传入预览中。
107
- if (isCustom === '1') {
108
- await ElMessageBox.prompt('在下面输入框中输入您给的自定义数据', '自定义数据', {
109
- inputType: 'textarea',
110
- inputValue: localStorage.hiprintCustomValue || null,
111
- }).then((e) => {
112
- try {
113
- hasData = true;
114
- localStorage.hiprintCustomValue = e.value;
115
- list = JSON.parse(e.value);
116
- } catch (error) {
117
- try {
78
+ try {
79
+ let res = await getPrintTemplate(code, reqParams);
80
+ loading.setText('格式化数据,初始化打印插件');
81
+ let {list, json} = res.data;
82
+ if (json) {
83
+ let hasData = false;
84
+ if (Array.isArray(list) && list.length > 0) {
85
+ list = list.map((item, index) => {
86
+ let printData = {};
87
+ let datas = Object.entries(item);
88
+ datas.forEach((arr) => {
89
+ if (Array.isArray(arr[1])) {
90
+ hasData = arr[1].length > 0;
91
+ printData[arr[0]] = arr[1];
92
+ } else {
93
+ if (arr[1] !== null) {
118
94
  hasData = true;
119
- list = JSON.parse(data);
120
- } catch (error) {
121
- hasData = false;
122
- list = [];
95
+ let itemsEntries = Object.entries(arr[1]);
96
+ itemsEntries.forEach((cArr) => {
97
+ printData[`$${arr[0]}[${cArr[0]}]`] = cArr[1];
98
+ });
123
99
  }
124
100
  }
125
101
  });
126
- } else if (data) {
102
+ return printData;
103
+ });
104
+ } else {
105
+ list = [];
106
+ }
107
+ let hiprintTemplate = new hiprint.PrintTemplate({template: JSON.parse(json)});
108
+ // 如若有本地数据,那么将本地数据替换远端数据。传入预览中。
109
+ if (isCustom === '1') {
110
+ await ElMessageBox.prompt('在下面输入框中输入您给的自定义数据', '自定义数据', {
111
+ inputType: 'textarea',
112
+ inputValue: localStorage.hiprintCustomValue || null,
113
+ }).then((e) => {
127
114
  try {
128
115
  hasData = true;
129
- list = JSON.parse(data);
116
+ localStorage.hiprintCustomValue = e.value;
117
+ list = JSON.parse(e.value);
130
118
  } catch (error) {
131
- hasData = false;
132
- list = [];
119
+ try {
120
+ hasData = true;
121
+ list = JSON.parse(data);
122
+ } catch (error) {
123
+ hasData = false;
124
+ list = [];
125
+ }
133
126
  }
127
+ });
128
+ } else if (data) {
129
+ try {
130
+ hasData = true;
131
+ list = JSON.parse(data);
132
+ } catch (error) {
133
+ hasData = false;
134
+ list = [];
134
135
  }
135
- let html = hiprintTemplate.getHtml(list);
136
+ }
137
+ let html = hiprintTemplate.getHtml(list);
138
+ if (opener && returnHtml && html[0]) {
139
+ opener.postMessage({
140
+ type: 'sendPrintHTML',
141
+ data: html[0].innerHTML,
142
+ });
143
+ ElMessageBox.alert('渲染完成!确认后返回系统').then(() => {
144
+ window.close();
145
+ });
146
+ } else {
136
147
  document.body.innerHTML = '';
137
148
  document.body.appendChild(html[0]);
138
149
  if (hasData) {
@@ -144,18 +155,17 @@ function getData(query) {
144
155
  window.close();
145
156
  });
146
157
  }
147
- success.value = false;
148
- } else {
149
- error.value = true;
150
158
  }
151
- })
152
- .catch((err) => {
153
- console.error(err);
159
+ success.value = false;
160
+ } else {
154
161
  error.value = true;
155
- })
156
- .finally(() => {
157
- loading.close();
158
- });
162
+ }
163
+ } catch (error) {
164
+ console.error(error);
165
+ error.value = true;
166
+ } finally {
167
+ loading.close();
168
+ }
159
169
  }
160
170
  </script>
161
171
  <style lang="scss"></style>
@@ -1,17 +1,16 @@
1
- import { hiprint as h, defaultElementTypeProvider as p } from "../libs/hiprint.bundle.js";
1
+ import {hiprint as h, defaultElementTypeProvider as p} from '../libs/hiprint.bundle.js';
2
2
  // 调用浏览器打印js
3
- import "../libs/plugins/jquery.hiwprint.js";
3
+ import '../libs/plugins/jquery.hiwprint.js';
4
4
  // 默认配置
5
- import "../libs/hiprint.config.js";
5
+ import '../libs/hiprint.config.js';
6
6
  // 样式
7
- import "../libs/css/hiprint.css";
8
- import "../libs/css/print-lock.css";
9
- import { ref, computed, watch, reactive, nextTick } from "vue";
7
+ import '../libs/css/hiprint.css';
8
+ import {ref, computed, watch, reactive, nextTick} from 'vue';
10
9
 
11
10
  export const hiprint = h;
12
11
  export const defaultElementTypeProvider = p;
13
12
 
14
- export function print (provider = defaultElementTypeProvider, template, ...args) {
13
+ export function print(provider = defaultElementTypeProvider, template, ...args) {
15
14
  hiprint.init({
16
15
  providers: [new provider()],
17
16
  });
@@ -22,7 +21,7 @@ export function print (provider = defaultElementTypeProvider, template, ...args)
22
21
  return hiprintTemplate;
23
22
  }
24
23
 
25
- export function print2 (provider = defaultElementTypeProvider, template, ...args) {
24
+ export function print2(provider = defaultElementTypeProvider, template, ...args) {
26
25
  hiprint.init({
27
26
  providers: [new provider()],
28
27
  });
@@ -33,26 +32,26 @@ export function print2 (provider = defaultElementTypeProvider, template, ...args
33
32
  return hiprintTemplate;
34
33
  }
35
34
 
36
- export function usePaper () {
37
- const paperType = ref("A4");
35
+ export function usePaper() {
36
+ const paperType = ref('A4');
38
37
  const paperTypeName = computed(() => {
39
38
  const paperTypeObj = paperTypesObj.find((item) => item.value === paperType.value);
40
39
  if (paperTypeObj) {
41
40
  return paperTypeObj.label;
42
41
  } else {
43
- return "自定义";
42
+ return '自定义';
44
43
  }
45
44
  });
46
45
  const paperWidth = ref(210);
47
46
  const paperHeight = ref(296.6);
48
47
 
49
48
  const paperTypesObj = reactive([
50
- { label: "A3", value: "A3" },
51
- { label: "A4", value: "A4" },
52
- { label: "A5", value: "A5" },
53
- { label: "B3", value: "B3" },
54
- { label: "B4", value: "B4" },
55
- { label: "B5", value: "B5" },
49
+ {label: 'A3', value: 'A3'},
50
+ {label: 'A4', value: 'A4'},
51
+ {label: 'A5', value: 'A5'},
52
+ {label: 'B3', value: 'B3'},
53
+ {label: 'B4', value: 'B4'},
54
+ {label: 'B5', value: 'B5'},
56
55
  ]);
57
56
 
58
57
  const paperTypes = {
@@ -82,9 +81,9 @@ export function usePaper () {
82
81
  },
83
82
  };
84
83
 
85
- function setPaper (type, callback) {
86
- if (type !== "other") {
87
- let { width, height } = paperTypes[type];
84
+ function setPaper(type, callback) {
85
+ if (type !== 'other') {
86
+ let {width, height} = paperTypes[type];
88
87
  paperWidth.value = width;
89
88
  paperHeight.value = height;
90
89
  }
@@ -94,19 +93,19 @@ export function usePaper () {
94
93
  watch([paperWidth, paperHeight], ([width, height], [oldW, oldH]) => {
95
94
  nextTick(() => {
96
95
  if (width === 420 && height === 297) {
97
- paperType.value = "A3";
96
+ paperType.value = 'A3';
98
97
  } else if (width === 210 && height === 297) {
99
- paperType.value = "A4";
98
+ paperType.value = 'A4';
100
99
  } else if (width === 210 && height === 148) {
101
- paperType.value = "A5";
100
+ paperType.value = 'A5';
102
101
  } else if (width === 500 && height === 353) {
103
- paperType.value = "B3";
102
+ paperType.value = 'B3';
104
103
  } else if (width === 250 && height === 353) {
105
- paperType.value = "B4";
104
+ paperType.value = 'B4';
106
105
  } else if (width === 250 && height === 176) {
107
- paperType.value = "B5";
106
+ paperType.value = 'B5';
108
107
  } else {
109
- paperType.value = "other";
108
+ paperType.value = 'other';
110
109
  }
111
110
  });
112
111
  });
@@ -121,7 +120,7 @@ export function usePaper () {
121
120
  };
122
121
  }
123
122
 
124
- export function useScale (callback) {
123
+ export function useScale(callback) {
125
124
  const scaleValue = ref(1);
126
125
  const scalePercentage = computed(() => {
127
126
  return `${(scaleValue.value * 100).toFixed(0)}%`;
@@ -132,11 +131,11 @@ export function useScale (callback) {
132
131
  const canZoomOut = computed(() => {
133
132
  return scaleValue.value < 4;
134
133
  });
135
- function zoomIn () {
134
+ function zoomIn() {
136
135
  scaleValue.value = scaleValue.value - 0.1;
137
136
  callback();
138
137
  }
139
- function zoomOut () {
138
+ function zoomOut() {
140
139
  scaleValue.value = scaleValue.value + 0.1;
141
140
  callback();
142
141
  }
@@ -150,22 +149,22 @@ export function useScale (callback) {
150
149
  };
151
150
  }
152
151
 
153
- export function useDataSource (axios) {
152
+ export function useDataSource(axios) {
154
153
  const detailData = ref();
155
- function getDetail (id) {
154
+ function getDetail(id) {
156
155
  return axios
157
156
  .request({
158
157
  url: `/printTemplate/get/${id}`,
159
- method: "post",
158
+ method: 'post',
160
159
  })
161
160
  .then((res) => {
162
161
  if (res.data.data && res.data.data.sourceCodes) {
163
- let codes = res.data.data.sourceCodes.split(",");
162
+ let codes = res.data.data.sourceCodes.split(',');
164
163
  let formListCode = dataSourceForm.value.map((item) => item.value);
165
164
  codes = codes.filter((item) => formListCode.includes(item));
166
165
  formCode.value = codes;
167
166
  } else {
168
- formCode.value = "";
167
+ formCode.value = '';
169
168
  }
170
169
  detailData.value = res.data.data;
171
170
  return Promise.resolve(res.data.data);
@@ -175,7 +174,7 @@ export function useDataSource (axios) {
175
174
  const dataSourceList = ref([]);
176
175
  const listColumns = computed(() => {
177
176
  return codeMapDataSource.value[listCode.value].map((item) => {
178
- let index = item.indexOf("[");
177
+ let index = item.indexOf('[');
179
178
  return item.substring(index + 1, item.length - 1);
180
179
  });
181
180
  });
@@ -192,18 +191,18 @@ export function useDataSource (axios) {
192
191
  });
193
192
  const dataSourceForm = ref([]);
194
193
  const codeMapDataSource = ref({});
195
- function getDataSourceList () {
194
+ function getDataSourceList() {
196
195
  return axios
197
196
  .request({
198
- url: "/printTemplate/getDsList",
199
- method: "post",
197
+ url: '/printTemplate/getDsList',
198
+ method: 'post',
200
199
  })
201
200
  .then((res) => {
202
201
  res.data.list.forEach((item) => {
203
202
  if (item.columns) {
204
- codeMapDataSource.value[item.code] = item?.columns?.split(",")?.map((col) => `$${item.code}[${col.toUpperCase()}]`) || [];
203
+ codeMapDataSource.value[item.code] = item?.columns?.split(',')?.map((col) => `$${item.code}[${col.toUpperCase()}]`) || [];
205
204
  }
206
- if (item.type === "LIST") {
205
+ if (item.type === 'LIST') {
207
206
  dataSourceList.value.push({
208
207
  label: item.name,
209
208
  value: item.code,
package/index.js CHANGED
@@ -1,63 +1,125 @@
1
- import "./libs/jquery";
2
- export { hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource } from "./hooks/useHiprint";
3
- export { default as fontSize } from "./font-size";
4
- export { default as scale } from "./scale";
5
- export { default as zIndex } from "./z-index";
6
- export { default as panel } from "./panel";
1
+ import './libs/jquery';
2
+ export { hiprint, defaultElementTypeProvider, print, print2, usePaper, useScale, useDataSource } from './hooks/useHiprint';
3
+ export { default as fontSize } from './font-size';
4
+ export { default as scale } from './scale';
5
+ export { default as zIndex } from './z-index';
6
+ export { default as panel } from './panel';
7
+ import { getPrintTemplate } from 'yh-hiprint/libs/index.js';
8
+ import { ElMessageBox, ElLoading } from 'element-plus';
7
9
 
8
10
  export function cLog (string, isError = false) {
9
11
  if (isError) {
10
- console.error("%cyhHiprint:%c", "font-size: 16px;font-weight: bold;color: #00ffff", "font-size: 16px;font-weight: bold;color: #ccccc", string);
12
+ console.error('%cyhHiprint:%c', 'font-size: 16px;font-weight: bold;color: #00ffff', 'font-size: 16px;font-weight: bold;color: #ccccc', string);
11
13
  } else {
12
- console.log("%cyhHiprint%c " + string, "font-size: 18px;font-weight: bold;color: #61AFEF", "font-size: 12px;color: #999");
14
+ console.log('%cyhHiprint%c ' + string, 'font-size: 18px;font-weight: bold;color: #61AFEF', 'font-size: 12px;color: #999');
13
15
  }
14
16
  }
15
17
 
16
- const hiprint = ({ code, params, data, isCustom }) => {
18
+ const hiprintFun = async ({ code, params, data, isCustom, returnHtml }) => {
17
19
  let height = document.documentElement.clientHeight;
18
- let width = (document.documentElement.clientWidth - 1200) / 2;
20
+ let width = 1200;
21
+ let left = (document.documentElement.clientWidth - 1200) / 2;
19
22
  // 转换数组
20
23
  let paramData = params;
21
24
  if (!Array.isArray(params)) {
22
25
  paramData = [params];
23
26
  }
24
- let url = "/hiprint/#/preview?code=" + code;
27
+ let url = '/hiprint/#/preview?code=' + encodeURIComponent(code);
25
28
  if (params) {
26
- url += `&params=${JSON.stringify(paramData)}`;
29
+ url += `&params=${encodeURIComponent(JSON.stringify(paramData))}`;
27
30
  }
28
31
  if (data) {
29
- url += `&data=${JSON.stringify(data)}`;
32
+ url += `&data=${encodeURIComponent(JSON.stringify(data))}`;
30
33
  }
31
34
  if (isCustom) {
32
- url += `&isCustom=${isCustom ? "1" : "0"}`;
35
+ url += `&isCustom=${isCustom ? '1' : '0'}`;
33
36
  }
34
- let windowOpen = window.open(url, "hiprintWindow", `height=${height}, width=1200, top=20, left=${width}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
37
+ if (returnHtml) {
38
+ url += `&returnHtml=${returnHtml ? '1' : '0'}`;
39
+ }
40
+ let windowOpen = window.open(url, 'hiprintWindow', `height=${height}, width=${width}, top=20, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
41
+
42
+ return new Promise((resolve, reject) => {
43
+ if (!windowOpen) {
44
+ reject("预览窗口渲染失败!");
45
+ }
46
+ function printHTMLHandler (e) {
47
+ let { type, data } = e.data;
48
+ if (type === "sendPrintHTML" && data) {
49
+ resolve(data);
50
+ }
51
+ window.removeEventListener('message', printHTMLHandler);
52
+ }
53
+ window.addEventListener('message', printHTMLHandler);
54
+ });
35
55
  };
56
+
57
+ const silentHiprint = async ({ code, params = {}, data }) => {
58
+ if (!code || !data) {
59
+ ElMessageBox.alert("请正确的传递模板编号和打印数据", '提示');
60
+ return false;
61
+ }
62
+ let loading = null;
63
+ try {
64
+ loading = ElLoading.service({
65
+ text: "正在获取打印模板"
66
+ });
67
+ let res = await getPrintTemplate(code, params);
68
+ if (res.status === 200 && res.data && res.data.json) {
69
+ let json = res.data.json;
70
+ let hiprintTemplate = new hiprint.PrintTemplate({ template: JSON.parse(json) });
71
+ let html = hiprintTemplate.getHtml(data);
72
+ if (html[0]) {
73
+ return html[0].innerHTML;
74
+ } else {
75
+ ElMessageBox.alert("打印内容渲染失败", '提示');
76
+ }
77
+ } else {
78
+ ElMessageBox.alert(res.message || "获取打印模板失败", '提示');
79
+ }
80
+ } catch (error) {
81
+ if (loading) {
82
+ loading.close();
83
+ loading = null;
84
+ }
85
+ console.error(error);
86
+ } finally {
87
+ if (loading) {
88
+ loading.close();
89
+ loading = null;
90
+ }
91
+ }
92
+
93
+ };
94
+
95
+ window.silentHiprint = silentHiprint;
96
+
36
97
  export default {
37
98
  install (app, { router, pinia, isAdmin }) {
38
- app.provide("$hiprint", hiprint);
99
+ app.provide('$hiprint', hiprintFun);
100
+ app.provide('$silentHiprint', silentHiprint);
39
101
  if (router) {
40
- router.addRoute("Index", {
41
- path: "hiprint/designer",
42
- name: "打印设计器",
102
+ router.addRoute('Index', {
103
+ path: 'hiprint/designer',
104
+ name: '打印设计器',
43
105
  meta: {
44
- icon: "md-planet",
45
- title: "打印设计器",
106
+ icon: 'md-planet',
107
+ title: '打印设计器',
46
108
  },
47
- component: () => import("yh-hiprint/HiprintDesigner.vue"),
109
+ component: () => import('yh-hiprint/HiprintDesigner.vue'),
48
110
  });
49
111
  router.addRoute({
50
- path: "/preview",
51
- name: "printView",
112
+ path: '/preview',
113
+ name: 'printView',
52
114
  meta: {
53
- icon: "md-planet",
54
- title: "printView",
115
+ icon: 'md-planet',
116
+ title: 'printView',
55
117
  },
56
- component: () => import("yh-hiprint/hiprintPreview.vue"),
118
+ component: () => import('yh-hiprint/hiprintPreview.vue'),
57
119
  });
58
120
  } else {
59
121
  if (!router) {
60
- cLog("没有传递 router 对象,所以无法将路由添加到应用中", true);
122
+ cLog('没有传递 router 对象,所以无法将路由添加到应用中', true);
61
123
  }
62
124
  }
63
125
  },