gm-printer 10.14.1-alpha.7 → 10.14.1-alpha.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gm-printer",
3
- "version": "10.14.1-alpha.7",
3
+ "version": "10.14.1-alpha.8",
4
4
  "description": "diy printer",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,36 @@
1
+ import React from 'react'
2
+ import Printer from '../finance_printer'
3
+ import _ from 'lodash'
4
+ import PropTypes from 'prop-types'
5
+
6
+ class BatchPrinter extends React.Component {
7
+ constructor(props) {
8
+ super(props)
9
+ this.ready = 0
10
+ }
11
+
12
+ handleReady = () => {
13
+ this.ready++
14
+ if (this.ready === this.props.list.length) {
15
+ this.props.onReady()
16
+ }
17
+ }
18
+
19
+ render() {
20
+ const { list } = this.props
21
+ return _.map(list, ({ config, data }, i) => (
22
+ <Printer key={i} data={data} config={config} onReady={this.handleReady} />
23
+ ))
24
+ }
25
+ }
26
+
27
+ BatchPrinter.propTypes = {
28
+ list: PropTypes.array.isRequired,
29
+ onReady: PropTypes.func
30
+ }
31
+
32
+ BatchPrinter.defaultProps = {
33
+ onReady: _.noop
34
+ }
35
+
36
+ export default BatchPrinter
@@ -0,0 +1,343 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import _ from 'lodash'
4
+ import {
5
+ dispatchMsg,
6
+ getDataKey,
7
+ getHeight,
8
+ getMultiNumber,
9
+ getTableColumnName,
10
+ getWidth,
11
+ isMultiTable
12
+ } from '../../util'
13
+ import { inject, observer } from 'mobx-react'
14
+ import classNames from 'classnames'
15
+ import { MULTI_SUFFIX } from '../../config'
16
+ import SpecialTr from '../../printer/table_special_tr'
17
+ import SubtotalTr from './table_subtotal_tr'
18
+ import PageSummary from '../../printer/page_summary'
19
+
20
+ @inject('printerStore')
21
+ @observer
22
+ class Table extends React.Component {
23
+ constructor(props) {
24
+ super(props)
25
+ this.ref = React.createRef()
26
+ this.state = {
27
+ index: null
28
+ }
29
+ }
30
+
31
+ componentDidMount() {
32
+ const { name, printerStore } = this.props
33
+
34
+ if (!printerStore.ready) {
35
+ const $table = this.ref.current.querySelector('table')
36
+ const tHead = $table.querySelector('thead')
37
+ const ths = tHead.querySelectorAll('th') || []
38
+ const trs = $table.querySelectorAll('tbody tr') || []
39
+ const detailsDiv = $table.querySelectorAll('tr td .b-table-details')
40
+
41
+ printerStore.setHeight(name, getHeight($table))
42
+
43
+ printerStore.setTable(name, {
44
+ head: {
45
+ height: getHeight(tHead),
46
+ widths: _.map(ths, th => getWidth(th))
47
+ },
48
+ body: {
49
+ heights: _.map(trs, tr => getHeight(tr)),
50
+ children: _.map(detailsDiv, div => getHeight(div))
51
+ }
52
+ })
53
+ }
54
+ }
55
+
56
+ handleClick = e => {
57
+ const { name } = this.props
58
+ const { index } = e.target.dataset
59
+
60
+ dispatchMsg('gm-printer-select', {
61
+ selected: getTableColumnName(name, index)
62
+ })
63
+ }
64
+
65
+ handleDragStart = e => {
66
+ const { name } = this.props
67
+ const { index } = e.target.dataset
68
+
69
+ this.setState({
70
+ index
71
+ })
72
+
73
+ dispatchMsg('gm-printer-select', {
74
+ selected: getTableColumnName(name, index)
75
+ })
76
+ }
77
+
78
+ handleSelectedRegion = () => {
79
+ const { name } = this.props
80
+
81
+ dispatchMsg('gm-printer-select-region', { selected: name })
82
+ }
83
+
84
+ handleDrop = e => {
85
+ const { index } = e.target.dataset
86
+
87
+ if (this.state.index !== index) {
88
+ dispatchMsg('gm-printer-table-drag', {
89
+ source: this.state.index,
90
+ target: index
91
+ })
92
+ }
93
+ }
94
+
95
+ handleDragOver = e => {
96
+ e.preventDefault()
97
+ }
98
+
99
+ getColumns = () => {
100
+ const {
101
+ config: { columns, dataKey }
102
+ } = this.props
103
+ const columns1 = columns.map((val, index) => ({ ...val, index }))
104
+ // 如果是多列表格
105
+ if (isMultiTable(dataKey)) {
106
+ // 多栏商品的第二列有点特殊,都带 _MULTI_SUFFIX 后缀
107
+ let res = columns1
108
+ const colNumber = getMultiNumber(dataKey)
109
+ for (let i = 2; i <= colNumber; i++) {
110
+ const colNum = i > 2 ? i : '' // 栏数
111
+ const columnsI = columns.map((val, index) => {
112
+ const data = {
113
+ ...val,
114
+ index,
115
+ text: val.text.replace(
116
+ /{{(price\()?列\.([^{{]+)}}/g,
117
+ (s, s1, s2) => {
118
+ if (s1) {
119
+ // 有price函数插进来
120
+ const mix_suffix = s2.replace(/\*/, () => `${MULTI_SUFFIX}*`)
121
+ return `{{${s1}列.${mix_suffix}${colNum}}}`
122
+ } else {
123
+ return `{{列.${s2}${MULTI_SUFFIX}${colNum}}}`
124
+ }
125
+ }
126
+ ) // {{列.xx}} => {{列.xxMULTI_SUFFIXi}}
127
+ }
128
+ // 多栏商品的明细取 __details_MULTI_SUFFIX
129
+ if (val.specialDetailsKey)
130
+ data.specialDetailsKey =
131
+ val.specialDetailsKey + `${MULTI_SUFFIX}${colNum}`
132
+ return data
133
+ })
134
+ res = res.concat(columnsI)
135
+ }
136
+ return res
137
+ } else {
138
+ return columns1
139
+ }
140
+ }
141
+
142
+ renderDefault() {
143
+ let {
144
+ config,
145
+ config: { dataKey, arrange, customerRowHeight = 23 },
146
+ name,
147
+ range,
148
+ pageIndex,
149
+ printerStore
150
+ } = this.props
151
+
152
+ // 数据
153
+ dataKey = getDataKey(dataKey, arrange)
154
+ const tableData = printerStore.data._table[dataKey] || []
155
+
156
+ // 列
157
+ const columns = this.getColumns()
158
+ // 列宽固定(避免跳页bug)
159
+ const thWidths = printerStore.tablesInfo[name]?.head.widths || []
160
+ // 每页合计在前
161
+ const subtotalTrPageSummary = () => {
162
+ return (
163
+ <>
164
+ <SubtotalTr
165
+ range={range}
166
+ config={config}
167
+ printerStore={printerStore}
168
+ />
169
+ <PageSummary {...this.props} />
170
+ </>
171
+ )
172
+ }
173
+ // 合计在前
174
+ const PageSummarySubtotalTr = () => {
175
+ return (
176
+ <>
177
+ <PageSummary {...this.props} />
178
+ <SubtotalTr
179
+ range={range}
180
+ config={config}
181
+ printerStore={printerStore}
182
+ />
183
+ </>
184
+ )
185
+ }
186
+ const getTdStyle = (index, style = {}) => {
187
+ const width = thWidths[index]
188
+ const { fontSize } = style
189
+ let tdStyle = {}
190
+ let minWidth = 24
191
+
192
+ if (!width) return tdStyle
193
+ if (fontSize) minWidth = _.parseInt(fontSize) * 2
194
+ if (width >= 150) {
195
+ tdStyle = {
196
+ minWidth: width
197
+ }
198
+ } else {
199
+ tdStyle = {
200
+ minWidth,
201
+ width
202
+ }
203
+ }
204
+ return tdStyle
205
+ }
206
+
207
+ return (
208
+ <table>
209
+ <thead>
210
+ <tr style={{ height: `${customerRowHeight}px` }}>
211
+ {_.map(columns, (col, i) => (
212
+ <th
213
+ key={i}
214
+ data-index={col.index}
215
+ data-name={getTableColumnName(name, col.index)}
216
+ draggable
217
+ style={{
218
+ ...getTdStyle(i, col.headStyle),
219
+ ...col.headStyle
220
+ }}
221
+ className={classNames({
222
+ active:
223
+ getTableColumnName(name, col.index) ===
224
+ printerStore.selected
225
+ })}
226
+ onClick={this.handleClick}
227
+ onDragStart={this.handleDragStart}
228
+ onDrop={this.handleDrop}
229
+ onDragOver={this.handleDragOver}
230
+ >
231
+ {col.head}
232
+ </th>
233
+ ))}
234
+ </tr>
235
+ </thead>
236
+ <tbody>
237
+ {_.map(_.range(range.begin, range.end), i => {
238
+ const _special = tableData[i] && tableData[i]._special
239
+ if (_special)
240
+ return <SpecialTr key={i} config={config} data={_special} />
241
+ // 如果项为空对象展现一个占满一行的td
242
+ const isItemNone = !_.keys(tableData[i]).length
243
+
244
+ return (
245
+ <tr style={{ height: `${customerRowHeight}px` }} key={i}>
246
+ {isItemNone ? (
247
+ <td colSpan='99' />
248
+ ) : (
249
+ _.map(columns, (col, j) => {
250
+ return (
251
+ <td
252
+ key={j}
253
+ data-name={getTableColumnName(name, col.index)}
254
+ style={{
255
+ ...getTdStyle(j, col.style),
256
+ ...col.style
257
+ }}
258
+ className={classNames({
259
+ active:
260
+ getTableColumnName(name, col.index) ===
261
+ printerStore.selected
262
+ })}
263
+ dangerouslySetInnerHTML={{
264
+ __html: col.isSpecialColumn
265
+ ? printerStore.templateSpecialDetails(
266
+ col,
267
+ dataKey,
268
+ i
269
+ )
270
+ : printerStore
271
+ .templateFinance(
272
+ col.text,
273
+ dataKey,
274
+ i,
275
+ pageIndex
276
+ )
277
+ .replace(/\(\)/g, '')
278
+ }}
279
+ />
280
+ )
281
+ })
282
+ )}
283
+ </tr>
284
+ )
285
+ })}
286
+ {this.props.config.subtotal.isCommutationPlace
287
+ ? PageSummarySubtotalTr()
288
+ : subtotalTrPageSummary()}
289
+ </tbody>
290
+ </table>
291
+ )
292
+ }
293
+
294
+ renderEmptyTable() {
295
+ return (
296
+ <table>
297
+ <thead />
298
+ <tbody>
299
+ <tr />
300
+ </tbody>
301
+ </table>
302
+ )
303
+ }
304
+
305
+ render() {
306
+ let {
307
+ config: { className, dataKey, arrange },
308
+ name,
309
+ placeholder,
310
+ printerStore
311
+ } = this.props
312
+ dataKey = getDataKey(dataKey, arrange)
313
+ const tableData = printerStore.data._table[dataKey] || []
314
+ const active = printerStore.selectedRegion === name
315
+
316
+ return (
317
+ <div
318
+ ref={this.ref}
319
+ className={classNames(
320
+ 'gm-printer-table',
321
+ 'gm-printer-table-classname-' + (className || 'default'),
322
+ { active }
323
+ )}
324
+ data-name={name}
325
+ data-placeholder={placeholder}
326
+ onClick={this.handleSelectedRegion}
327
+ >
328
+ {tableData.length ? this.renderDefault() : this.renderEmptyTable()}
329
+ </div>
330
+ )
331
+ }
332
+ }
333
+
334
+ Table.propTypes = {
335
+ config: PropTypes.object.isRequired,
336
+ name: PropTypes.string.isRequired,
337
+ range: PropTypes.object.isRequired,
338
+ pageIndex: PropTypes.number.isRequired,
339
+ placeholder: PropTypes.string,
340
+ printerStore: PropTypes.object
341
+ }
342
+
343
+ export default Table
@@ -0,0 +1,147 @@
1
+ import React from 'react'
2
+ import i18next from '../../../locales'
3
+ import PropTypes from 'prop-types'
4
+ import _ from 'lodash'
5
+ import { MULTI_SUFFIX } from '../../config'
6
+ import Big from 'big.js'
7
+ import { coverDigit2Uppercase, getDataKey } from '../../util'
8
+ import { observer } from 'mobx-react'
9
+ import { get } from 'mobx'
10
+
11
+ /**
12
+ * 每页合计组件,分页计算后,根据range来统计每页合计数据
13
+ * @param props
14
+ * @returns {*}
15
+ */
16
+ const SubtotalTr = props => {
17
+ const {
18
+ config: {
19
+ dataKey,
20
+ arrange,
21
+ subtotal,
22
+ subtotal: {
23
+ show,
24
+ isUpperCaseBefore, // 决定小写金额是否在前
25
+ isUpperLowerCaseSeparate, // 决定大小写金额是否分开在两端
26
+ style,
27
+ fields = [
28
+ {
29
+ name: i18next.t('出库金额'),
30
+ valueField: 'real_item_price'
31
+ }
32
+ ],
33
+ displayName = false // 是否展示字段名
34
+ }
35
+ },
36
+ range,
37
+ printerStore: {
38
+ config: {
39
+ financeSpecialConfig: {
40
+ CapitalPrefix = '大写',
41
+ LowercasePrefix = '小写'
42
+ }
43
+ }
44
+ },
45
+ printerStore
46
+ } = props
47
+ const tableData = printerStore.data._table[getDataKey(dataKey, arrange)] || []
48
+ // 计算合计
49
+ const sumData = (list, field) => {
50
+ return _.reduce(
51
+ list,
52
+ (a, b) => {
53
+ let result = a
54
+
55
+ const _origin = b._origin || {}
56
+ const _origin2 = b['_origin' + MULTI_SUFFIX] || {}
57
+
58
+ result = a.plus(_origin[field] || 0)
59
+ if (_origin2[field]) {
60
+ result = result.plus(_origin2[field])
61
+ }
62
+ return result
63
+ },
64
+ Big(0)
65
+ ).toFixed(2)
66
+ }
67
+
68
+ // 每页小计
69
+ // show 是否展示小计,fields<Array> 合计的字段和展现的name,displayName 是否显示名字
70
+ if (show && printerStore.ready) {
71
+ const list = tableData.slice(range.begin, range.end)
72
+
73
+ const sum = {}
74
+
75
+ _.each(fields, v => {
76
+ sum[v.name] = sumData(list, v.valueField)
77
+ })
78
+
79
+ // 定义每页小记的左右两侧内容
80
+ let subtotalLeftContent
81
+ let subtotalRightContent
82
+
83
+ for (const name in sum) {
84
+ const price = sum[name]
85
+ const priceUpperCase = get(subtotal, 'needUpperCase') // needUpperCase在初始模板中undefined,所以必须用这个方法
86
+ ? coverDigit2Uppercase(price)
87
+ : ''
88
+ if (displayName) {
89
+ // 大写金额是否在前
90
+ if (isUpperCaseBefore) {
91
+ subtotalLeftContent = `${name}${priceUpperCase}`
92
+ subtotalRightContent = `(${LowercasePrefix || '小写'}:¥${price})`
93
+ } else {
94
+ subtotalLeftContent = `(${LowercasePrefix || '小写'}:¥${price})`
95
+ subtotalRightContent = `${name} ${priceUpperCase}`
96
+ }
97
+ } else {
98
+ if (isUpperCaseBefore) {
99
+ subtotalLeftContent = `${priceUpperCase}`
100
+ subtotalRightContent = `(${LowercasePrefix || '小写'}:¥${price})`
101
+ } else {
102
+ subtotalLeftContent = `(${LowercasePrefix || '小写'}:¥${price})`
103
+ subtotalRightContent = `${priceUpperCase}`
104
+ }
105
+ }
106
+ }
107
+
108
+ return (
109
+ <tr>
110
+ <td colSpan={99} style={{ ...style, height: '8mm' }}>
111
+ {/* 大写金额和小写金额分开在表格的两侧,通过控制类名实现,添加类名后,覆盖原来的居中、靠右靠左 */}
112
+ <div
113
+ className={
114
+ isUpperLowerCaseSeparate
115
+ ? 'gm-printer-subtotal-UpperLowerCaseSeparate-outer'
116
+ : ''
117
+ }
118
+ style={{ margin: '0 30px' }}
119
+ >
120
+ {i18next.t(`${CapitalPrefix || '大写'}:`)}
121
+ {/* {i18next.t('每页合计')}: */}
122
+ <span
123
+ className={
124
+ isUpperLowerCaseSeparate
125
+ ? 'gm-printer-subtotal-UpperLowerCaseSeparate-inter'
126
+ : ''
127
+ }
128
+ >
129
+ <span>&nbsp;&nbsp;{subtotalLeftContent}&nbsp;&nbsp;</span>
130
+ <span>&nbsp;&nbsp;{subtotalRightContent}&nbsp;&nbsp;</span>
131
+ </span>
132
+ </div>
133
+ </td>
134
+ </tr>
135
+ )
136
+ } else {
137
+ return null
138
+ }
139
+ }
140
+
141
+ SubtotalTr.propTypes = {
142
+ config: PropTypes.object.isRequired,
143
+ range: PropTypes.object.isRequired,
144
+ printStore: PropTypes.object
145
+ }
146
+
147
+ export default observer(SubtotalTr)
@@ -0,0 +1,312 @@
1
+ import i18next from '../../locales'
2
+ import React from 'react'
3
+ import classNames from 'classnames'
4
+ import { inject, observer, Provider } from 'mobx-react'
5
+ import PropTypes from 'prop-types'
6
+ import PrinterStore from '../printer/store'
7
+ import Page from '../printer/page'
8
+ import _ from 'lodash'
9
+ import Panel from '../printer/panel'
10
+ import Table from './components/table'
11
+ import MergePage from '../printer/merge_page'
12
+
13
+ // Header Sign Footer 相对特殊,要单独处理
14
+ const Header = props => (
15
+ <Panel {...props} name='header' placeholder={i18next.t('页眉')} />
16
+ )
17
+
18
+ const Sign = props => (
19
+ <Panel
20
+ {...props}
21
+ style={{
22
+ ...props.style,
23
+ position: 'absolute',
24
+ left: 0,
25
+ right: 0
26
+ }}
27
+ name='sign'
28
+ placeholder={i18next.t('签名')}
29
+ />
30
+ )
31
+
32
+ Sign.propTypes = {
33
+ style: PropTypes.object
34
+ }
35
+
36
+ const Footer = props => (
37
+ <Panel
38
+ {...props}
39
+ style={{
40
+ ...props.style,
41
+ position: 'absolute',
42
+ bottom: 0,
43
+ left: 0,
44
+ right: 0
45
+ }}
46
+ name='footer'
47
+ placeholder={i18next.t('页脚')}
48
+ />
49
+ )
50
+
51
+ Footer.propTypes = {
52
+ style: PropTypes.object
53
+ }
54
+
55
+ @inject('printerStore')
56
+ @observer
57
+ class Printer extends React.Component {
58
+ constructor(props) {
59
+ super(props)
60
+
61
+ // 无实际意义,辅助 onReady,见 didMount
62
+ this.state = {}
63
+
64
+ props.printerStore.init(props.config, props.data)
65
+ props.printerStore.setSelected(props.selected)
66
+ props.printerStore.setSelectedRegion(props.selectedRegion)
67
+ }
68
+
69
+ UNSAFE_componentWillReceiveProps(nextProps) {
70
+ if (nextProps.selected !== this.props.selected) {
71
+ this.props.printerStore.setSelected(nextProps.selected)
72
+ }
73
+ if (nextProps.selectedRegion !== this.props.selectedRegion) {
74
+ this.props.printerStore.setSelectedRegion(nextProps.selectedRegion)
75
+ }
76
+ }
77
+
78
+ componentDidMount() {
79
+ const { printerStore, config } = this.props
80
+ const batchPrintConfig = config.batchPrintConfig
81
+
82
+ // 连续打印不需要计算
83
+ if (batchPrintConfig !== 2) {
84
+ // didMount 代表第一次渲染完成
85
+ printerStore.setReady(true)
86
+
87
+ // 开始计算,获取各种数据
88
+ // printerStore.computedPages()
89
+ printerStore.financeComputedPages()
90
+ }
91
+ // Printer 不是立马就呈现出最终样式,有个过程。这个过程需要时间,什么 ready,不太清楚,估借 setState 来获取过程结束时刻
92
+ this.setState({}, () => {
93
+ this.props.onReady()
94
+ })
95
+ }
96
+
97
+ renderBefore() {
98
+ const { printerStore } = this.props
99
+ const { config } = printerStore
100
+
101
+ return (
102
+ <Page isFontFamily>
103
+ <Header config={config.header} pageIndex={0} />
104
+ {_.map(config.contents, (content, index) => {
105
+ switch (content.type) {
106
+ case 'table':
107
+ // eslint-disable-next-line no-case-declarations
108
+ const list = printerStore.data._table[content.dataKey]
109
+ return (
110
+ <Table
111
+ key={`contents.table.${index}`}
112
+ name={`contents.table.${index}`}
113
+ config={content}
114
+ range={{ begin: 0, end: list?.length || 0 }}
115
+ pageIndex={0}
116
+ placeholder={`${i18next.t('区域')} ${index}`}
117
+ />
118
+ )
119
+
120
+ default:
121
+ return (
122
+ <Panel
123
+ key={`contents.panel.${index}`}
124
+ name={`contents.panel.${index}`}
125
+ config={content}
126
+ pageIndex={0}
127
+ placeholder={`${i18next.t('区域')} ${index}`}
128
+ />
129
+ )
130
+ }
131
+ })}
132
+ <Sign config={config.sign} pageIndex={0} />
133
+ <Footer config={config.footer} pageIndex={0} />
134
+ </Page>
135
+ )
136
+ }
137
+
138
+ renderPage() {
139
+ const { printerStore } = this.props
140
+ const { config } = printerStore
141
+ return (
142
+ <>
143
+ {_.map(printerStore.pages, (page, i) => {
144
+ const isLastPage = i === printerStore.pages.length - 1
145
+
146
+ return (
147
+ <Page key={i} isFontFamily>
148
+ <Header config={config.header} pageIndex={i} />
149
+
150
+ {_.map(page, (panel, ii) => {
151
+ switch (panel.type) {
152
+ case 'table':
153
+ return (
154
+ <Table
155
+ key={`contents.table.${panel.index}.${ii}`}
156
+ name={`contents.table.${panel.index}`}
157
+ config={config.contents[panel.index]}
158
+ range={{
159
+ begin: panel.begin,
160
+ end: panel.end
161
+ }}
162
+ placeholder={`${i18next.t('区域')} ${panel.index}`}
163
+ pageIndex={i}
164
+ />
165
+ )
166
+
167
+ default:
168
+ return (
169
+ <Panel
170
+ key={`contents.panel.${panel.index}`}
171
+ name={`contents.panel.${panel.index}`}
172
+ config={config.contents[panel.index]}
173
+ pageIndex={i}
174
+ placeholder={`${i18next.t('区域')} ${panel.index}`}
175
+ />
176
+ )
177
+ }
178
+ })}
179
+ {isLastPage && (
180
+ <Sign
181
+ config={config.sign}
182
+ pageIndex={i}
183
+ style={{ bottom: config.footer.style.height }}
184
+ />
185
+ )}
186
+ <Footer config={config.footer} pageIndex={i} />
187
+ </Page>
188
+ )
189
+ })}
190
+ </>
191
+ )
192
+ }
193
+
194
+ renderMerge() {
195
+ const { printerStore } = this.props
196
+ const { config } = printerStore
197
+
198
+ return (
199
+ <MergePage>
200
+ <Header config={config.header} pageIndex={0} />
201
+ {_.map(config.contents, (content, index) => {
202
+ switch (content.type) {
203
+ case 'table':
204
+ // eslint-disable-next-line no-case-declarations
205
+ const list = printerStore.data._table[content.dataKey]
206
+ return (
207
+ <Table
208
+ key={`contents.table.${index}`}
209
+ name={`contents.table.${index}`}
210
+ config={content}
211
+ range={{ begin: 0, end: list?.length || 0 }}
212
+ pageIndex={0}
213
+ placeholder={`${i18next.t('区域')} ${index}`}
214
+ />
215
+ )
216
+
217
+ default:
218
+ return (
219
+ <Panel
220
+ key={`contents.panel.${index}`}
221
+ name={`contents.panel.${index}`}
222
+ config={content}
223
+ pageIndex={0}
224
+ placeholder={`${i18next.t('区域')} ${index}`}
225
+ />
226
+ )
227
+ }
228
+ })}
229
+ <Sign config={config.sign} pageIndex={0} />
230
+ <Footer config={config.footer} pageIndex={0} />
231
+ </MergePage>
232
+ )
233
+ }
234
+
235
+ doRender() {
236
+ const { printerStore, config } = this.props
237
+ // batchPrintConfig: 1 不连续打印(纸张会间断)2 连续打印(纸张连续打,不间断)
238
+ const batchPrintConfig = config.batchPrintConfig
239
+ if (batchPrintConfig === 2) {
240
+ return this.renderMerge()
241
+ } else {
242
+ // renderBefore ,拿到各种数据,哪些模块哪些内容放合适位置,切割表格
243
+ // renderPage,最终渲染打印的页面
244
+ return !printerStore.ready ? this.renderBefore() : this.renderPage()
245
+ }
246
+ }
247
+
248
+ render() {
249
+ const {
250
+ selected,
251
+ data,
252
+ config,
253
+ onReady,
254
+ selectedRegion,
255
+ printerStore,
256
+ ...rest
257
+ } = this.props
258
+
259
+ const {
260
+ size: { width },
261
+ className,
262
+ style
263
+ } = printerStore.config.page
264
+ // batchPrintConfig: 1 不连续打印(纸张会间断)2 连续打印(纸张连续打,不间断)
265
+ const batchPrintConfig = config.batchPrintConfig
266
+
267
+ return (
268
+ <div
269
+ {...rest}
270
+ className={classNames('gm-printer', className)}
271
+ style={Object.assign({}, style, {
272
+ width,
273
+ breakAfter: batchPrintConfig === 2 ? 'auto' : 'always'
274
+ })}
275
+ >
276
+ {this.doRender()}
277
+ </div>
278
+ )
279
+ }
280
+ }
281
+
282
+ Printer.propTypes = {
283
+ className: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
284
+ style: PropTypes.object,
285
+ printerStore: PropTypes.object,
286
+ selected: PropTypes.string,
287
+ selectedRegion: PropTypes.string,
288
+ data: PropTypes.object.isRequired,
289
+ config: PropTypes.object.isRequired,
290
+ onReady: PropTypes.func
291
+ }
292
+
293
+ Printer.defaultProps = {
294
+ onReady: _.noop
295
+ }
296
+
297
+ class WithStorePrinter extends React.Component {
298
+ constructor(props) {
299
+ super(props)
300
+ this.printerStore = new PrinterStore()
301
+ }
302
+
303
+ render() {
304
+ return (
305
+ <Provider printerStore={this.printerStore}>
306
+ <Printer {...this.props} />
307
+ </Provider>
308
+ )
309
+ }
310
+ }
311
+
312
+ export default WithStorePrinter
package/src/index.js CHANGED
@@ -16,7 +16,8 @@ import {
16
16
  doPrint,
17
17
  Printer,
18
18
  getCSS,
19
- getPrintContainerHTML
19
+ getPrintContainerHTML,
20
+ doBatchFinancePrint
20
21
  } from './printer'
21
22
  import { MULTI_SUFFIX, MULTI_SUFFIX3 } from './config'
22
23
 
@@ -41,5 +42,6 @@ export {
41
42
  getCSS,
42
43
  MULTI_SUFFIX,
43
44
  MULTI_SUFFIX3,
44
- getPrintContainerHTML
45
+ getPrintContainerHTML,
46
+ doBatchFinancePrint
45
47
  }
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'
3
3
  import Printer from './printer'
4
4
  import getCSS from './get_css'
5
5
  import BatchPrinter from './batch_printer'
6
+ import BatchFinancePrinter from '../finance_voucher/components/finance_batch_printer'
6
7
  import { isZoom } from 'gm-util'
7
8
  import { afterImgAndSvgLoaded } from '../util'
8
9
 
@@ -92,6 +93,25 @@ function toDoPrintBatch(list, isPrint = true) {
92
93
  })
93
94
  }
94
95
 
96
+ function toDoPrintFinanceBatch(list, isPrint = true) {
97
+ return new window.Promise(resolve => {
98
+ const $app = $printer.contentWindow.document.getElementById('appContainer')
99
+ ReactDOM.unmountComponentAtNode($app)
100
+ ReactDOM.render(
101
+ <BatchFinancePrinter
102
+ list={list}
103
+ onReady={() => {
104
+ afterImgAndSvgLoaded(() => {
105
+ isPrint && $printer.contentWindow.print()
106
+ resolve()
107
+ }, $app)
108
+ }}
109
+ />,
110
+ $app
111
+ )
112
+ })
113
+ }
114
+
95
115
  function doPrint({ data, config }, isTest) {
96
116
  init({ isTest })
97
117
 
@@ -107,6 +127,16 @@ function doBatchPrint(
107
127
  return toDoPrintBatch(list, extraCofnig.isPrint)
108
128
  }
109
129
 
130
+ function doBatchFinancePrint(
131
+ list,
132
+ isTest,
133
+ extraCofnig = { isPrint: true, isTipZoom: true }
134
+ ) {
135
+ init({ isTest, isTipZoom: extraCofnig.isTipZoom })
136
+
137
+ return toDoPrintFinanceBatch(list, extraCofnig.isPrint)
138
+ }
139
+
110
140
  /**
111
141
  * @description: 获取打印的html
112
142
  */
@@ -114,4 +144,4 @@ function getPrintContainerHTML() {
114
144
  return $printer.contentWindow.document.getElementsByTagName('html')[0]
115
145
  .innerHTML
116
146
  }
117
- export { doPrint, doBatchPrint, getPrintContainerHTML }
147
+ export { doPrint, doBatchPrint, getPrintContainerHTML, doBatchFinancePrint }
@@ -1,6 +1,11 @@
1
1
  import Printer from './printer'
2
2
  import BatchPrinter from './batch_printer'
3
- import { doPrint, doBatchPrint, getPrintContainerHTML } from './do_print'
3
+ import {
4
+ doPrint,
5
+ doBatchPrint,
6
+ getPrintContainerHTML,
7
+ doBatchFinancePrint
8
+ } from './do_print'
4
9
  import getCSS from './get_css'
5
10
 
6
11
  export {
@@ -9,5 +14,6 @@ export {
9
14
  Printer,
10
15
  BatchPrinter,
11
16
  getCSS,
12
- getPrintContainerHTML
17
+ getPrintContainerHTML,
18
+ doBatchFinancePrint
13
19
  }
@@ -2,6 +2,7 @@ import React from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import { inject, observer } from 'mobx-react'
4
4
  import { getHeight } from '../util'
5
+ import classNames from 'classnames'
5
6
 
6
7
  @inject('printerStore')
7
8
  @observer
@@ -18,13 +19,14 @@ class Page extends React.Component {
18
19
  }
19
20
 
20
21
  render() {
21
- const { children, printerStore } = this.props
22
+ const { children, printerStore, isFontFamily } = this.props
22
23
  const {
23
24
  paddingTop,
24
25
  paddingRight,
25
26
  paddingBottom,
26
27
  paddingLeft
27
28
  } = printerStore.config.page.gap
29
+ console.log('isFontFamily', isFontFamily)
28
30
 
29
31
  const {
30
32
  size: { width, height },
@@ -36,7 +38,9 @@ class Page extends React.Component {
36
38
  return (
37
39
  <div
38
40
  ref={this.ref}
39
- className='gm-printer-page'
41
+ className={classNames('gm-printer-page', {
42
+ 'gm-printer-page-fontFamily': isFontFamily
43
+ })}
40
44
  style={{
41
45
  ...pageStyle,
42
46
  boxSizing: 'content-box',
@@ -60,7 +64,8 @@ class Page extends React.Component {
60
64
  }
61
65
 
62
66
  Page.propTypes = {
63
- printerStore: PropTypes.number
67
+ printerStore: PropTypes.number,
68
+ isFontFamily: PropTypes.bool
64
69
  }
65
70
 
66
71
  export default Page
@@ -397,6 +397,46 @@ class PrinterStore {
397
397
  this.remainPageHeight = +Big(this.pageHeight - currentPageHeight).toFixed(0)
398
398
  }
399
399
 
400
+ @action
401
+ financeComputedPages() {
402
+ // 当前在处理 contents 的索引
403
+ let index = 0
404
+ /** 一页承载的内容. [object, object, ...] */
405
+ let page = []
406
+ const pageFixLineNum = this.config.financeSpecialConfig.pageFixLineNum
407
+ while (index < this.config.contents.length) {
408
+ const content = this.config.contents[index]
409
+ // debugger
410
+ const table = this.tablesInfo[`contents.table.${index}`]
411
+ // 表格行的索引,用于table.slice(begin, end), 分割到不同页面中
412
+ let begin = 0
413
+ let end = 0
414
+
415
+ if (content.type === 'table') {
416
+ while (end < table.body.heights.length) {
417
+ end += pageFixLineNum
418
+ page.push({
419
+ type: 'table',
420
+ index,
421
+ begin,
422
+ end
423
+ })
424
+ this.pages.push(page)
425
+ page = []
426
+ begin = end
427
+ }
428
+ index++
429
+ } else {
430
+ page.push({
431
+ type: 'panel',
432
+ index
433
+ })
434
+ index++
435
+ }
436
+ }
437
+ // this.pages.push(page)
438
+ }
439
+
400
440
  template(text, pageIndex) {
401
441
  // 做好保护,出错就返回 text
402
442
  try {
@@ -432,6 +472,33 @@ class PrinterStore {
432
472
  }
433
473
  }
434
474
 
475
+ /**
476
+ * 配送单的定制化需求:限制表格里的字的个数,超出显示'...'
477
+ */
478
+ templateFinance(text, dataKey, index, pageIndex) {
479
+ // 做好保护,出错就返回 text
480
+ try {
481
+ const list = this.data._table[dataKey] || this.data._table.orders
482
+
483
+ let res = _.template(text, {
484
+ interpolate: /{{([\s\S]+?)}}/g
485
+ })({
486
+ ...this.data.common,
487
+ [i18next.t('列')]: list[index],
488
+ [i18next.t('当前页码')]: pageIndex + 1,
489
+ [i18next.t('页码总数')]: this.pages.length,
490
+ price: price // 提供一个价格处理函数
491
+ })
492
+ res =
493
+ text === '{{列.商品名}}' && res.length > 5
494
+ ? `${res.slice(0, 5)}...`
495
+ : res
496
+ return res
497
+ } catch (err) {
498
+ return text
499
+ }
500
+ }
501
+
435
502
  templateSpecialDetails(col, dataKey, index) {
436
503
  // 做好保护,出错就返回 text
437
504
  const { specialDetailsKey, text, detailLastColType, separator } = col
@@ -28,6 +28,10 @@
28
28
  }
29
29
  }
30
30
 
31
+ .gm-printer-page-fontFamily {
32
+ font-family:STSong
33
+ }
34
+
31
35
  // panel
32
36
  .gm-printer-panel {
33
37
  position: relative;
package/src/util.js CHANGED
@@ -141,9 +141,7 @@ const coverDigit2Uppercase = n => {
141
141
  )
142
142
  ] + fraction[i]
143
143
  }
144
-
145
- right = right.replace(/(零分)/, '整').replace(/(零角整)/, '') || '整'
146
-
144
+ right = right.replace(/(零分)/, '').replace(/(零角)/, '零')
147
145
  n = Math.floor(n)
148
146
 
149
147
  for (i = 0; i < unit[0].length && n > 0; i++) {
@@ -157,10 +155,10 @@ const coverDigit2Uppercase = n => {
157
155
 
158
156
  return (
159
157
  head +
160
- (left.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零') + right).replace(
161
- /^整$/,
162
- '零元整'
163
- )
158
+ (
159
+ left.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零') +
160
+ (right === '零' ? '整' : right)
161
+ ).replace(/^整$/, '零元整')
164
162
  )
165
163
  }
166
164