gm-printer-label 3.0.7-beta0 → 3.0.9-alpha.0

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/.eslintcache CHANGED
@@ -1 +1 @@
1
- [{"/Users/yer/work/gm-printer-label/src/printer/block.js":"1","/Users/yer/work/gm-printer-label/src/common/editor_compoents/store.js":"2","/Users/yer/work/gm-printer-label/src/common/editor_compoents/edit_modify_filed.js":"3","/Users/yer/work/gm-printer-label/src/common/editor_compoents/edit_add_filed.js":"4","/Users/yer/work/gm-printer-label/src/util.js":"5"},{"size":7484,"mtime":1621234660871,"results":"6","hashOfConfig":"7"},{"size":6800,"mtime":1621594263392,"results":"8","hashOfConfig":"7"},{"size":4556,"mtime":1621826319817,"results":"9","hashOfConfig":"7"},{"size":1583,"mtime":1621234282316,"results":"10","hashOfConfig":"7"},{"size":1259,"mtime":1624965538689},{"filePath":"11","messages":"12","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"cmqj6t",{"filePath":"13","messages":"14","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"15","messages":"16","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"17","messages":"18","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/yer/work/gm-printer-label/src/printer/block.js",[],"/Users/yer/work/gm-printer-label/src/common/editor_compoents/store.js",[],"/Users/yer/work/gm-printer-label/src/common/editor_compoents/edit_modify_filed.js",[],"/Users/yer/work/gm-printer-label/src/common/editor_compoents/edit_add_filed.js",[]]
1
+ [{"/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/util.js":"1","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/edit_select.js":"2","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/edit.js":"3","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/component.js":"4","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/qrcode.js":"5","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/printer.js":"6","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/index.js":"7","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/block.js":"8","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/batch_printer.js":"9","/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/printer/barcode.js":"10"},{"size":1934,"mtime":1642494468444,"results":"11","hashOfConfig":"12"},{"size":5505,"mtime":1635767717933,"results":"13","hashOfConfig":"12"},{"size":2694,"mtime":1635767728834,"results":"14","hashOfConfig":"12"},{"size":11621,"mtime":1635767876018,"results":"15","hashOfConfig":"12"},{"size":394,"mtime":1642488443965},{"size":1594,"mtime":1642499397285},{"size":3064,"mtime":1642499124287},{"size":9350,"mtime":1642499166536},{"size":825,"mtime":1642499158040},{"size":525,"mtime":1642488487262},{"filePath":"16","messages":"17","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"18d3ysg",{"filePath":"18","messages":"19","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"20","messages":"21","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"22","messages":"23","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/util.js",[],"/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/edit_select.js",[],"/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/edit.js",[],"/Users/min/Documents/GMFE/gm-util/gm-printer-label/src/common/editor_compoents/component.js",[]]
package/.prettierrc.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ semi: false,
3
+ singleQuote: true,
4
+ jsxSingleQuote: true,
5
+ };
package/README.md CHANGED
@@ -1,7 +1,62 @@
1
- import { PrinterEdit } from 'gm-printer-label/src/edit'
2
- import { Printer, getHtml, getCSS } from 'gm-printer-label/src/printer'
1
+ ## 介绍
3
2
 
3
+ gm-printer-label是分拣标签和预分拣标签的组件库
4
4
 
5
+ ```js
6
+ export * from './editor_label'//分拣标签
7
+ export * from './editor_presorting' // 预分拣标签
8
+ ```
9
+
10
+ ## 启动
11
+
12
+ 1. yarn,安装依赖
13
+ 2. yarn start 启动项目
14
+
15
+ ## 使用
16
+
17
+ 新老架构和工位屏都有使用
18
+
19
+ ## 与 erp 进行连接测试
20
+
21
+ 1. 在 gm-printer-label 中,使用`yarn link`
22
+ 2. 在 gm-static-stationv2 中使用`yarn link 'gm-printer-label'`,这样就连接上了 gm-printer-label 组件库,改动组件库的时候,就可以在 gm-static-stationv2 看到效果
23
+ 3. 断开连接,先在 gm-printer-label 中使用`yarn unlink`,在 gm-static-stationv2 中运行`yarn unlink 'gm-printer-label'`,重新 yarn 和 yarn start 一下
24
+ 4. 可能断开连接不太好用,不好用的时候,删除 node_modeles,关闭 gm-x-printer,重新 yarn
25
+ 5. yarn link 不太稳定
26
+
27
+ ## 版本命名规则
28
+
29
+ 1. GNU 风格的版本号命名格式:主版本号 . 子版本号 [. 修正版本号 [. 编译版本号 ]],示例 : 1.2.1 (正式版本), 1.2.1-beta.0(测试版本)
30
+ 2. 版本号管理策略:
31
+ 1. 项目最初版本为 1.0.0
32
+ 2. 当项目在进行了局部修改或 bug 修正时,主版本号和子版本号都不变,修正版本号加 1;示例:1.0.1
33
+ 3. 当项目在原有的基础上增加了部分功能时,主版本号不变,子版本号加 1,修正版本号复位为 0。示例:1.1.0
34
+ 4. 当项目在进行了重大修改或局部修正累积较多,而导致项目整体发生全局变化时,主版本号加 1.示例:2.1.0
35
+
36
+ ## 版本发布
37
+
38
+ 首先先切换到 npm 的环境
39
+
40
+ 1. 没有登陆过,使用`npm login`登陆,一次登陆永久使用,登陆的时候会输入密码,用户名,邮箱(公司有)
41
+
42
+ 2. beta 版本包的版本发布(用来测试该版本,解决该版本的 bug)
43
+
44
+ 1. 将`pageage.json`中的`version`修改为`X.X.X-beta.0`,或者运行 `npm version X.X.X-beta.0`来更新`package.json`,同时创建一个 git 标签 (请参考 https://docs.npmjs.com/cli/version)。
45
+
46
+ 在你的版本末尾添加 `beta.0` 非常重要。`.0` 表示它是哪个版本。当我们对 `beta` 版进行修补发布新的 `beta` 版本时,我们会将 `.0` 递增到 `.1`,以此类推。
47
+
48
+ 2. ⚠️ 将该版本的包 push 到远程仓库
49
+
50
+ 3. 使用`npm publish --tag beta`发布测试版本
51
+
52
+ 4. 对版本进行修补时,只需要将`beta.0`递增到`beta.1`进行版本发布即可,以此类推
53
+
54
+ 3. 正式版本的发布
55
+
56
+ 1. 遵循上面的版本命名规则,修改`pageage.json`中的`version`版本号,或者运行 `npm version X.X.X`来更新`package.json`,同时创建一个 git 标签
57
+ 2. ⚠️ 将正式版本 push 到远程仓库
58
+ 3. 使用`npm publish`发布正式版本
59
+ 4. 在自己的分支上进行发布版本即可,发完后,将分支合并到 master 上!!!
5
60
 
6
61
  ## 依赖
7
62
  react react-dom
@@ -12,4 +67,5 @@ big.js
12
67
  moment
13
68
 
14
69
  ## edit 额外依赖
15
- clipboard
70
+ clipboard
71
+
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "gm-printer-label",
3
- "version": "3.0.7-beta0",
3
+ "version": "3.0.9-alpha.0",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
7
  "precommit": "lint-staged",
8
- "start": "webpack-dev-server"
8
+ "start": "webpack-dev-server",
9
+ "publish-latest": "npm publish --force-publish --registry https://registry.npmjs.org/",
10
+ "publish-beta": "npm publish --force-publish --dist-tag beta --preid beta --registry https://registry.npmjs.org/",
11
+ "publish-alpha": "npm publish --force-publish --dist-tag alpha --preid alpha --registry https://registry.npmjs.org/"
9
12
  },
10
13
  "lint-staged": {
11
14
  "./src/**/*.js": [
@@ -29,7 +32,8 @@
29
32
  "lodash": "^4.17.10",
30
33
  "moment": "^2.22.2",
31
34
  "react": "^16.8.5",
32
- "react-dom": "^16.8.5"
35
+ "react-dom": "^16.8.5",
36
+ "qrcode.react": "^0.9.2"
33
37
  },
34
38
  "dependencies": {
35
39
  "clipboard": "^2.0.1",
@@ -37,7 +41,8 @@
37
41
  "gm-util": "^5.10.4",
38
42
  "mobx": "^4.3.0",
39
43
  "mobx-react": "^6.1.1",
40
- "precss": "^4.0.0"
44
+ "precss": "^4.0.0",
45
+ "qrcode.react": "^0.9.2"
41
46
  },
42
47
  "devDependencies": {
43
48
  "@babel/cli": "^7.6.0",
@@ -86,6 +91,7 @@
86
91
  "url-loader": "^1.0.1",
87
92
  "webpack": "^4.10.1",
88
93
  "webpack-cli": "^3.3.0",
89
- "webpack-dev-server": "^3.1.4"
94
+ "webpack-dev-server": "^3.1.4",
95
+ "jsbarcode": "^3.11.0"
90
96
  }
91
97
  }
@@ -401,7 +401,12 @@ class ImageUploader extends React.Component {
401
401
  Tip.warning(i18next.t('图片大小不能超过500Kb'))
402
402
  return
403
403
  }
404
-
404
+ if (this.props.uploadQiniuImage) {
405
+ this.props.uploadQiniuImage(file).then((json) => {
406
+ this.props.onSuccess(json.data.url)
407
+ })
408
+ return
409
+ }
405
410
  const STATION_URL = {
406
411
  reqUrl: '/station/image/upload',
407
412
  imgUrl: 'http://img.guanmai.cn/station_pic'
@@ -452,7 +457,8 @@ class ImageUploader extends React.Component {
452
457
 
453
458
  ImageUploader.propTypes = {
454
459
  onSuccess: PropTypes.func.isRequired,
455
- text: PropTypes.string.isRequired
460
+ text: PropTypes.string.isRequired,
461
+ uploadTengXunFile: PropTypes.func
456
462
  }
457
463
 
458
464
  export {
@@ -22,7 +22,7 @@ class Edit extends React.Component {
22
22
  }
23
23
 
24
24
  render () {
25
- const { data, initDefaultTemp, defaultTempList, addFields, onSave, editStore, insertBlocksConfig } = this.props
25
+ const { data, initDefaultTemp, defaultTempList, addFields, onSave, editStore, insertBlocksConfig, uploadQiniuImage } = this.props
26
26
  return (
27
27
  <div className='gm-printer-label-edit'>
28
28
 
@@ -43,6 +43,7 @@ class Edit extends React.Component {
43
43
  insertBlocksConfig={insertBlocksConfig}
44
44
  initDefaultTemp={initDefaultTemp}
45
45
  defaultTempList={defaultTempList}
46
+ uploadQiniuImage={uploadQiniuImage}
46
47
  />
47
48
  <Gap height='5px'/>
48
49
  <EditModifyFiled/>
@@ -62,7 +62,7 @@ class EditSelect extends React.Component {
62
62
  }
63
63
 
64
64
  render () {
65
- const { defaultTempList, editStore, insertBlocksConfig } = this.props
65
+ const { defaultTempList, editStore, insertBlocksConfig, uploadQiniuImage } = this.props
66
66
  const {
67
67
  tempKey,
68
68
  config: { name, page }
@@ -160,6 +160,7 @@ class EditSelect extends React.Component {
160
160
  }
161
161
  key={v.value}
162
162
  text={v.text}
163
+ uploadQiniuImage={uploadQiniuImage}
163
164
  />
164
165
  </button>
165
166
  ) : (
@@ -0,0 +1,26 @@
1
+ import React from 'react'
2
+ import jsBarcode from 'jsbarcode'
3
+ import PropTypes from 'prop-types'
4
+
5
+ class BarCode extends React.Component {
6
+ barcode = React.createRef()
7
+
8
+ componentDidMount () {
9
+ const { value, ...rest } = this.props
10
+ jsBarcode(this.barcode.current, value, {
11
+ ...rest
12
+ })
13
+ }
14
+
15
+ render () {
16
+ const { dataName } = this.props
17
+ return <svg data-name={dataName} ref={this.barcode}/>
18
+ }
19
+ }
20
+
21
+ BarCode.propTypes = {
22
+ value: PropTypes.string,
23
+ dataName: PropTypes.string
24
+ }
25
+
26
+ export default BarCode
@@ -0,0 +1,43 @@
1
+ import React from 'react'
2
+ import Printer from './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, isStation } = this.props
21
+ return _.map(list, ({ config, data }, i) => (
22
+ <Printer
23
+ key={i}
24
+ data={data}
25
+ config={config}
26
+ onReady={this.handleReady}
27
+ isStation={isStation}
28
+ />
29
+ ))
30
+ }
31
+ }
32
+
33
+ BatchPrinter.propTypes = {
34
+ list: PropTypes.array.isRequired,
35
+ onReady: PropTypes.func,
36
+ isStation: PropTypes.bool
37
+ }
38
+
39
+ BatchPrinter.defaultProps = {
40
+ onReady: _.noop
41
+ }
42
+
43
+ export default BatchPrinter
@@ -2,8 +2,10 @@ import React from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import classNames from 'classnames'
4
4
  import { observer } from 'mobx-react'
5
- import { getStyleWithDiff, dispatchMsg, template } from '../util'
5
+ import { getStyleWithDiff, dispatchMsg, template, miniAppLink } from '../util'
6
6
  import TableType from './components/table_type'
7
+ import BarCode from './barcode'
8
+ import QrCode from './qrcode'
7
9
 
8
10
  @observer
9
11
  class Block extends React.Component {
@@ -31,7 +33,7 @@ class Block extends React.Component {
31
33
  }
32
34
  })
33
35
  )
34
- };
36
+ }
35
37
 
36
38
  handleDragEnd = ({ clientX, clientY }) => {
37
39
  const { config } = this.props
@@ -43,7 +45,7 @@ class Block extends React.Component {
43
45
  dispatchMsg('gm-printer-label-block-style-set', {
44
46
  style
45
47
  })
46
- };
48
+ }
47
49
 
48
50
  handleClick = () => {
49
51
  const { index } = this.props
@@ -51,7 +53,7 @@ class Block extends React.Component {
51
53
  dispatchMsg('gm-printer-label-select', {
52
54
  selected: index
53
55
  })
54
- };
56
+ }
55
57
 
56
58
  handleDoubleClick = () => {
57
59
  const {
@@ -67,19 +69,19 @@ class Block extends React.Component {
67
69
  }
68
70
  )
69
71
  }
70
- };
72
+ }
71
73
 
72
74
  handleEditBlur = () => {
73
75
  this.setState({
74
76
  isEdit: false
75
77
  })
76
- };
78
+ }
77
79
 
78
80
  handleText = (e) => {
79
81
  dispatchMsg('gm-printer-label-block-text-set', {
80
82
  text: e.target.value
81
83
  })
82
- };
84
+ }
83
85
 
84
86
  render () {
85
87
  const {
@@ -102,6 +104,7 @@ class Block extends React.Component {
102
104
  },
103
105
  data,
104
106
  className,
107
+ isStation,
105
108
  ...rest
106
109
  } = this.props
107
110
  const { isEdit } = this.state
@@ -122,9 +125,11 @@ class Block extends React.Component {
122
125
  } else if (type === 'line') {
123
126
  content = null
124
127
  } else if (type === 'qrcode') {
125
- content = (
128
+ content = isStation ? (
129
+ <QrCode value={template(qrcode, data)} size={parseInt(style.height)}/>
130
+ ) : (
126
131
  <div
127
- data-qrcode={template(qrcode, data)}
132
+ data-qrcode={miniAppLink + template(qrcode, data)}
128
133
  data-width={style.width}
129
134
  data-height={style.height}
130
135
  data-name={index}
@@ -133,7 +138,12 @@ class Block extends React.Component {
133
138
  />
134
139
  )
135
140
  } else if (type === 'order_qrcode') {
136
- content = (
141
+ content = isStation ? (
142
+ <QrCode
143
+ value={miniAppLink + template(order_qrcode, data)}
144
+ size={parseInt(style.height)}
145
+ />
146
+ ) : (
137
147
  <div
138
148
  data-orderQrcode={template(order_qrcode, data)}
139
149
  data-width={style.width}
@@ -144,7 +154,18 @@ class Block extends React.Component {
144
154
  />
145
155
  )
146
156
  } else if (type === 'barcode') {
147
- content = (
157
+ content = isStation ? (
158
+ <BarCode
159
+ value={template(barcode, data)}
160
+ textMargin={0}
161
+ margin={0}
162
+ height={parseInt(style.height) - 14}
163
+ width={2}
164
+ displayValue={false}
165
+ dataName={barcode}
166
+ background='transparent'
167
+ />
168
+ ) : (
148
169
  <div
149
170
  data-packagecode='验货条形码'
150
171
  data-name={index}
@@ -161,7 +182,18 @@ class Block extends React.Component {
161
182
  </div>
162
183
  )
163
184
  } else if (type === 'in_stock_barcode') {
164
- content = (
185
+ content = isStation ? (
186
+ <BarCode
187
+ value={template(in_stock_barcode, data)}
188
+ textMargin={0}
189
+ margin={0}
190
+ height={parseInt(style.height) - 14}
191
+ width={2}
192
+ displayValue={false}
193
+ dataName={in_stock_barcode}
194
+ background='transparent'
195
+ />
196
+ ) : (
165
197
  <div
166
198
  data-instockcode='入库条码'
167
199
  data-name={index}
@@ -178,7 +210,18 @@ class Block extends React.Component {
178
210
  </div>
179
211
  )
180
212
  } else if (type === 'production_barcode') {
181
- content = (
213
+ content = isStation ? (
214
+ <BarCode
215
+ value={template(production_barcode, data)}
216
+ textMargin={0}
217
+ margin={0}
218
+ height={parseInt(style.height) - 14}
219
+ width={2}
220
+ displayValue={false}
221
+ dataName={production_barcode}
222
+ background='transparent'
223
+ />
224
+ ) : (
182
225
  <div
183
226
  data-productionbarcode='商品条码'
184
227
  data-name={index}
@@ -195,7 +238,12 @@ class Block extends React.Component {
195
238
  </div>
196
239
  )
197
240
  } else if (type === 'package_id_qrcode') {
198
- content = (
241
+ content = isStation ? (
242
+ <QrCode
243
+ value={template(package_id_qrcode, data)}
244
+ size={parseInt(style.height)}
245
+ />
246
+ ) : (
199
247
  <div
200
248
  data-packageqrcode={template(package_id_qrcode, data)}
201
249
  data-width={style.width}
@@ -206,7 +254,18 @@ class Block extends React.Component {
206
254
  />
207
255
  )
208
256
  } else if (type === 'diycode') {
209
- content = (
257
+ content = isStation ? (
258
+ <BarCode
259
+ value={template(diycode, data)}
260
+ textMargin={0}
261
+ margin={0}
262
+ height={35}
263
+ width={2}
264
+ displayValue={false}
265
+ dataName={'diycode'}
266
+ background='transparent'
267
+ />
268
+ ) : (
210
269
  <div
211
270
  data-diycode='自定义编码条形码'
212
271
  data-name={index}
@@ -223,7 +282,9 @@ class Block extends React.Component {
223
282
  </div>
224
283
  )
225
284
  } else if (type === 'diyqrcode') {
226
- content = (
285
+ content = isStation ? (
286
+ <QrCode value={template(text, data)} size={parseInt(style.height)}/>
287
+ ) : (
227
288
  <div
228
289
  data-diyqrcode={template(text, data)}
229
290
  data-width={style.width}
@@ -284,7 +345,8 @@ Block.propTypes = {
284
345
  index: PropTypes.number.isRequired,
285
346
  config: PropTypes.object.isRequired,
286
347
  data: PropTypes.object.isRequired,
287
- selected: PropTypes.number
348
+ selected: PropTypes.number,
349
+ isStation: PropTypes.bool
288
350
  }
289
351
 
290
352
  export default Block
@@ -2,11 +2,15 @@ import React from 'react'
2
2
  import ReactDOMServer from 'react-dom/server'
3
3
  import Printer from './printer'
4
4
  import _ from 'lodash'
5
+ import { isZoom } from 'gm-util'
6
+ import ReactDOM from 'react-dom'
7
+ import BatchPrinter from './batch_printer'
5
8
 
6
9
  // eslint-disable-next-line
7
- import normalizeCSS from "css-loader!./normalize.csss";
10
+ import normalizeCSS from 'css-loader!./normalize.csss'
8
11
  // eslint-disable-next-line
9
- import printerCSS from "css-loader!postcss-loader!less-loader!./style.lesss";
12
+ import printerCSS from 'css-loader!postcss-loader!less-loader!./style.lesss'
13
+ import { afterImgAndSvgLoaded } from '../util'
10
14
 
11
15
  function getHtml (props) {
12
16
  return ReactDOMServer.renderToString(<Printer {...props}/>)
@@ -26,4 +30,82 @@ function getCSS () {
26
30
  return normalizeCSS.toString() + printerCSS.toString()
27
31
  }
28
32
 
29
- export { getHtml, getBatchHtml, getCSS, Printer }
33
+ const printerId = '_gm-printer_' + Math.random()
34
+ let $printer = window.document.getElementById(printerId)
35
+
36
+ /**
37
+ * @param {boolean} isTest 是否test
38
+ * @param {boolean} isTipZoom zoom的时候是否提示
39
+ */
40
+ function init ({ isTest, isTipZoom = true } = {}) {
41
+ isTipZoom &&
42
+ isZoom() &&
43
+ window.alert(
44
+ '检测您的浏览器使用了缩放,为了避免影响打印布局,请重置缩放到100%后再进行打印'
45
+ )
46
+ if (!$printer) {
47
+ $printer = window.document.createElement('iframe')
48
+ $printer.id = printerId
49
+ $printer.style.position = 'fixed'
50
+ $printer.style.top = '0'
51
+ $printer.style.width = '100%' // 使移动端可滚动
52
+ if (isTest) {
53
+ // 模板编辑[测试打印],隐藏起来
54
+ // $printer.style.left = '-2000px'
55
+ } else {
56
+ $printer.style.left = '0px'
57
+ $printer.style.height = '100vh'
58
+ }
59
+ window.document.body.appendChild($printer)
60
+
61
+ const idocument = $printer.contentDocument
62
+ idocument.open()
63
+ idocument.write('<!DOCTYPE html><html><head></head><body></body></html>')
64
+ idocument.close()
65
+
66
+ const doc = $printer.contentWindow.document
67
+
68
+ const style = doc.createElement('style')
69
+ style.type = 'text/css'
70
+ style.appendChild(doc.createTextNode(getCSS()))
71
+ doc.head.appendChild(style)
72
+
73
+ const div = doc.createElement('div')
74
+ div.id = 'appContainer'
75
+
76
+ doc.body.appendChild(div)
77
+ }
78
+ }
79
+
80
+ function doBatchPrint (
81
+ list,
82
+ isTest,
83
+ extraCofnig = { isPrint: true, isTipZoom: true },
84
+ isStation = false
85
+ ) {
86
+ init({ isTest, isTipZoom: extraCofnig.isTipZoom })
87
+
88
+ return toDoPrintBatch(list, extraCofnig.isPrint, isStation)
89
+ }
90
+
91
+ function toDoPrintBatch (list, isPrint = true, isStation = false) {
92
+ return new window.Promise((resolve) => {
93
+ const $app = $printer.contentWindow.document.getElementById('appContainer')
94
+
95
+ ReactDOM.unmountComponentAtNode($app)
96
+ ReactDOM.render(
97
+ <BatchPrinter
98
+ list={list}
99
+ isStation={isStation}
100
+ onReady={() => {
101
+ afterImgAndSvgLoaded(() => {
102
+ isPrint && $printer.contentWindow.print()
103
+ resolve()
104
+ }, $app)
105
+ }}
106
+ />,
107
+ $app
108
+ )
109
+ })
110
+ }
111
+ export { getHtml, getBatchHtml, getCSS, Printer, doBatchPrint }
@@ -8,15 +8,27 @@ import { pageTypeMap } from '../common/config'
8
8
 
9
9
  @observer
10
10
  class Printer extends React.Component {
11
+ componentDidMount () {
12
+ // Printer 不是立马就呈现出最终样式,有个过程。这个过程需要时间,什么 ready,不太清楚,估借 setState 来获取过程结束时刻
13
+ this.setState({}, () => {
14
+ this.props.onReady && this.props.onReady()
15
+ })
16
+ }
17
+
11
18
  render () {
12
- const { config, selected, data } = this.props
19
+ const { config, selected, data, isStation } = this.props
13
20
  const { type, style, customizeWidth, customizeHeight } = config.page
14
21
  return (
15
- <div className='gm-printer-label' style={{
16
- ...style,
17
- width: type === '-1' ? customizeWidth + 'mm' : pageTypeMap[type].width,
18
- height: type === '-1' ? customizeHeight + 'mm' : pageTypeMap[type].height
19
- }}>
22
+ <div
23
+ className='gm-printer-label'
24
+ style={{
25
+ ...style,
26
+ width:
27
+ type === '-1' ? customizeWidth + 'mm' : pageTypeMap[type].width,
28
+ height:
29
+ type === '-1' ? customizeHeight + 'mm' : pageTypeMap[type].height
30
+ }}
31
+ >
20
32
  <Page config={config.page}>
21
33
  {_.map(config.blocks, (block, i) => (
22
34
  <Block
@@ -25,6 +37,7 @@ class Printer extends React.Component {
25
37
  selected={selected}
26
38
  config={block}
27
39
  data={data}
40
+ isStation={isStation}
28
41
  />
29
42
  ))}
30
43
  </Page>
@@ -36,7 +49,9 @@ class Printer extends React.Component {
36
49
  Printer.propTypes = {
37
50
  selected: PropTypes.number,
38
51
  config: PropTypes.object.isRequired,
39
- data: PropTypes.object.isRequired // 格式化后的数据
52
+ data: PropTypes.object.isRequired, // 格式化后的数据
53
+ onReady: PropTypes.func,
54
+ isStation: PropTypes.bool
40
55
  }
41
56
 
42
57
  export default Printer
@@ -0,0 +1,17 @@
1
+ import React from 'react'
2
+ import QRCode from 'qrcode.react'
3
+ import PropTypes from 'prop-types'
4
+ import _ from 'lodash'
5
+
6
+ const QrCode = props => {
7
+ const { value, size } = props
8
+ return (
9
+ <QRCode renderAs='svg' value={value} size={_.isFinite(size) ? size : 75}/>
10
+ )
11
+ }
12
+
13
+ QrCode.propTypes = {
14
+ value: PropTypes.string.isRequired,
15
+ size: PropTypes.number.isRequired
16
+ }
17
+ export default QrCode
package/src/util.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _ from 'lodash'
2
2
 
3
3
  function pxAdd (origin, add) {
4
- return parseFloat(origin, 10) + add < 0 ? '0px' : parseFloat(origin, 10) + add + 'px'
4
+ return parseFloat(origin, 10) + add + 'px'
5
5
  }
6
6
 
7
7
  function getStyleWithDiff (style, diffX, diffY) {
@@ -35,17 +35,22 @@ function insertCSS (cssString, target) {
35
35
  }
36
36
 
37
37
  function dispatchMsg (event, data) {
38
- window.document.dispatchEvent(new window.CustomEvent(event, {
39
- detail: data
40
- }))
38
+ window.document.dispatchEvent(
39
+ new window.CustomEvent(event, {
40
+ detail: data
41
+ })
42
+ )
43
+ }
44
+ function substring (target, start = 0, end) {
45
+ return target.substring(start, end)
41
46
  }
42
-
43
47
  function template (text, data) {
44
48
  try {
45
49
  return _.template(text, {
46
50
  interpolate: /{{([\s\S]+?)}}/g
47
51
  })({
48
- ...data
52
+ ...data,
53
+ substring: substring // 添加一个截取字符串函数
49
54
  })
50
55
  } catch (err) {
51
56
  console.warn(err)
@@ -53,10 +58,30 @@ function template (text, data) {
53
58
  }
54
59
  }
55
60
 
61
+ let timer
62
+
63
+ function afterImgAndSvgLoaded (callback, $printer) {
64
+ const $imgList = $printer.querySelectorAll('img')
65
+ const $svgList = $printer.querySelectorAll('svg')
66
+
67
+ clearTimeout(timer)
68
+
69
+ const everyThingIsOk =
70
+ _.every($imgList, (img) => img.complete) &&
71
+ _.every($svgList, (svg) => svg.children.length)
72
+ if (everyThingIsOk) {
73
+ callback()
74
+ } else {
75
+ timer = setTimeout(afterImgAndSvgLoaded.bind(this, callback, $printer), 300)
76
+ }
77
+ }
78
+ const miniAppLink = 'https://miniapp.guanmai.cn/traceability/?id='
56
79
  export {
57
80
  pxAdd,
58
81
  getStyleWithDiff,
59
82
  insertCSS,
60
83
  dispatchMsg,
61
- template
84
+ template,
85
+ afterImgAndSvgLoaded,
86
+ miniAppLink
62
87
  }