q-koa 8.8.4 → 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.
@@ -48,3 +48,56 @@ exports.copyOther = async (ctx) => {
48
48
 
49
49
  ctx.SUCCESS('ok')
50
50
  }
51
+
52
+ exports.init = async (ctx) => {
53
+ const { app, appName } = getAppByCtx(ctx)
54
+
55
+ const parentList = await app.model.setting.findAll({
56
+ where: {
57
+ parent_id: 0,
58
+ },
59
+ order: [['id', 'ASC']],
60
+ raw: true,
61
+ })
62
+
63
+ let list = []
64
+ let number = 0
65
+ for (let i = 0; i < parentList.length; i++) {
66
+ number += 1
67
+ const { id: parent_id, ...rest } = parentList[i]
68
+ list = [
69
+ ...list,
70
+ {
71
+ id: number,
72
+ ...rest,
73
+ parent_id: 0,
74
+ },
75
+ ]
76
+ const childList = await app.model.setting.findAll({
77
+ where: {
78
+ parent_id,
79
+ },
80
+ order: [['id', 'ASC']],
81
+ raw: true,
82
+ })
83
+ list = [
84
+ ...list,
85
+ ...childList.map((item, index) => {
86
+ const { id: child_id, ...restChild } = item
87
+ const id = number + index + 1
88
+
89
+ return {
90
+ id,
91
+ ...restChild,
92
+ parent_id: number,
93
+ }
94
+ }),
95
+ ]
96
+ number += childList.length
97
+ }
98
+
99
+ await app.model.setting.sync({ force: true })
100
+ await app.model.setting.bulkCreate(list)
101
+
102
+ ctx.SUCCESS(list)
103
+ }
@@ -183,6 +183,8 @@ exports.showTables = async (ctx) => {
183
183
  deleteCheckList: lodash.get(target, 'deleteCheckList', []),
184
184
  // 批量更新字段
185
185
  bulkCreateList: lodash.get(target, 'bulkCreateList', []),
186
+
187
+ fn: app.controller[_model] ? Object.keys(app.controller[_model]) : [],
186
188
  }
187
189
  })
188
190
 
@@ -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.4",
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",