q-koa 8.0.2 → 8.0.5

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
@@ -70,7 +70,16 @@ class APP {
70
70
  constructor(_config) {
71
71
  const config = _.defaultsDeep(_config, defaultConfig)
72
72
  const { port } = config
73
- this.config = config
73
+ this.config = {
74
+ ...config,
75
+ includes: config.includes.map((item) => {
76
+ return typeof item === 'string'
77
+ ? {
78
+ item,
79
+ }
80
+ : item
81
+ }),
82
+ }
74
83
  this.port = port
75
84
  this.app = new Koa()
76
85
  this.server = null
@@ -244,11 +253,12 @@ class APP {
244
253
  // });
245
254
 
246
255
  starter.on('loadAll', async () => {
247
- const appName = this.config.includes[0]
256
+ const routerName = this.config.includes[0].routerName
257
+ const appName = this.config.includes[0].dirName
248
258
  this.server = this.app.listen(this.port, () => {
249
259
  console.log(`server is running at http://localhost:${this.port}`)
250
260
  console.log(
251
- `first app is http://localhost:3001/${appName}/setting/findOne`
261
+ `first app is http://localhost:3001/${routerName}/setting/findOne`
252
262
  )
253
263
  })
254
264
 
@@ -295,20 +305,17 @@ class APP {
295
305
  }
296
306
 
297
307
  async initApp() {
298
- let dirList = await fsPromise.readdir(
299
- path.resolve(__dirname, `${rootDirectory}/${APP_DIR}`)
300
- )
301
- dirList = dirList.filter((item) => this.config.includes.includes(item))
302
-
308
+ if (this.config.includes.length === 0) throw new Error('请填入项目')
303
309
  let dbFlag = false
304
- for (let i = 0; i < dirList.length; i++) {
305
- const appName = dirList[i]
310
+ for (let i = 0; i < this.config.includes.length; i++) {
311
+ const routerName = this.config.includes[i].routerName
312
+ const appName = this.config.includes[i].dirName
306
313
 
307
314
  console.log(
308
315
  chalk.green(`==================== 启动 ${appName} ====================`)
309
316
  )
310
317
  const routerConfig = {
311
- prefix: `/${appName}`,
318
+ prefix: `/${routerName}`,
312
319
  }
313
320
  const router = new Router(routerConfig)
314
321
  Serve(
@@ -784,7 +791,10 @@ class APP {
784
791
 
785
792
  initRouter(router) {
786
793
  router.post('/:controller', async (ctx, next) => {
787
- const appName = router.opts.prefix.substring(1)
794
+ const routerName = router.opts.prefix.substring(1)
795
+ const appName = this.config.includes.find(
796
+ (i) => i.routerName === routerName
797
+ )
788
798
  const { app } = ctx
789
799
  const { controller } = ctx.params
790
800
  if (
@@ -796,7 +806,10 @@ class APP {
796
806
  await next()
797
807
  })
798
808
  const handleRouter = async (ctx) => {
799
- const appName = router.opts.prefix.substring(1)
809
+ const routerName = router.opts.prefix.substring(1)
810
+ const appName = this.config.includes.find(
811
+ (i) => i.routerName === routerName
812
+ ).dirName
800
813
  const { app } = ctx
801
814
 
802
815
  const { controller, fn, sub } = ctx.params
@@ -149,10 +149,10 @@ exports.mp_getPhone = async (ctx) => {
149
149
  exports.getPhone = async (ctx) => {
150
150
  const { app } = getAppByCtx(ctx)
151
151
 
152
- const { code, iv, encryptedData } = ctx.request.body
152
+ const { code, iv, encryptedData, config = 'weixin_mp' } = ctx.request.body
153
153
 
154
154
  const appConfig = getConfig(app)
155
- const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
155
+ const { app_id, app_secrect } = await appConfig.getObject(config)
156
156
 
157
157
  const weixinLoginUrl = `https://api.weixin.qq.com/sns/jscode2session?appid=${app_id}&secret=${app_secrect}&js_code=${code}&grant_type=authorization_code`
158
158
 
@@ -918,15 +918,57 @@ exports.notify = async (ctx) => {
918
918
  ctx.res.end(returnResult)
919
919
  }
920
920
 
921
+ exports.url_link = async (ctx) => {
922
+ const { app } = getAppByCtx(ctx)
923
+
924
+ const { page, scene, config = 'weixin_mp' } = ctx.request.body
925
+ const appConfig = getConfig(app)
926
+ const { app_id, app_secrect } = await appConfig.getObject(config)
927
+ const weixinMp = new WeixinMp({
928
+ appid: app_id,
929
+ secrect: app_secrect,
930
+ })
931
+ weixinMp.init()
932
+ const result = await weixinMp.createUrlLink({
933
+ page,
934
+ scene,
935
+ })
936
+ ctx.SUCCESS(result)
937
+ }
938
+
939
+ exports.url_scheme = async (ctx) => {
940
+ const { app } = getAppByCtx(ctx)
941
+
942
+ const { page, scene, config = 'weixin_mp' } = ctx.request.body
943
+ const appConfig = getConfig(app)
944
+ const { app_id, app_secrect } = await appConfig.getObject(config)
945
+ const weixinMp = new WeixinMp({
946
+ appid: app_id,
947
+ secrect: app_secrect,
948
+ })
949
+ weixinMp.init()
950
+ const result = await weixinMp.createUrlscheme({
951
+ page,
952
+ scene,
953
+ })
954
+ ctx.SUCCESS(result)
955
+ }
956
+
921
957
  exports.qr_code = async (ctx) => {
922
958
  const { app } = getAppByCtx(ctx)
923
959
 
924
- const { page, is_hyaline, scene, is_oss = false } = ctx.request.body
960
+ const {
961
+ page,
962
+ is_hyaline,
963
+ scene,
964
+ is_oss = false,
965
+ config = 'weixin_mp',
966
+ } = ctx.request.body
925
967
 
926
968
  // if (!lodash.get(ctx.request, `${appName}-user.id`, null)) throw new Error('没有登录')
927
969
 
928
970
  const appConfig = getConfig(app)
929
- const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
971
+ const { app_id, app_secrect } = await appConfig.getObject(config)
930
972
 
931
973
  const { site_host } = await appConfig.getObject('base')
932
974
  const weixinMp = new WeixinMp({
@@ -984,10 +1026,16 @@ exports.qr_code = async (ctx) => {
984
1026
  exports.sendMessage = async (ctx) => {
985
1027
  const { app } = getAppByCtx(ctx)
986
1028
 
987
- const { openid, template_id, data, page } = ctx.request.body
1029
+ const {
1030
+ openid,
1031
+ template_id,
1032
+ data,
1033
+ page,
1034
+ config = 'weixin_mp',
1035
+ } = ctx.request.body
988
1036
 
989
1037
  const appConfig = getConfig(app)
990
- const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
1038
+ const { app_id, app_secrect } = await appConfig.getObject(config)
991
1039
 
992
1040
  const weixinMp = new WeixinMp({
993
1041
  appid: app_id,
@@ -8,6 +8,10 @@ const getPhoneNumberUrl =
8
8
  'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s'
9
9
  const createQRCodeUrl =
10
10
  'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s'
11
+ const createUrlschemeUrl =
12
+ 'https://api.weixin.qq.com/wxa/generatescheme?access_token=%s'
13
+ const createUrlLinkUrl =
14
+ 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s'
11
15
  const sendMessageUrl =
12
16
  'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s'
13
17
  const handleMessageUrl =
@@ -41,6 +45,7 @@ const cache = new LRU({
41
45
  maxAge: 1000 * 60 * 60,
42
46
  })
43
47
  const uuid = require('node-uuid')
48
+ const { rest } = require('lodash')
44
49
 
45
50
  const uploadPromise = (options) => {
46
51
  return new Promise((resolve, reject) => {
@@ -296,6 +301,60 @@ module.exports = class Singleton {
296
301
  return result.phone_info
297
302
  }
298
303
 
304
+ async createUrlLink(options) {
305
+ const { page, scene, ...rest } = options
306
+ const access_token = await this.getAccessToken()
307
+ const url = util.format(createUrlLinkUrl, access_token)
308
+ const payLoad = {
309
+ ...rest,
310
+ path: options.page,
311
+ query: options.scene
312
+ ? Object.keys(options.scene)
313
+ .map((item) => `${item}=${options.scene[item]}`)
314
+ .join('&')
315
+ : '',
316
+ }
317
+
318
+ const result = await axios.post(url, payLoad).then((res) => res.data)
319
+
320
+ if (result.errcode) {
321
+ if (result.errcode === 40001) {
322
+ cache.reset()
323
+ return await this.getPhoneNumber(code)
324
+ }
325
+ throw new Error(`${result.errcode};${result.errmsg}`)
326
+ }
327
+ return result.url_link
328
+ }
329
+
330
+ async createUrlscheme(options) {
331
+ const { page, scene, ...rest } = options
332
+ const access_token = await this.getAccessToken()
333
+ const url = util.format(createUrlschemeUrl, access_token)
334
+ const payLoad = {
335
+ ...rest,
336
+ jump_wxa: {
337
+ query: options.scene
338
+ ? Object.keys(options.scene)
339
+ .map((item) => `${item}=${options.scene[item]}`)
340
+ .join('&')
341
+ : '',
342
+ path: options.page,
343
+ },
344
+ }
345
+
346
+ const result = await axios.post(url, payLoad).then((res) => res.data)
347
+
348
+ if (result.errcode) {
349
+ if (result.errcode === 40001) {
350
+ cache.reset()
351
+ return await this.getPhoneNumber(code)
352
+ }
353
+ throw new Error(`${result.errcode};${result.errmsg}`)
354
+ }
355
+ return result.openlink
356
+ }
357
+
299
358
  async createQR(options) {
300
359
  if (!this.config.targetPath) throw new Error('没有配置文件夹')
301
360
  const access_token = await this.getAccessToken()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "8.0.2",
3
+ "version": "8.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -40,6 +40,7 @@
40
40
  "koa-static": "^5.0.0",
41
41
  "koa-useragent": "^4.0.0",
42
42
  "koa-xml-body": "^2.1.0",
43
+ "koa-proxies": "^0.12.2",
43
44
  "lodash": "^4.17.15",
44
45
  "lru-cache": "^5.1.1",
45
46
  "mockjs": "^1.0.1-beta3",