q-koa 13.8.56 → 13.8.57
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 +10 -0
- package/core/file/plugins/weixin/controller.js +106 -0
- package/package.json +1 -1
package/core/app.js
CHANGED
|
@@ -370,6 +370,16 @@ class APP {
|
|
|
370
370
|
|
|
371
371
|
const version = require('q-koa/package.json').version
|
|
372
372
|
console.log(chalk.green(`version: ${version}`))
|
|
373
|
+
|
|
374
|
+
// 监听完成后,执行各 appConfig 中配置的 startHook,上下文为对应 app
|
|
375
|
+
Object.keys(this.app).forEach((name) => {
|
|
376
|
+
const startHook = _.get(this.app[name], 'appConfig.startHook')
|
|
377
|
+
if (typeof startHook === 'function') {
|
|
378
|
+
Promise.resolve(startHook(this.app[name])).catch((e) => {
|
|
379
|
+
console.log(chalk.red(`${name} startHook执行失败`), e)
|
|
380
|
+
})
|
|
381
|
+
}
|
|
382
|
+
})
|
|
373
383
|
})
|
|
374
384
|
|
|
375
385
|
// keep-alive 超时必须大于负载均衡的空闲连接超时(CLB/ALB 默认 15 秒),
|
|
@@ -452,6 +452,112 @@ exports.mp_getPhoneNew = async (ctx) => {
|
|
|
452
452
|
})
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
+
/**
|
|
456
|
+
* 小程序手机号实时认证组件(getRealtimePhoneNumber)
|
|
457
|
+
* 后端换取接口与普通手机号一致,code 来自实时认证组件回调
|
|
458
|
+
*/
|
|
459
|
+
exports.mp_getRealPhone = async (ctx) => {
|
|
460
|
+
const { app, appName } = getAppByCtx(ctx)
|
|
461
|
+
|
|
462
|
+
const { code, config = 'weixin_mp' } = ctx.request.body
|
|
463
|
+
|
|
464
|
+
const user_id = ctx.request[appName + '-user'].id
|
|
465
|
+
const appConfig = getConfig(app)
|
|
466
|
+
const { app_id, app_secrect } = await appConfig.getObject(config)
|
|
467
|
+
|
|
468
|
+
const weixinMp = new WeixinMp({
|
|
469
|
+
appid: app_id,
|
|
470
|
+
secrect: app_secrect,
|
|
471
|
+
})
|
|
472
|
+
weixinMp.init()
|
|
473
|
+
|
|
474
|
+
const { phoneNumber } = await weixinMp.getPhoneNumber(code)
|
|
475
|
+
|
|
476
|
+
if (!phoneNumber) throw new Error('微信获取手机失败,请重试')
|
|
477
|
+
|
|
478
|
+
const exist = await app.model.user.findOne({
|
|
479
|
+
where: {
|
|
480
|
+
mobile: phoneNumber,
|
|
481
|
+
},
|
|
482
|
+
include: [
|
|
483
|
+
{
|
|
484
|
+
model: app.model.mp_user,
|
|
485
|
+
},
|
|
486
|
+
],
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
const mobileExist =
|
|
490
|
+
exist &&
|
|
491
|
+
exist.mp_user &&
|
|
492
|
+
(!exist.mp_user.appid || exist.mp_user.appid === app_id)
|
|
493
|
+
|
|
494
|
+
if (mobileExist) {
|
|
495
|
+
return ctx.ERROR('系统已存在该手机号')
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
await app.model.user.upsert({
|
|
499
|
+
id: user_id,
|
|
500
|
+
mobile: phoneNumber,
|
|
501
|
+
})
|
|
502
|
+
|
|
503
|
+
const result = await app.model.user.findOne({
|
|
504
|
+
where: {
|
|
505
|
+
id: user_id,
|
|
506
|
+
},
|
|
507
|
+
include: [
|
|
508
|
+
{
|
|
509
|
+
model: app.model.h5_user,
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
model: app.model.mp_user,
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
model: app.model.toutiao_user,
|
|
516
|
+
},
|
|
517
|
+
...app.include.user,
|
|
518
|
+
].filter((item) => item.model),
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
const is_token_name = app.appConfig.is_token_name
|
|
522
|
+
|
|
523
|
+
const tokenResult = {
|
|
524
|
+
id: result.id,
|
|
525
|
+
...(is_token_name
|
|
526
|
+
? {
|
|
527
|
+
name: result.name,
|
|
528
|
+
}
|
|
529
|
+
: {}),
|
|
530
|
+
mp_user: result.mp_user
|
|
531
|
+
? {
|
|
532
|
+
openid: result.mp_user && result.mp_user.openid,
|
|
533
|
+
}
|
|
534
|
+
: null,
|
|
535
|
+
...(result.h5_user
|
|
536
|
+
? {
|
|
537
|
+
h5_user: {
|
|
538
|
+
openid: result.h5_user && result.h5_user.openid,
|
|
539
|
+
},
|
|
540
|
+
}
|
|
541
|
+
: {}),
|
|
542
|
+
...(result.toutiao_user
|
|
543
|
+
? {
|
|
544
|
+
toutiao_user: {
|
|
545
|
+
openid: result.toutiao_user && result.toutiao_user.openid,
|
|
546
|
+
},
|
|
547
|
+
}
|
|
548
|
+
: {}),
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const token = await app.sign({
|
|
552
|
+
user: tokenResult,
|
|
553
|
+
})
|
|
554
|
+
|
|
555
|
+
return ctx.SUCCESS({
|
|
556
|
+
token,
|
|
557
|
+
user: result,
|
|
558
|
+
})
|
|
559
|
+
}
|
|
560
|
+
|
|
455
561
|
/**
|
|
456
562
|
* 微信小程序登录
|
|
457
563
|
*/
|