forked from go-cqhttp/for-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
44 lines (42 loc) · 866 Bytes
/
service.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
const qr = require('qr-image')
const fs = require('fs')
const path = require('path')
const os = require('os')
async function getImage(text) {
return new Promise((resolve, reject) => {
const filename = path.join(
os.tmpdir(),
`go-cqhttp-node-qrcode-${Date.now()}.png`
)
const stream = fs.createWriteStream(filename)
qr.image(text, {
type: 'png',
size: 10,
margin: 2
}).pipe(stream)
stream.on('finish', () =>
resolve([
{
type: 'image',
data: {
file: 'file://' + filename
}
}
])
)
stream.on('error', err => {
console.error('[qrcode]', err)
resolve([
{
type: 'text',
data: {
text: '生成二维码失败'
}
}
])
})
})
}
module.exports = {
getImage
}