q-koa 9.8.0 → 9.8.2

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
@@ -21,7 +21,8 @@ const util = require('util')
21
21
  const jwt = require('jsonwebtoken')
22
22
  const verify = util.promisify(jwt.verify)
23
23
  const fsExtra = require('fs-extra')
24
- const { getObject } = require('./file/utils')
24
+ const { getObject, getAppConfig } = require('./file/utils')
25
+
25
26
  const restc = require('./restc')
26
27
  const APP_DIR = 'app'
27
28
  const PLUGINS_DIR = 'plugins'
@@ -2001,4 +2002,6 @@ APP.getConfig = (app) => ({
2001
2002
  },
2002
2003
  })
2003
2004
 
2005
+ APP.getAppConfig = getAppConfig
2006
+
2004
2007
  module.exports = APP
@@ -1,9 +1,15 @@
1
1
  const md5 = require('js-md5')
2
2
  const { getAppByCtx, getConfig, lodash } = require('q-koa')
3
3
  const { getAppConfig } = require('../../utils')
4
+
4
5
  exports.login = async (ctx) => {
5
6
  const { app, appName } = getAppByCtx(ctx)
6
- const { mobile, password, ...rest } = ctx.request.body
7
+ const { mobile, password, config: _config, ...rest } = ctx.request.body
8
+
9
+ const config = _config || ctx.request.header.config || 'weixin_mp'
10
+ const appConfig = getConfig(app)
11
+ const { app_id } = await appConfig.getObject(config)
12
+
7
13
  const where =
8
14
  process.env.NODE_ENV !== 'production'
9
15
  ? {
@@ -86,26 +92,73 @@ exports.login = async (ctx) => {
86
92
  /**
87
93
  * 查出最新用户,存token并返回
88
94
  */
89
- result = await app.model.user.findOne({
90
- where: {
91
- id: result.id,
92
- },
93
- include: [
94
- {
95
- model: app.model.github_user,
96
- },
97
- {
95
+ const h5_user_include = config.includes('h5')
96
+ ? {
98
97
  model: app.model.h5_user,
99
- },
100
- {
98
+ where: {
99
+ appid: app_id,
100
+ },
101
+ attributes: {
102
+ exclude: ['created_at', 'updated_at', 'createdid'],
103
+ },
104
+ }
105
+ : {
106
+ model: app.model.h5_user,
107
+ }
108
+ const mp_user_include = config.includes('mp')
109
+ ? {
110
+ model: app.model.mp_user,
111
+ where: {
112
+ appid: app_id,
113
+ },
114
+ }
115
+ : {
101
116
  model: app.model.mp_user,
117
+ }
118
+ const includeDefault = [
119
+ {
120
+ model: app.model.github_user,
121
+ },
122
+ h5_user_include,
123
+ mp_user_include,
124
+ {
125
+ model: app.model.toutiao_user,
126
+ },
127
+ ...app.include.user,
128
+ ].filter((item) => item.model)
129
+
130
+ if (app.appConfig.loginData) {
131
+ const include = includeDefault.filter((i) => {
132
+ return app.appConfig.loginData.excludeInclude.every((m) => {
133
+ return app.model[m] !== i.model
134
+ })
135
+ })
136
+ const { exclude_list = [] } = await getAppConfig({ app, config })
137
+ const attributes = {
138
+ exclude: Array.from(
139
+ new Set([
140
+ ...app.appConfig.loginData.attributes.exclude,
141
+ ...(Array.isArray(exclude_list) ? exclude_list : []),
142
+ ])
143
+ ),
144
+ }
145
+ result = await app.model.user.findOne({
146
+ where: {
147
+ id: result.id,
102
148
  },
103
- {
104
- model: app.model.toutiao_user,
149
+ attributes,
150
+ include,
151
+ })
152
+ } else {
153
+ result = await app.model.user.findOne({
154
+ where: {
155
+ id: result.id,
105
156
  },
106
- ...app.include.user,
107
- ].filter((item) => item.model),
108
- })
157
+ include: includeDefault,
158
+ })
159
+ }
160
+
161
+ if (!result) throw new Error('账号密码错误,或者不属于当前应用')
109
162
 
110
163
  const tokenResult = {
111
164
  id: result.id,
@@ -133,7 +186,9 @@ exports.login = async (ctx) => {
133
186
 
134
187
  return ctx.SUCCESS({
135
188
  token,
136
- user: result,
189
+ user: app.appConfig.loginData
190
+ ? lodash.omit(result.toJSON(), app.appConfig.loginData.omit)
191
+ : result,
137
192
  })
138
193
  }
139
194
 
@@ -11,6 +11,7 @@ const WXBizDataCrypt = require('../../services/weixinCrypt')
11
11
  const weixinArticle = require('../../services/weixinArticle')
12
12
  const fxp = require('fast-xml-parser')
13
13
  const crypto = require('crypto')
14
+ const { getAppConfig } = require('../../utils')
14
15
 
15
16
  const check = ({ timestamp, nonce, signature, token }) => {
16
17
  const tmp = [token, timestamp, nonce].sort().join('')
@@ -387,25 +388,74 @@ exports.mp_login = async (ctx) => {
387
388
  }
388
389
  }
389
390
 
390
- const result = await app.model.user.findOne({
391
- where: {
392
- id: uid,
393
- },
394
- include: [
395
- {
391
+ const h5_user_include = config.includes('h5')
392
+ ? {
396
393
  model: app.model.h5_user,
397
- },
398
- {
394
+ where: {
395
+ appid: app_id,
396
+ },
397
+ attributes: {
398
+ exclude: ['created_at', 'updated_at', 'createdid'],
399
+ },
400
+ }
401
+ : {
402
+ model: app.model.h5_user,
403
+ }
404
+ const mp_user_include = config.includes('mp')
405
+ ? {
399
406
  model: app.model.mp_user,
407
+ where: {
408
+ appid: app_id,
409
+ },
410
+ }
411
+ : {
412
+ model: app.model.mp_user,
413
+ }
414
+ let result
415
+ const includeDefault = [
416
+ {
417
+ model: app.model.github_user,
418
+ },
419
+ h5_user_include,
420
+ mp_user_include,
421
+ {
422
+ model: app.model.toutiao_user,
423
+ },
424
+ ...app.include.user,
425
+ ].filter((item) => item.model)
426
+
427
+ if (app.appConfig.loginData) {
428
+ const include = includeDefault.filter((i) => {
429
+ return app.appConfig.loginData.excludeInclude.every((m) => {
430
+ return app.model[m] !== i.model
431
+ })
432
+ })
433
+ const { exclude_list = [] } = await getAppConfig({ app, config })
434
+ const attributes = {
435
+ exclude: Array.from(
436
+ new Set([
437
+ ...app.appConfig.loginData.attributes.exclude,
438
+ ...(Array.isArray(exclude_list) ? exclude_list : []),
439
+ ])
440
+ ),
441
+ }
442
+ result = await app.model.user.findOne({
443
+ where: {
444
+ id: uid,
400
445
  },
401
- {
402
- model: app.model.toutiao_user,
446
+ attributes,
447
+ include,
448
+ })
449
+ } else {
450
+ result = await app.model.user.findOne({
451
+ where: {
452
+ id: uid,
403
453
  },
404
- ...app.include.user,
405
- ].filter((item) => item.model),
406
- })
454
+ include: includeDefault,
455
+ })
456
+ }
407
457
 
408
- if (!result.id) throw new Error('登录失败')
458
+ if (!(result && result.id)) throw new Error('登录失败')
409
459
 
410
460
  const tokenResult = {
411
461
  id: result.id,
@@ -434,7 +484,9 @@ exports.mp_login = async (ctx) => {
434
484
 
435
485
  return ctx.SUCCESS({
436
486
  token,
437
- user: result,
487
+ user: app.appConfig.loginData
488
+ ? lodash.omit(result.toJSON(), app.appConfig.loginData.omit)
489
+ : result,
438
490
  })
439
491
  }
440
492
 
@@ -898,7 +950,7 @@ exports.mp_pay = async (ctx) => {
898
950
  throw new Error(
899
951
  payResult.return_msg === '签名错误,请检查后再试'
900
952
  ? payResult.return_msg + '(商户号/APIv2/关联)'
901
- : payResult.return_msg
953
+ : payResult.err_code_des + '(商户号/APIv2/关联)'
902
954
  )
903
955
  }
904
956
  throw new Error(`支付失败,请重试`)
@@ -1,5 +1,12 @@
1
1
  const { lodash } = require('q-koa')
2
2
 
3
+ const findConfig = (type) => (item) =>
4
+ item.key === type ||
5
+ item.config === type ||
6
+ item.pay_config === type ||
7
+ (item.ToUserName && item.ToUserName === type) ||
8
+ (item.appId && item.appId === type)
9
+
3
10
  exports.getSetting = (config) => (type) =>
4
11
  config.find((i) => i.key === type).value
5
12
 
@@ -34,18 +41,12 @@ exports.getObject = (config, app) => async (type) => {
34
41
  flag = true
35
42
  }
36
43
 
37
- const findConfig = (item) =>
38
- item.key === type ||
39
- item.config === type ||
40
- item.pay_config === type ||
41
- (item.ToUserName && item.ToUserName === type) ||
42
- (item.appId && item.appId === type)
43
44
  if (app) {
44
45
  const applicationList = app.cache.get('application')
45
- if (applicationList && applicationList.find(findConfig)) {
46
+ if (applicationList && applicationList.find(findConfig(type))) {
46
47
  flag = true
47
48
  return {
48
- ...applicationList.find(findConfig),
49
+ ...applicationList.find(findConfig(type)),
49
50
  ...temObj,
50
51
  }
51
52
  }
@@ -58,7 +59,7 @@ exports.getObject = (config, app) => async (type) => {
58
59
  const formatData = JSON.parse(JSON.stringify(result))
59
60
  app.cache.set('application', formatData)
60
61
 
61
- const target = formatData.find(findConfig)
62
+ const target = formatData.find(findConfig(type))
62
63
  if (target) {
63
64
  flag = true
64
65
  return {
@@ -159,12 +160,12 @@ exports.getImageType = (fileBuffer) => {
159
160
  return ''
160
161
  }
161
162
 
162
- exports.getAppConfig = async ({ app, config }) => {
163
- if (!config || !app) return null
163
+ exports.getAppConfig = async ({ app, config, key }) => {
164
+ if (!(app && (config || key))) return null
164
165
  const application =
165
166
  app.cache.get('application') || (await app.model.application.findAll())
166
167
 
167
- const target = application.find((a) => a.config === config)
168
+ const target = application.find(findConfig(config || key))
168
169
 
169
170
  return target
170
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "9.8.0",
3
+ "version": "9.8.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {