q-koa 8.8.0 → 8.8.3

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
@@ -426,6 +426,11 @@ class APP {
426
426
  ) {
427
427
  this.app[appName].appConfig.router(router)(this.app[appName])
428
428
  }
429
+
430
+ if (this.config.defaultRouter) {
431
+ this.config.defaultRouter(router)(this.app[appName])
432
+ }
433
+
429
434
  this.initRouter(router)
430
435
  this.app.use(router.routes()).use(router.allowedMethods())
431
436
  }
package/core/config.js CHANGED
@@ -130,4 +130,20 @@ module.exports = {
130
130
  excludes: ['admin'],
131
131
  },
132
132
  cacheModel: [],
133
+ defaultRouter: (router) => (app) => {
134
+ router.get('/common/getAppConfig', app.controller.common.getAppConfig)
135
+ router.get('/weixin/messagePush', app.controller.weixin.messagePush)
136
+ router.get('/common/createQr', app.controller.common.createQr)
137
+ router.get('/lanhu/:str', async (ctx) => {
138
+ const { str } = ctx.params
139
+ const url = `https://lanhu.oss-cn-beijing.aliyuncs.com/${str}`
140
+ const imgData = await axios
141
+ .get(url, {
142
+ responseType: 'arraybuffer',
143
+ })
144
+ .then((res) => res.data)
145
+ ctx.set('content-type', 'image')
146
+ ctx.body = imgData
147
+ })
148
+ },
133
149
  }
@@ -181,9 +181,9 @@ exports.email = async (ctx) => {
181
181
  exports.createQr = async (ctx) => {
182
182
  const { app, appName } = getAppByCtx(ctx)
183
183
 
184
- const { url } = ctx.request.query
184
+ const { url, size = 10 } = ctx.request.query
185
185
  const img = qr.image(url, {
186
- size: 10,
186
+ size: Number(size),
187
187
  })
188
188
  ctx.type = 'image/png'
189
189
  ctx.body = img
@@ -406,19 +406,64 @@ exports.verify = async (ctx) => {
406
406
  exports.express = async (ctx) => {
407
407
  const { app } = getAppByCtx(ctx)
408
408
  const appConfig = getConfig(app)
409
- const { app_code } = await appConfig.getObject('express')
409
+ const { app_code, express_mobile } = await appConfig.getObject('express')
410
410
 
411
- const express = new Express({
412
- app_code,
413
- })
411
+ const { express_number: _express_number } = ctx.request.body
414
412
 
415
- const { id, express_number } = ctx.request.body
413
+ const express_number = _express_number.replace(/\t/, '').trim()
416
414
 
417
- const result = await express.send({
418
- no: express_number,
419
- })
415
+ const lastFour = express_mobile
416
+ .split('')
417
+ .slice(express_mobile.length - 4, express_mobile.length)
418
+ .join('')
420
419
 
421
- ctx.SUCCESS(result)
420
+ let history = null
421
+ if (app.model.express_record) {
422
+ history = await app.model.express_record.findOne({
423
+ where: {
424
+ express_number,
425
+ },
426
+ })
427
+ if (history) {
428
+ const diffNoLong = moment().diff(moment(history.updated_at), 'hours') < 1
429
+ const isTooLong =
430
+ moment().diff(moment(history.result.updateTime), 'days') > 30
431
+
432
+ if (diffNoLong || isTooLong) {
433
+ return ctx.SUCCESS(history.result)
434
+ }
435
+ }
436
+ }
437
+
438
+ try {
439
+ const express = new Express({
440
+ app_code,
441
+ })
442
+
443
+ const result = await express.send({
444
+ no: express_number.startsWith('SF')
445
+ ? `${express_number}:${lastFour}`
446
+ : express_number,
447
+ })
448
+
449
+ if (app.model.express_record) {
450
+ if (!history) {
451
+ app.model.express_record.upsert({
452
+ express_number,
453
+ result,
454
+ })
455
+ } else {
456
+ app.model.express_record.upsert({
457
+ id: history.id,
458
+ result,
459
+ })
460
+ }
461
+ }
462
+
463
+ ctx.SUCCESS(result)
464
+ } catch (e) {
465
+ return ctx.SUCCESS(null)
466
+ }
422
467
  }
423
468
 
424
469
  exports.urlToOss = async (ctx) => {
@@ -456,7 +501,6 @@ exports.urlToOss = async (ctx) => {
456
501
  .then((res) => res.data)
457
502
 
458
503
  const type = getImageType(buffer)
459
- console.log('type', type)
460
504
  const fileName = `${
461
505
  String(new Date().getTime()).split('').reverse().join('') + Math.random()
462
506
  }${type}`
@@ -474,3 +518,12 @@ exports.urlToOss = async (ctx) => {
474
518
  throw new Error('上传失败')
475
519
  }
476
520
  }
521
+
522
+ exports.getAppConfig = async (ctx) => {
523
+ const { app, appName } = getAppByCtx(ctx)
524
+ const appConfig = getConfig(app)
525
+ if (!ctx.request.query.config) throw new Error('?config')
526
+ const result = await appConfig.getObject(ctx.request.query.config)
527
+
528
+ ctx.SUCCESS(result)
529
+ }
@@ -14,6 +14,12 @@ const fxp = require('fast-xml-parser')
14
14
  const crypto = require('crypto')
15
15
  const OSS = require('ali-oss')
16
16
 
17
+ const check = ({ timestamp, nonce, signature, token }) => {
18
+ const tmp = [token, timestamp, nonce].sort().join('')
19
+ const currSign = crypto.createHash('sha1').update(tmp).digest('hex')
20
+ return currSign === signature
21
+ }
22
+
17
23
  exports.getConfig = async (ctx) => {
18
24
  const { app } = getAppByCtx(ctx)
19
25
 
@@ -1216,3 +1222,47 @@ exports.init_mp_openid = async (ctx) => {
1216
1222
 
1217
1223
  ctx.SUCCESS(result.length)
1218
1224
  }
1225
+
1226
+ exports.messagePush = async (ctx) => {
1227
+ const { app, appName } = getAppByCtx(ctx)
1228
+
1229
+ const { echostr, nonce, signature, timestamp } = ctx.request.query
1230
+
1231
+ const token = appName
1232
+
1233
+ const checkResult = check({
1234
+ nonce,
1235
+ signature,
1236
+ timestamp,
1237
+ token,
1238
+ })
1239
+
1240
+ if (!checkResult) {
1241
+ return (ctx.body = 'It is not from weixin')
1242
+ }
1243
+
1244
+ if (ctx.request.method === 'GET') {
1245
+ ctx.body = echostr
1246
+ } else {
1247
+ const result = ctx.request.body
1248
+ if (!lodash.isEmpty(result)) {
1249
+ const event = result.Event
1250
+ if (event) {
1251
+ if (app.service.weixin[event]) {
1252
+ await app.service.weixin[event]({
1253
+ app,
1254
+ result,
1255
+ })
1256
+ }
1257
+ } else {
1258
+ await app.service.weixin.handleUserMessage({
1259
+ app,
1260
+ result,
1261
+ })
1262
+ }
1263
+ }
1264
+
1265
+ ctx.status = 200
1266
+ ctx.body = 'success'
1267
+ }
1268
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "8.8.0",
3
+ "version": "8.8.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {