q-koa 8.8.6 → 8.8.7
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/core/file/services/print.js +115 -0
- package/package.json +3 -2
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const { lodash, getConfig } = require('q-koa')
|
|
2
|
+
const yly = require('yly-nodejs-sdk')
|
|
3
|
+
// const LRU = require('lru-cache')
|
|
4
|
+
// const cache = new LRU({
|
|
5
|
+
// max: 100,
|
|
6
|
+
// maxAge: 1000 * 60 * 60,
|
|
7
|
+
// })
|
|
8
|
+
// const cacheKey = Symbol('print')
|
|
9
|
+
module.exports = class Singleton {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = {
|
|
12
|
+
...config,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 单例模式
|
|
17
|
+
*/
|
|
18
|
+
if (!Singleton.instance) {
|
|
19
|
+
Singleton.instance = this
|
|
20
|
+
}
|
|
21
|
+
const previous = Singleton.instance.getConfig()
|
|
22
|
+
if (!lodash.isEqual(previous, config)) {
|
|
23
|
+
Singleton.instance = this
|
|
24
|
+
}
|
|
25
|
+
return Singleton.instance
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async getAccessToken() {
|
|
29
|
+
const app = this.config.app
|
|
30
|
+
const { cid, secret } = this.config
|
|
31
|
+
this.oauthConfig = new yly.Config({
|
|
32
|
+
cid,
|
|
33
|
+
secret,
|
|
34
|
+
})
|
|
35
|
+
const oauthClient = new yly.OauthClinet(this.oauthConfig)
|
|
36
|
+
|
|
37
|
+
const tokenData = await app.model.setting.findOne({
|
|
38
|
+
where: {
|
|
39
|
+
code: 'print_token',
|
|
40
|
+
name: 'AccessToken',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
const print_token = tokenData.value
|
|
44
|
+
if (print_token) return print_token
|
|
45
|
+
const res = await oauthClient.getToken()
|
|
46
|
+
if (res.error !== 0 && res.error_description !== 'success') {
|
|
47
|
+
throw new Error('failed:' + res.error_description)
|
|
48
|
+
}
|
|
49
|
+
await app.model.setting.update(
|
|
50
|
+
{
|
|
51
|
+
value: res.body.access_token,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
where: {
|
|
55
|
+
code: 'print_token',
|
|
56
|
+
name: 'AccessToken',
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return res.body.access_token
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async print({ order_id = 0, content }) {
|
|
65
|
+
const app = this.config.app
|
|
66
|
+
const accessToken = await this.getAccessToken()
|
|
67
|
+
try {
|
|
68
|
+
const RpcClient = new yly.RpcClient(accessToken, this.oauthConfig)
|
|
69
|
+
const Print = new yly.Print(RpcClient)
|
|
70
|
+
return await Print.index(this.config.machine_code, order_id, content)
|
|
71
|
+
} catch (e) {
|
|
72
|
+
throw new Error(`请重试,${e.message}`)
|
|
73
|
+
await app.model.setting.update(
|
|
74
|
+
{
|
|
75
|
+
value: '',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
where: {
|
|
79
|
+
code: 'print_token',
|
|
80
|
+
name: 'AccessToken',
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
// return await this.print({ order_id, content })
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async cancelAll() {
|
|
89
|
+
const app = this.config.app
|
|
90
|
+
const accessToken = await this.getAccessToken()
|
|
91
|
+
try {
|
|
92
|
+
const RpcClient = new yly.RpcClient(accessToken, this.oauthConfig)
|
|
93
|
+
const Print = new yly.Printer(RpcClient)
|
|
94
|
+
return await Print.cancelAll(this.config.machine_code)
|
|
95
|
+
} catch (e) {
|
|
96
|
+
throw new Error(`请重试,${e.message}`)
|
|
97
|
+
await app.model.setting.update(
|
|
98
|
+
{
|
|
99
|
+
value: '',
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
where: {
|
|
103
|
+
code: 'print_token',
|
|
104
|
+
name: 'AccessToken',
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
// return await this.print({ order_id, content })
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getConfig() {
|
|
113
|
+
return this.config
|
|
114
|
+
}
|
|
115
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "q-koa",
|
|
3
|
-
"version": "8.8.
|
|
3
|
+
"version": "8.8.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -55,7 +55,8 @@
|
|
|
55
55
|
"weixin-pay-fork": "^1.0.0",
|
|
56
56
|
"node-uuid": "^1.4.8",
|
|
57
57
|
"sha1": "^1.1.1",
|
|
58
|
-
"redis": "^4.0.3"
|
|
58
|
+
"redis": "^4.0.3",
|
|
59
|
+
"yly-nodejs-sdk": "^1.0.2"
|
|
59
60
|
},
|
|
60
61
|
"devDependencies": {
|
|
61
62
|
"eslint": "^4.19.1",
|