-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (54 loc) · 1.85 KB
/
app.js
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
const qrcode = require('qrcode')
const extend = require('extend')
const debug = require('./lib/debug')
const synthetic = require('./lib/synthetic')
const config = require('./config')
class Poster {
constructor(options = {}) {
this.options = options
}
/**
*
* @param {String} text 必须 qrcode内容
* @param {String} filepath 可选 海报生成路径,填写则覆盖options.dist()配置
* @param {Object} options 可选 填写则合并到上游配置
* @return {Promise<String>} 返回海报生成路径
*/
async toFile(text, filepath, options) {
if ('object' === typeof filepath) {
options = filepath
filepath = null
}
// 合并配置
options = this.$createOptions(options)
debug('options:', options)
// 查询海报模板
const template = options.templates[options.index]
if (!template) {
throw new Error('Template is Empty, Please check templates and index')
}
// 创建二维码
const qrcode = await this.$qrcode(text, options)
const poster = {
...template,
dist: filepath || options.dist()
}
debug('qrcode:', qrcode)
debug('poster:', poster)
// 二维码 + 海报模板 = 合成海报
return synthetic(qrcode, poster).then(() => poster.dist)
}
$qrcode(text, options) {
const filepath = options.qrcode.dist()
const template = options.templates[options.index]
return qrcode.toFile(filepath, text, options.qrcode).then(() => ({
width: template.qrWidth,
height: template.qrHeight || template.qrWidth,
filepath
}))
}
$createOptions(options = {}) {
return extend(true, {}, config, this.options, options)
}
}
module.exports = Poster