q-koa 9.8.0 → 9.8.1

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.
@@ -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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "9.8.0",
3
+ "version": "9.8.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {