q-koa 13.8.55 → 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
CHANGED
|
@@ -8,7 +8,6 @@ const Sequelize = require('sequelize')
|
|
|
8
8
|
const redis = require('redis')
|
|
9
9
|
const moment = require('moment')
|
|
10
10
|
const { EventEmitter } = require('events')
|
|
11
|
-
const nodeVm = require('vm')
|
|
12
11
|
const { LRUCache } = require('lru-cache')
|
|
13
12
|
const axios = require('axios')
|
|
14
13
|
|
|
@@ -67,7 +66,7 @@ const starter = new EventEmitter()
|
|
|
67
66
|
const formatPostFunction = (str) => `eval(${str})`
|
|
68
67
|
|
|
69
68
|
const runVm = (_str) => {
|
|
70
|
-
return
|
|
69
|
+
return new Function(`return ${_str}`)()
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
const getLiteral = (str) => {
|
|
@@ -371,6 +370,16 @@ class APP {
|
|
|
371
370
|
|
|
372
371
|
const version = require('q-koa/package.json').version
|
|
373
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
|
+
})
|
|
374
383
|
})
|
|
375
384
|
|
|
376
385
|
// keep-alive 超时必须大于负载均衡的空闲连接超时(CLB/ALB 默认 15 秒),
|
|
@@ -550,106 +550,6 @@ exports.initDataWithCache = exports.initData = async (ctx) => {
|
|
|
550
550
|
return ctx.SUCCESS(result)
|
|
551
551
|
}
|
|
552
552
|
|
|
553
|
-
exports.recentlyError = async (ctx) => {
|
|
554
|
-
// const {
|
|
555
|
-
// app,
|
|
556
|
-
// appName,
|
|
557
|
-
// } = getAppByCtx(ctx);
|
|
558
|
-
|
|
559
|
-
if (ctx.ws) {
|
|
560
|
-
const ws = await ctx.ws()
|
|
561
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
562
|
-
ctx.app.on('error', async (err) => {
|
|
563
|
-
console.log(err)
|
|
564
|
-
try {
|
|
565
|
-
send({
|
|
566
|
-
type: 'error',
|
|
567
|
-
payload: err ? err.errorData : {},
|
|
568
|
-
})
|
|
569
|
-
} catch (e) {
|
|
570
|
-
console.log(e)
|
|
571
|
-
}
|
|
572
|
-
})
|
|
573
|
-
} else {
|
|
574
|
-
throw new Error('这是一个websocket url')
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
exports.sandbox = async (ctx) => {
|
|
579
|
-
const code = `
|
|
580
|
-
module.exports = async (callback)=>{
|
|
581
|
-
callback({
|
|
582
|
-
name:1
|
|
583
|
-
})
|
|
584
|
-
}
|
|
585
|
-
`
|
|
586
|
-
// const { code } = ctx.request.body;
|
|
587
|
-
const vm = new VM({
|
|
588
|
-
sandbox: {
|
|
589
|
-
axios,
|
|
590
|
-
module,
|
|
591
|
-
setTimeout,
|
|
592
|
-
sleep: (time) =>
|
|
593
|
-
new Promise((resolve) => setTimeout(resolve, time * 1000)),
|
|
594
|
-
},
|
|
595
|
-
})
|
|
596
|
-
vm.run(code)(ctx.SUCCESS)
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
exports.onlineChat = async (ctx) => {
|
|
600
|
-
if (ctx.ws) {
|
|
601
|
-
const ws = await ctx.ws()
|
|
602
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
603
|
-
ws.on('message', async (code) => {
|
|
604
|
-
const vm = new VM({
|
|
605
|
-
sandbox: {
|
|
606
|
-
send: (data) => ws.send(JSON.stringify(data)),
|
|
607
|
-
axios,
|
|
608
|
-
},
|
|
609
|
-
})
|
|
610
|
-
vm.run(code)
|
|
611
|
-
const unhandledRejections = {}
|
|
612
|
-
|
|
613
|
-
process.on('unhandledRejection', (reason) => {
|
|
614
|
-
if (unhandledRejections[reason]) {
|
|
615
|
-
console.log('====?', reason)
|
|
616
|
-
} else {
|
|
617
|
-
send({
|
|
618
|
-
type: 'error',
|
|
619
|
-
payload: `${reason}`,
|
|
620
|
-
})
|
|
621
|
-
unhandledRejections[reason] = true
|
|
622
|
-
}
|
|
623
|
-
})
|
|
624
|
-
})
|
|
625
|
-
} else {
|
|
626
|
-
throw new Error('这是一个websocket url')
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
exports.chat = async (ctx) => {
|
|
631
|
-
const { app } = getAppByCtx(ctx)
|
|
632
|
-
if (ctx.ws) {
|
|
633
|
-
const ws = await ctx.ws()
|
|
634
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
635
|
-
ws.on('message', async (payload) => {
|
|
636
|
-
const message = JSON.parse(payload)
|
|
637
|
-
const result = await app.model.ask.create({
|
|
638
|
-
user_id: message.payload.user_id,
|
|
639
|
-
content: message.payload.content,
|
|
640
|
-
is_ask: Boolean(message.payload.user_id),
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
send({
|
|
644
|
-
type: 'message',
|
|
645
|
-
payload: result,
|
|
646
|
-
})
|
|
647
|
-
})
|
|
648
|
-
} else {
|
|
649
|
-
throw new Error('这是一个websocket url')
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
553
|
exports.showModelCount = async (ctx) => {
|
|
654
554
|
const { app, appName } = getAppByCtx(ctx)
|
|
655
555
|
const { is_count = false } = ctx.request.body
|
|
@@ -2,10 +2,9 @@ const lodash = require('lodash')
|
|
|
2
2
|
const { Sequelize } = require('q-koa')
|
|
3
3
|
const { formatLiteralObj, filterFunction } = require('../../utils')
|
|
4
4
|
const formatPostFunction = (str) => `eval(${str})`
|
|
5
|
-
const nodeVm = require('vm')
|
|
6
5
|
const axios = require('axios')
|
|
7
6
|
const runVm = (_str) => {
|
|
8
|
-
return
|
|
7
|
+
return new Function(`return ${_str}`)()
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
const memRunVm = lodash.memoize(runVm)
|
|
@@ -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
|
*/
|