yh-hiprint 2.6.13 → 2.6.15

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.
@@ -11,165 +11,164 @@
11
11
  sub-title="请求参数或者返回数据错误"></el-result>
12
12
  </template>
13
13
  <script setup>
14
- import {ElLoading, ElResult, ElMessageBox} from 'element-plus';
15
- import {onMounted, ref} from 'vue';
16
- import {useRoute} from 'vue-router';
17
- import axios from '@/libs/api.request.js';
18
- import {hiprint} from 'yh-hiprint';
19
- import {getPrintTemplate} from 'yh-hiprint/libs/index.js';
14
+ import {ElLoading, ElResult, ElMessageBox} from 'element-plus';
15
+ import {onMounted, ref} from 'vue';
16
+ import {useRoute} from 'vue-router';
17
+ import {hiprint} from 'yh-hiprint';
18
+ import {getPrintTemplate} from 'yh-hiprint';
20
19
 
21
- function getQuery() {
22
- let query = {};
23
- try {
24
- location.hash
25
- .split('?')[1]
26
- .split('&')
27
- .forEach((str) => {
28
- let index = str.indexOf('=');
29
- let key = str.substring(0, index);
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
- }
39
- query[key] = value;
40
- });
41
- } catch (error) {
42
- console.error('hiprint Preview getQuery on error:', error);
20
+ function getQuery() {
21
+ let query = {};
22
+ try {
23
+ location.hash
24
+ .split('?')[1]
25
+ .split('&')
26
+ .forEach((str) => {
27
+ let index = str.indexOf('=');
28
+ let key = str.substring(0, index);
29
+ let value = decodeURIComponent(str.substring(index + 1));
30
+ switch (key) {
31
+ case 'returnHtml':
32
+ value = value === '1';
33
+ break;
34
+ case 'isCustom':
35
+ value = value === '1';
36
+ break;
37
+ }
38
+ query[key] = value;
39
+ });
40
+ } catch (error) {
41
+ console.error('hiprint Preview getQuery on error:', error);
42
+ }
43
+ return query;
43
44
  }
44
- return query;
45
- }
46
45
 
47
- onMounted(() => {
48
- let query = getQuery();
49
- if (query.code) {
50
- sessionStorage.printQuery = JSON.stringify(query);
51
- getData(query);
52
- } else {
53
- if (sessionStorage.printQuery) {
54
- query = JSON.parse(sessionStorage.printQuery);
46
+ onMounted(() => {
47
+ let query = getQuery();
48
+ if (query.code) {
49
+ sessionStorage.printQuery = JSON.stringify(query);
55
50
  getData(query);
56
51
  } else {
57
- loading.close();
58
- error.value = true;
52
+ if (sessionStorage.printQuery) {
53
+ query = JSON.parse(sessionStorage.printQuery);
54
+ getData(query);
55
+ } else {
56
+ loading.close();
57
+ error.value = true;
58
+ }
59
59
  }
60
- }
61
- window.addEventListener('afterprint', () => {
62
- self.close();
60
+ window.addEventListener('afterprint', () => {
61
+ self.close();
62
+ });
63
63
  });
64
- });
65
64
 
66
- const route = useRoute();
67
- const success = ref(false);
68
- const error = ref(false);
65
+ const route = useRoute();
66
+ const success = ref(false);
67
+ const error = ref(false);
69
68
 
70
- let loading = ElLoading.service({
71
- text: '正在加载数据……',
72
- });
69
+ let loading = ElLoading.service({
70
+ text: '正在加载数据……',
71
+ });
73
72
 
74
- async function getData(query) {
75
- let {code, params, data, isCustom, returnHtml} = query;
76
- loading.setText('获取打印配置和数据');
77
- let reqParams = params ? JSON.parse(params) : [];
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) {
73
+ async function getData(query) {
74
+ let {code, params, data, isCustom, returnHtml} = query;
75
+ loading.setText('获取打印配置和数据');
76
+ let reqParams = params ? JSON.parse(params) : [];
77
+ try {
78
+ let res = await getPrintTemplate(code, reqParams);
79
+ loading.setText('格式化数据,初始化打印插件');
80
+ let {list, json} = res.data;
81
+ if (json) {
82
+ let hasData = false;
83
+ if (Array.isArray(list) && list.length > 0) {
84
+ list = list.map((item, index) => {
85
+ let printData = {};
86
+ let datas = Object.entries(item);
87
+ datas.forEach((arr) => {
88
+ if (Array.isArray(arr[1])) {
89
+ hasData = arr[1].length > 0;
90
+ printData[arr[0]] = arr[1];
91
+ } else {
92
+ if (arr[1] !== null) {
93
+ hasData = true;
94
+ let itemsEntries = Object.entries(arr[1]);
95
+ itemsEntries.forEach((cArr) => {
96
+ printData[`$${arr[0]}[${cArr[0]}]`] = cArr[1];
97
+ });
98
+ }
99
+ }
100
+ });
101
+ return printData;
102
+ });
103
+ } else {
104
+ list = [];
105
+ }
106
+ let hiprintTemplate = new hiprint.PrintTemplate({template: JSON.parse(json)});
107
+ // 如若有本地数据,那么将本地数据替换远端数据。传入预览中。
108
+ if (isCustom === '1') {
109
+ await ElMessageBox.prompt('在下面输入框中输入您给的自定义数据', '自定义数据', {
110
+ inputType: 'textarea',
111
+ inputValue: localStorage.hiprintCustomValue || null,
112
+ }).then((e) => {
113
+ try {
114
+ hasData = true;
115
+ localStorage.hiprintCustomValue = e.value;
116
+ list = JSON.parse(e.value);
117
+ } catch (error) {
118
+ try {
94
119
  hasData = true;
95
- let itemsEntries = Object.entries(arr[1]);
96
- itemsEntries.forEach((cArr) => {
97
- printData[`$${arr[0]}[${cArr[0]}]`] = cArr[1];
98
- });
120
+ list = JSON.parse(data);
121
+ } catch (error) {
122
+ hasData = false;
123
+ list = [];
99
124
  }
100
125
  }
101
126
  });
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
+ } else if (data) {
114
128
  try {
115
129
  hasData = true;
116
- localStorage.hiprintCustomValue = e.value;
117
- list = JSON.parse(e.value);
130
+ list = JSON.parse(data);
118
131
  } catch (error) {
119
- try {
120
- hasData = true;
121
- list = JSON.parse(data);
122
- } catch (error) {
123
- hasData = false;
124
- list = [];
125
- }
132
+ hasData = false;
133
+ list = [];
126
134
  }
127
- });
128
- } else if (data) {
129
- try {
130
- hasData = true;
131
- list = JSON.parse(data);
132
- } catch (error) {
133
- hasData = false;
134
- list = [];
135
135
  }
136
- }
137
136
 
138
- // 默认浏览器打印
139
- let html = hiprintTemplate.getHtml(list);
140
- if (opener && returnHtml && html[0]) {
141
- opener.postMessage({
142
- type: 'sendPrintHTML',
143
- data: html[0].innerHTML,
144
- });
145
- ElMessageBox.alert('渲染完成!确认后返回系统').then(() => {
146
- window.close();
147
- });
148
- } else {
149
- document.body.innerHTML = '';
150
- document.body.appendChild(html[0]);
151
- if (hasData) {
152
- setTimeout(() => {
153
- window.print();
154
- }, 1000);
155
- } else {
156
- ElMessageBox.alert('数据源没有数据,打印将取消').then(() => {
137
+ // 默认浏览器打印
138
+ let html = hiprintTemplate.getHtml(list);
139
+ if (opener && returnHtml && html[0]) {
140
+ opener.postMessage({
141
+ type: 'sendPrintHTML',
142
+ data: html[0].innerHTML,
143
+ });
144
+ ElMessageBox.alert('渲染完成!确认后返回系统').then(() => {
157
145
  window.close();
158
146
  });
147
+ } else {
148
+ document.body.innerHTML = '';
149
+ document.body.appendChild(html[0]);
150
+ if (hasData) {
151
+ setTimeout(() => {
152
+ window.print();
153
+ }, 1000);
154
+ } else {
155
+ ElMessageBox.alert('数据源没有数据,打印将取消').then(() => {
156
+ window.close();
157
+ });
158
+ }
159
159
  }
160
+ success.value = false;
161
+ } else {
162
+ error.value = true;
160
163
  }
161
- success.value = false;
162
- } else {
164
+ } catch (error) {
165
+ console.error(error);
163
166
  error.value = true;
164
- }
165
- } catch (error) {
166
- console.error(error);
167
- error.value = true;
168
- } finally {
169
- if (loading) {
170
- loading.close();
167
+ } finally {
168
+ if (loading) {
169
+ loading.close();
170
+ }
171
171
  }
172
172
  }
173
- }
174
173
  </script>
175
174
  <style lang="scss"></style>
@@ -0,0 +1,3 @@
1
+ export function useHipirntCanvas() {
2
+ return {};
3
+ }
@@ -1,17 +1,58 @@
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
3
  import '../libs/plugins/jquery.hiwprint.js';
4
4
  // 默认配置
5
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 '../libs/css/print-lock.css';
9
+ import {ref, computed, watch, reactive, nextTick} from 'vue';
10
+ import useHiprintStore from '../store/useHiprintStore.js';
11
+
12
+ // 定义类型
13
+ interface PaperType {
14
+ label: string;
15
+ value: string;
16
+ }
17
+
18
+ interface PaperDimensions {
19
+ width: number;
20
+ height: number;
21
+ }
22
+
23
+ interface DataSourceItem {
24
+ columns?: string;
25
+ code: string;
26
+ name: string;
27
+ type: string;
28
+ }
29
+
30
+ interface DataSourceOption {
31
+ label: string;
32
+ value: string;
33
+ }
34
+
35
+ interface AxiosInstance {
36
+ request: (config: any) => Promise<any>;
37
+ }
38
+
39
+ interface Response {
40
+ data: {
41
+ data?: {
42
+ sourceCodes?: string;
43
+ };
44
+ list: DataSourceItem[];
45
+ };
46
+ }
10
47
 
11
48
  export const hiprint = h;
12
49
  export const defaultElementTypeProvider = p;
13
50
 
14
- export function print (provider = defaultElementTypeProvider, template, ...args) {
51
+ export function print(
52
+ provider: any = defaultElementTypeProvider,
53
+ template: any,
54
+ ...args: any[]
55
+ ): any {
15
56
  hiprint.init({
16
57
  providers: [new provider()],
17
58
  });
@@ -22,7 +63,11 @@ export function print (provider = defaultElementTypeProvider, template, ...args)
22
63
  return hiprintTemplate;
23
64
  }
24
65
 
25
- export function print2 (provider = defaultElementTypeProvider, template, ...args) {
66
+ export function print2(
67
+ provider: any = defaultElementTypeProvider,
68
+ template: any,
69
+ ...args: any[]
70
+ ): any {
26
71
  hiprint.init({
27
72
  providers: [new provider()],
28
73
  });
@@ -33,9 +78,10 @@ export function print2 (provider = defaultElementTypeProvider, template, ...args
33
78
  return hiprintTemplate;
34
79
  }
35
80
 
36
- export function usePaper () {
37
- const paperType = ref('A4');
38
- const paperTypeName = computed(() => {
81
+ export function usePaper() {
82
+ const hiprintStore = useHiprintStore();
83
+ const paperType = ref<string>('A4');
84
+ const paperTypeName = computed((): string => {
39
85
  const paperTypeObj = paperTypesObj.find((item) => item.value === paperType.value);
40
86
  if (paperTypeObj) {
41
87
  return paperTypeObj.label;
@@ -43,19 +89,19 @@ export function usePaper () {
43
89
  return '自定义';
44
90
  }
45
91
  });
46
- const paperWidth = ref(210);
47
- const paperHeight = ref(296.6);
48
-
49
- 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' },
92
+ const paperWidth = ref<number>(210);
93
+ const paperHeight = ref<number>(296.6);
94
+
95
+ const paperTypesObj = reactive<PaperType[]>([
96
+ {label: 'A3', value: 'A3'},
97
+ {label: 'A4', value: 'A4'},
98
+ {label: 'A5', value: 'A5'},
99
+ {label: 'B3', value: 'B3'},
100
+ {label: 'B4', value: 'B4'},
101
+ {label: 'B5', value: 'B5'},
56
102
  ]);
57
103
 
58
- const paperTypes = {
104
+ const paperTypes: Record<string, PaperDimensions> = {
59
105
  A3: {
60
106
  width: 420,
61
107
  height: 297,
@@ -82,11 +128,12 @@ export function usePaper () {
82
128
  },
83
129
  };
84
130
 
85
- function setPaper (type, callback) {
131
+ function setPaper(type: string, callback: () => void): void {
86
132
  if (type !== 'other') {
87
- let { width, height } = paperTypes[type];
133
+ const {width, height} = paperTypes[type];
88
134
  paperWidth.value = width;
89
135
  paperHeight.value = height;
136
+ hiprintStore.setPaperOption(width, height);
90
137
  }
91
138
  callback();
92
139
  }
@@ -121,22 +168,22 @@ export function usePaper () {
121
168
  };
122
169
  }
123
170
 
124
- export function useScale (callback) {
125
- const scaleValue = ref(1);
126
- const scalePercentage = computed(() => {
171
+ export function useScale(callback: () => void) {
172
+ const scaleValue = ref<number>(1);
173
+ const scalePercentage = computed((): string => {
127
174
  return `${(scaleValue.value * 100).toFixed(0)}%`;
128
175
  });
129
- const canZoomIn = computed(() => {
176
+ const canZoomIn = computed((): boolean => {
130
177
  return scaleValue.value > 0.5;
131
178
  });
132
- const canZoomOut = computed(() => {
179
+ const canZoomOut = computed((): boolean => {
133
180
  return scaleValue.value < 4;
134
181
  });
135
- function zoomIn () {
182
+ function zoomIn(): void {
136
183
  scaleValue.value = scaleValue.value - 0.1;
137
184
  callback();
138
185
  }
139
- function zoomOut () {
186
+ function zoomOut(): void {
140
187
  scaleValue.value = scaleValue.value + 0.1;
141
188
  callback();
142
189
  }
@@ -150,58 +197,67 @@ export function useScale (callback) {
150
197
  };
151
198
  }
152
199
 
153
- export function useDataSource (axios) {
154
- const detailData = ref();
155
- function getDetail (id) {
200
+ export function useDataSource(axios: AxiosInstance) {
201
+ const detailData = ref<any>();
202
+ function getDetail(id: string | number): Promise<any> {
156
203
  return axios
157
204
  .request({
158
205
  url: `/printTemplate/get/${id}`,
159
206
  method: 'post',
160
207
  })
161
- .then((res) => {
208
+ .then((res: Response) => {
162
209
  if (res.data.data && res.data.data.sourceCodes) {
163
- let codes = res.data.data.sourceCodes.split(',');
164
- let formListCode = dataSourceForm.value.map((item) => item.value);
165
- codes = codes.filter((item) => formListCode.includes(item));
166
- formCode.value = codes;
210
+ const codes = res.data.data.sourceCodes.split(',');
211
+ const formListCode = dataSourceForm.value.map((item) => item.value);
212
+ const filteredCodes = codes.filter((item) => formListCode.includes(item));
213
+ formCode.value = filteredCodes;
167
214
  } else {
168
- formCode.value = '';
215
+ formCode.value = [];
169
216
  }
170
217
  detailData.value = res.data.data;
171
218
  return Promise.resolve(res.data.data);
172
219
  });
173
220
  }
174
- const listCode = ref([]);
175
- const dataSourceList = ref([]);
176
- const listColumns = computed(() => {
177
- return codeMapDataSource.value[listCode.value].map((item) => {
178
- let index = item.indexOf('[');
221
+ const listCode = ref<string[]>([]);
222
+ const dataSourceList = ref<DataSourceOption[]>([]);
223
+ const listColumns = computed((): string[] => {
224
+ if (
225
+ !codeMapDataSource.value[listCode.value[0]] ||
226
+ !Array.isArray(codeMapDataSource.value[listCode.value[0]])
227
+ ) {
228
+ return [];
229
+ }
230
+ return codeMapDataSource.value[listCode.value[0]].map((item) => {
231
+ const index = item.indexOf('[');
179
232
  return item.substring(index + 1, item.length - 1);
180
233
  });
181
234
  });
182
235
 
183
- const formCode = ref([]);
184
- const formColumns = computed(() => {
185
- let arr = [];
236
+ const formCode = ref<string[]>([]);
237
+ const formColumns = computed((): string[] => {
238
+ const arr: string[] = [];
186
239
  if (formCode.value && formCode.value.length) {
187
240
  formCode.value.forEach((item) => {
188
- arr = arr.concat(codeMapDataSource.value[item]);
241
+ if (codeMapDataSource.value[item] && Array.isArray(codeMapDataSource.value[item])) {
242
+ arr.push(...codeMapDataSource.value[item]);
243
+ }
189
244
  });
190
245
  }
191
246
  return arr;
192
247
  });
193
- const dataSourceForm = ref([]);
194
- const codeMapDataSource = ref({});
195
- function getDataSourceList () {
248
+ const dataSourceForm = ref<DataSourceOption[]>([]);
249
+ const codeMapDataSource = ref<Record<string, string[]>>({});
250
+ function getDataSourceList(): Promise<void> {
196
251
  return axios
197
252
  .request({
198
253
  url: '/printTemplate/getDsList',
199
254
  method: 'post',
200
255
  })
201
- .then((res) => {
256
+ .then((res: Response) => {
202
257
  res.data.list.forEach((item) => {
203
258
  if (item.columns) {
204
- codeMapDataSource.value[item.code] = item?.columns?.split(',')?.map((col) => `$${item.code}[${col.toUpperCase()}]`) || [];
259
+ codeMapDataSource.value[item.code] =
260
+ item?.columns?.split(',')?.map((col) => `$${item.code}[${col.toUpperCase()}]`) || [];
205
261
  }
206
262
  if (item.type === 'LIST') {
207
263
  dataSourceList.value.push({
@@ -218,7 +274,7 @@ export function useDataSource (axios) {
218
274
  });
219
275
  }
220
276
 
221
- const dataSource = ref([]);
277
+ const dataSource = ref<any[]>([]);
222
278
  return {
223
279
  detailData,
224
280
  getDetail,
@@ -234,4 +290,10 @@ export function useDataSource (axios) {
234
290
  };
235
291
  }
236
292
 
293
+ declare global {
294
+ interface Window {
295
+ hiprint: any;
296
+ }
297
+ }
298
+
237
299
  window.hiprint = hiprint;
File without changes