q-koa 8.8.1 → 8.8.4

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
@@ -1,3 +1,4 @@
1
+ const axios = require('axios')
1
2
  const { Sequelize } = require('sequelize')
2
3
  const db = new Map()
3
4
  module.exports = {
@@ -130,4 +131,29 @@ module.exports = {
130
131
  excludes: ['admin'],
131
132
  },
132
133
  cacheModel: [],
134
+ defaultRouter: (router) => (app) => {
135
+ router.get('/weixin/h5_login', app.controller.weixin.h5_login)
136
+ router.get(
137
+ '/weixin/h5_login_callback',
138
+ app.controller.weixin.h5_login_callback
139
+ )
140
+ router.get(
141
+ '/weixin/h5_login_info_callback',
142
+ app.controller.weixin.h5_login_info_callback
143
+ )
144
+ router.get('/common/getAppConfig', app.controller.common.getAppConfig)
145
+ router.get('/weixin/messagePush', app.controller.weixin.messagePush)
146
+ router.get('/common/createQr', app.controller.common.createQr)
147
+ router.get('/lanhu/:str', async (ctx) => {
148
+ const { str } = ctx.params
149
+ const url = `https://lanhu.oss-cn-beijing.aliyuncs.com/${str}`
150
+ const imgData = await axios
151
+ .get(url, {
152
+ responseType: 'arraybuffer',
153
+ })
154
+ .then((res) => res.data)
155
+ ctx.set('content-type', 'image')
156
+ ctx.body = imgData
157
+ })
158
+ },
133
159
  }
@@ -181,9 +181,10 @@ 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
+ if (!url) return ctx.ERROR('?url')
185
186
  const img = qr.image(url, {
186
- size: 10,
187
+ size: Number(size),
187
188
  })
188
189
  ctx.type = 'image/png'
189
190
  ctx.body = img
@@ -417,6 +418,24 @@ exports.express = async (ctx) => {
417
418
  .slice(express_mobile.length - 4, express_mobile.length)
418
419
  .join('')
419
420
 
421
+ let history = null
422
+ if (app.model.express_record) {
423
+ history = await app.model.express_record.findOne({
424
+ where: {
425
+ express_number,
426
+ },
427
+ })
428
+ if (history) {
429
+ const diffNoLong = moment().diff(moment(history.updated_at), 'hours') < 1
430
+ const isTooLong =
431
+ moment().diff(moment(history.result.updateTime), 'days') > 30
432
+
433
+ if (diffNoLong || isTooLong) {
434
+ return ctx.SUCCESS(history.result)
435
+ }
436
+ }
437
+ }
438
+
420
439
  try {
421
440
  const express = new Express({
422
441
  app_code,
@@ -427,6 +446,21 @@ exports.express = async (ctx) => {
427
446
  ? `${express_number}:${lastFour}`
428
447
  : express_number,
429
448
  })
449
+
450
+ if (app.model.express_record) {
451
+ if (!history) {
452
+ app.model.express_record.upsert({
453
+ express_number,
454
+ result,
455
+ })
456
+ } else {
457
+ app.model.express_record.upsert({
458
+ id: history.id,
459
+ result,
460
+ })
461
+ }
462
+ }
463
+
430
464
  ctx.SUCCESS(result)
431
465
  } catch (e) {
432
466
  return ctx.SUCCESS(null)
@@ -468,7 +502,6 @@ exports.urlToOss = async (ctx) => {
468
502
  .then((res) => res.data)
469
503
 
470
504
  const type = getImageType(buffer)
471
- console.log('type', type)
472
505
  const fileName = `${
473
506
  String(new Date().getTime()).split('').reverse().join('') + Math.random()
474
507
  }${type}`
@@ -486,3 +519,12 @@ exports.urlToOss = async (ctx) => {
486
519
  throw new Error('上传失败')
487
520
  }
488
521
  }
522
+
523
+ exports.getAppConfig = async (ctx) => {
524
+ const { app, appName } = getAppByCtx(ctx)
525
+ const appConfig = getConfig(app)
526
+ if (!ctx.request.query.config) throw new Error('?config')
527
+ const result = await appConfig.getObject(ctx.request.query.config)
528
+
529
+ ctx.SUCCESS(result)
530
+ }
@@ -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.1",
3
+ "version": "8.8.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {