q-koa 7.7.5 → 7.8.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/core/app.js CHANGED
@@ -3,6 +3,7 @@ const Router = require('koa-router')
3
3
  const _ = require('lodash')
4
4
  const path = require('path')
5
5
  const fs = require('fs')
6
+ const fsPromise = require('fs/promises')
6
7
  const Sequelize = require('sequelize')
7
8
  const moment = require('moment')
8
9
  const { EventEmitter } = require('events')
@@ -242,7 +243,7 @@ class APP {
242
243
  // console.log(content);
243
244
  // });
244
245
 
245
- starter.on('loadAll', () => {
246
+ starter.on('loadAll', async () => {
246
247
  const appName = this.config.includes[0]
247
248
  this.server = this.app.listen(this.port, () => {
248
249
  console.log(`server is running at http://localhost:${this.port}`)
@@ -261,7 +262,7 @@ class APP {
261
262
  appName: appName,
262
263
  })
263
264
  } else {
264
- fs.writeFileSync(
265
+ await fsPromise.writeFile(
265
266
  `${rootDirectory}/${APP_DIR}/${appName}/${PLUGINS_DIR}/model/service.js`,
266
267
  ''
267
268
  )
@@ -294,7 +295,7 @@ class APP {
294
295
  }
295
296
 
296
297
  async initApp() {
297
- let dirList = fs.readdirSync(
298
+ let dirList = await fsPromise.readdir(
298
299
  path.resolve(__dirname, `${rootDirectory}/${APP_DIR}`)
299
300
  )
300
301
  dirList = dirList.filter((item) => this.config.includes.includes(item))
@@ -359,7 +360,7 @@ class APP {
359
360
  this.initFile(appName)
360
361
  }
361
362
 
362
- this.initPlugin(appName)
363
+ await this.initPlugin(appName)
363
364
  this.app[appName].event = new EventEmitter()
364
365
 
365
366
  const cacheConfig = _.get(this.app[appName], 'appConfig.cache', {
@@ -372,7 +373,7 @@ class APP {
372
373
  maxAge: cacheConfig.maxAge,
373
374
  })
374
375
 
375
- this.initModel(appName)
376
+ await this.initModel(appName)
376
377
  // const models = Object.keys(this.app[appName].model).join(' / ')
377
378
  if (taskConfig.is_available) {
378
379
  this.initTask(appName)
@@ -421,18 +422,21 @@ class APP {
421
422
  }
422
423
  }
423
424
 
424
- initTask(appName) {
425
+ async initTask(appName) {
425
426
  console.log(chalk.red(`启动任务${appName}`))
426
427
  const taskDir = path.resolve(
427
428
  __dirname,
428
429
  `${rootDirectory}/${APP_DIR}/${appName}/task`
429
430
  )
430
431
  const publicExist = fs.existsSync(taskDir)
431
- if (!publicExist) fs.mkdirSync(taskDir)
432
- fs.readdirSync(taskDir).forEach((folder) => {
432
+ if (!publicExist) await fsPromise.mkdir(taskDir)
433
+ const list = await fsPromise.readdir(taskDir)
434
+ for (let i = 0; i < list.length; i++) {
435
+ const folder = list[i]
433
436
  const isFolder = fs.lstatSync(path.resolve(taskDir, folder)).isDirectory()
434
437
  if (isFolder) {
435
- fs.readdirSync(path.resolve(taskDir, folder)).forEach((filename) => {
438
+ const folderDir = await fsPromise.readdir(path.resolve(taskDir, folder))
439
+ folderDir.forEach((filename) => {
436
440
  if (filename === 'index.js') {
437
441
  const task = require(path.resolve(
438
442
  __dirname,
@@ -447,23 +451,27 @@ class APP {
447
451
  }
448
452
  })
449
453
  }
450
- })
454
+ }
451
455
  }
452
456
 
453
- initPlugin(appName) {
457
+ async initPlugin(appName) {
454
458
  const pluginDir = path.resolve(
455
459
  __dirname,
456
460
  `${rootDirectory}/${APP_DIR}/${appName}/${PLUGINS_DIR}`
457
461
  )
458
462
  const publicExist = fs.existsSync(pluginDir)
459
- if (!publicExist) fs.mkdirSync(pluginDir)
460
-
461
- fs.readdirSync(pluginDir).forEach((folder) => {
463
+ if (!publicExist) await fsPromise.mkdir(pluginDir)
464
+ const pluginDirResult = await fsPromise.readdir(pluginDir)
465
+ for (let i = 0; i < pluginDirResult.length; i++) {
466
+ const folder = pluginDirResult[i]
462
467
  const isFolder = fs
463
468
  .lstatSync(path.resolve(pluginDir, folder))
464
469
  .isDirectory()
465
470
  if (isFolder) {
466
- fs.readdirSync(path.resolve(pluginDir, folder)).forEach((filename) => {
471
+ const folderResult = await fsPromise.readdir(
472
+ path.resolve(pluginDir, folder)
473
+ )
474
+ folderResult.forEach((filename) => {
467
475
  const extname = path.extname(filename)
468
476
  if (extname === '.js') {
469
477
  const n = path.basename(filename, extname)
@@ -658,7 +666,7 @@ class APP {
658
666
  }
659
667
  })
660
668
  }
661
- })
669
+ }
662
670
  }
663
671
 
664
672
  initModel(appName) {
@@ -1,4 +1,5 @@
1
1
  const fs = require('fs')
2
+ const fsPromise = require('fs/promises')
2
3
  const path = require('path')
3
4
  const crypto = require('crypto')
4
5
  const { Random, lodash, getAppByCtx, getConfig } = require('q-koa')
@@ -36,7 +37,7 @@ exports.upload = async (ctx) => {
36
37
 
37
38
  const { site_host } = await appConfig.getObject('base')
38
39
 
39
- const reader = fs.readFileSync(file.path)
40
+ const reader = await fsPromise.readFile(file.path)
40
41
 
41
42
  const fsHash = crypto.createHash('md5')
42
43
  fsHash.update(reader)
@@ -66,7 +67,7 @@ exports.upload = async (ctx) => {
66
67
  )
67
68
  if (isExists) return ctx.SUCCESS(responseData)
68
69
 
69
- fs.writeFileSync(targetPath, reader)
70
+ await fsPromise.writeFile(targetPath, reader)
70
71
  return ctx.SUCCESS(responseData)
71
72
  }
72
73
 
@@ -220,7 +221,7 @@ exports.swagger = async (ctx) => {
220
221
  `${process.cwd()}/app/${appName}/plugins`
221
222
  )
222
223
  const apis = []
223
- const pathList = fs.readdirSync(filePath)
224
+ const pathList = await fsPromise.readdir(filePath)
224
225
  for (let i = 0; i < pathList.length; i++) {
225
226
  const folder = pathList[i]
226
227
  const isFolder = fs.lstatSync(path.resolve(filePath, folder)).isDirectory()
@@ -240,7 +241,10 @@ exports.swagger = async (ctx) => {
240
241
  const controllerFilePath = path.resolve(filePath, folder, 'controller.js')
241
242
  const isControllerExist = fs.existsSync(controllerFilePath)
242
243
  if (isControllerExist) {
243
- const controllerFile = fs.readFileSync(controllerFilePath, 'utf-8')
244
+ const controllerFile = await fsPromise.readFile(
245
+ controllerFilePath,
246
+ 'utf-8'
247
+ )
244
248
  const annotations = controllerFile.match(annotationsReg)
245
249
 
246
250
  if (annotations) {
@@ -2,6 +2,7 @@ const { getAppByCtx, lodash } = require('q-koa')
2
2
 
3
3
  const fs = require('fs')
4
4
  const path = require('path')
5
+ const fsPromise = require('fs/promises')
5
6
 
6
7
  exports.getModel = async (ctx) => {
7
8
  const { app, appName } = getAppByCtx(ctx)
@@ -19,7 +20,7 @@ exports.getModel = async (ctx) => {
19
20
  )
20
21
  )
21
22
  if (!existFile) return ctx.SUCCESS('')
22
- const file = fs.readFileSync(
23
+ const file = await fsPromise.readFile(
23
24
  path.resolve(
24
25
  __dirname,
25
26
  `${process.cwd()}/app/${appName}/plugins/${model}/${option}.js`
@@ -54,7 +55,7 @@ exports.createFile = async (ctx) => {
54
55
  `${process.cwd()}/app/${appName}/plugins/${plugin}`
55
56
  )
56
57
  const exist = fs.existsSync(pluginDirPath)
57
- if (!exist) fs.mkdirSync(pluginDirPath)
58
+ if (!exist) await fsPromise.mkdir(pluginDirPath)
58
59
  const list = ['config', 'model', 'controller']
59
60
 
60
61
  for (let i = 0; i < list.length; i++) {
@@ -62,8 +63,10 @@ exports.createFile = async (ctx) => {
62
63
  if (!obj[type]) {
63
64
  continue
64
65
  }
65
- fs.writeFileSync(path.resolve(pluginDirPath, `${type}.js`), obj[type])
66
+ await fsPromise.writeFile(
67
+ path.resolve(pluginDirPath, `${type}.js`),
68
+ obj[type]
69
+ )
66
70
  }
67
- // fs.writeFileSync(path.resolve(pluginDirPath, 'config.js'), desc)
68
71
  ctx.SUCCESS('ok')
69
72
  }
@@ -1,12 +1,12 @@
1
1
  const fs = require('fs')
2
2
  const path = require('path')
3
-
3
+ const fsPromise = require('fs/promises')
4
4
  exports.loadModel = async ({ app, appName }) => {
5
- const data = fs.readdirSync(
5
+ const data = await fsPromise.readdir(
6
6
  path.resolve(__dirname, `${process.cwd()}/app/${appName}/plugins`)
7
7
  )
8
8
 
9
- const modelList = await app.model.model.findAll({ raw: true })
9
+ // const modelList = await app.model.model.findAll({ raw: true })
10
10
 
11
11
  await Promise.all(
12
12
  ['model', 'model_attributes'].map((item) =>
@@ -34,32 +34,32 @@ exports.loadModel = async ({ app, appName }) => {
34
34
  __dirname,
35
35
  `../${result[i]}/config.js`
36
36
  )
37
-
38
37
  const modelExist = fs.existsSync(filePath)
39
38
  const defaultModelExist = fs.existsSync(defaultFilePath)
40
39
  const configExist = fs.existsSync(configPath)
41
40
  if (configExist) {
42
- const config = fs.readFileSync(configPath, 'utf-8')
41
+ const config = (await fsPromise.readFile(configPath, 'utf-8'))
43
42
  ? require(configPath)
44
43
  : require(defaultConfigPath)
45
44
 
46
45
  const modelFile =
47
46
  defaultModelExist || modelExist
48
- ? fs.readFileSync(filePath, 'utf-8')
49
- ? fs.readFileSync(filePath, 'utf-8')
50
- : fs.readFileSync(defaultFilePath, 'utf-8')
47
+ ? (await fsPromise.readFile(filePath, 'utf-8'))
48
+ ? await fsPromise.readFile(filePath, 'utf-8')
49
+ : await fsPromise.readFile(defaultFilePath, 'utf-8')
51
50
  : null
52
51
 
53
52
  const model = modelFile
54
- ? fs.readFileSync(filePath, 'utf-8')
53
+ ? (await fsPromise.readFile(filePath, 'utf-8'))
55
54
  ? require(filePath)
56
55
  : require(defaultFilePath)
57
56
  : {}
58
57
  const attributes = modelFile ? modelFile.match(/exports.(.*)=/g) : []
59
58
  const types = modelFile ? modelFile.match(/Sequelize.(.*),\n/g) : {}
60
59
 
61
- const ori_target = modelList.find((m) => m.model === result[i])
62
- const id = ori_target ? ori_target.id : null
60
+ // const ori_target = modelList.find((m) => m.model === result[i])
61
+ // const id = ori_target ? ori_target.id : null
62
+ const id = i + 1
63
63
  const obj = {
64
64
  id,
65
65
  model: result[i],
@@ -1,5 +1,6 @@
1
1
  const fs = require('fs')
2
2
  const path = require('path')
3
+ const fsPromise = require('fs/promises')
3
4
  const { VM } = require('vm2')
4
5
  const axios = require('axios')
5
6
  const cheerio = require('cheerio')
@@ -57,7 +58,8 @@ exports.showTables = async (ctx) => {
57
58
  `${process.cwd()}/app/${appName}/plugins`
58
59
  )
59
60
  let aliasModelList = []
60
- fs.readdirSync(pluginDir).forEach((folder) => {
61
+ const allList = await fsPromise.readdir(pluginDir)
62
+ allList.forEach((folder) => {
61
63
  const isFolder = fs.lstatSync(path.resolve(pluginDir, folder)).isDirectory()
62
64
  const hasConfig = fs.existsSync(
63
65
  path.resolve(
@@ -249,7 +251,7 @@ exports.getImage = async (ctx) => {
249
251
  __dirname,
250
252
  `${process.cwd()}/public/upload`
251
253
  )}`
252
- const files = fs.readdirSync(targetDir)
254
+ const files = await fsPromise.readdir(targetDir)
253
255
  const images = files.filter((file) =>
254
256
  /\w(\.gif|\.jpeg|\.png|\.jpg|\.bmp)/i.test(file)
255
257
  )
@@ -3,7 +3,7 @@ const { lodash, getAppByCtx, getConfig } = require('q-koa')
3
3
  const path = require('path')
4
4
  const axios = require('axios')
5
5
  const OAuth = require('wechat-oauth')
6
- const WXPay = require('weixin-pay')
6
+ const WXPay = require('weixin-pay-fork')
7
7
  const WeixinPay = require('../../services/weixinPay')
8
8
  const WeixinMp = require('../../services/weixinMP')
9
9
  const Weixin = require('../../services/weixin')
@@ -956,15 +956,19 @@ exports.refund_notify = async (ctx) => {
956
956
  const prefix =
957
957
  out_trade_no.split('_').length === 4 ? out_trade_no.split('_')[1] : ''
958
958
 
959
- const order_id = Number(out_refund_no)
959
+ const order_id = out_trade_no.includes('_')
960
+ ? Number(out_trade_no.split('_')[0])
961
+ : Number(out_trade_no)
962
+ const type = out_trade_no.includes('_') ? out_trade_no.split('_')[1] : ''
960
963
  const model = prefix ? `${prefix}_order` : 'order'
961
- console.log(out_refund_no, '微信支付回调-----', model)
964
+ console.log(out_refund_no, '微信支付回调-----', type, model)
962
965
  if (app.service[model] && app.service[model].refund_notify) {
963
966
  await app.service[model].refund_notify({
964
967
  app,
965
968
  order_id,
966
969
  order: model,
967
970
  refund_price,
971
+ type,
968
972
  })
969
973
  }
970
974
 
@@ -2,6 +2,7 @@ const { getAppByCtx, getConfig } = require('q-koa')
2
2
  const { Pay } = require('@sigodenjs/wechatpay')
3
3
  const fs = require('fs')
4
4
  const path = require('path')
5
+ const fsPromise = require('fs/promises')
5
6
 
6
7
  exports.refund = async ({
7
8
  ctx,
@@ -10,6 +11,7 @@ exports.refund = async ({
10
11
  total_fee,
11
12
  refund_fee,
12
13
  price,
14
+ type = '',
13
15
  ...rest
14
16
  }) => {
15
17
  const { app, appName } = getAppByCtx(ctx)
@@ -21,7 +23,7 @@ exports.refund = async ({
21
23
  appId: appId,
22
24
  mchId: mchId,
23
25
  key: key, // 微信商户平台API密钥,
24
- pfx: fs.readFileSync(
26
+ pfx: await fsPromise.readFile(
25
27
  path.resolve(
26
28
  __dirname,
27
29
  `${process.cwd()}/app/${appName}/plugins/weixin/apiclient_cert.p12`
@@ -42,7 +44,7 @@ exports.refund = async ({
42
44
  const { result_code, err_code_des } = await payObj.refund({
43
45
  ...rest,
44
46
  out_trade_no,
45
- out_refund_no: id + '',
47
+ out_refund_no: id + '_' + type,
46
48
  total_fee: Math.round((total_fee || refund_fee || price) * 100),
47
49
  refund_fee: Math.round((refund_fee || price) * 100),
48
50
  notify_url: `https://${
@@ -71,7 +73,7 @@ exports.cash = async ({ ctx, id, user_id, number }) => {
71
73
  appId: appId,
72
74
  mchId: mchId,
73
75
  key: key, // 微信商户平台API密钥,
74
- pfx: fs.readFileSync(
76
+ pfx: await fsPromise.readFile(
75
77
  path.resolve(
76
78
  __dirname,
77
79
  `${process.cwd()}/app/${appName}/plugins/weixin/apiclient_cert.p12`
@@ -1,6 +1,6 @@
1
1
  const util = require('util')
2
2
  const axios = require('axios')
3
-
3
+ const fsPromise = require('fs/promises')
4
4
  const { lodash } = require('q-koa')
5
5
  const getAccessTokenUrl =
6
6
  'https://developer.toutiao.com/api/apps/token?grant_type=%s&appid=%s&secret=%s'
@@ -88,7 +88,7 @@ module.exports = class Singleton {
88
88
  }
89
89
  throw new Error(bufferResult.errmsg)
90
90
  }
91
- await fs.writeFileSync(
91
+ await fsPromise.writeFile(
92
92
  `${this.config.targetPath}/${creatuuid}.png`,
93
93
  bufferResult
94
94
  )
@@ -31,6 +31,7 @@ const addOrderUrl =
31
31
 
32
32
  const path = require('path')
33
33
  const fs = require('fs')
34
+ const fsPromise = require('fs/promises')
34
35
  const LRU = require('lru-cache')
35
36
  const request = require('request')
36
37
  const cache = new LRU({
@@ -313,7 +314,7 @@ module.exports = class Singleton {
313
314
  }
314
315
  throw new Error(bufferResult.errmsg)
315
316
  }
316
- await fs.writeFileSync(
317
+ await fsPromise.writeFile(
317
318
  `${this.config.targetPath}/${creatuuid}.png`,
318
319
  bufferResult
319
320
  )
@@ -4,7 +4,10 @@ const fs = require('fs')
4
4
  const path = require('path')
5
5
  const Gateway = require('./utils/gateway.js')
6
6
 
7
- const content = fs.readFileSync(path.join(__dirname, '../faas/index.html'), 'utf-8')
7
+ const content = fs.readFileSync(
8
+ path.join(__dirname, '../faas/index.html'),
9
+ 'utf-8'
10
+ )
8
11
 
9
12
  const cache = new WeakMap()
10
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "7.7.5",
3
+ "version": "7.8.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {