q-koa 8.0.2 → 8.0.3

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.
@@ -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,39 @@ exports.notify = async (ctx) => {
918
918
  ctx.res.end(returnResult)
919
919
  }
920
920
 
921
+ exports.urlScheme = 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.createUrlscheme({
933
+ page,
934
+ scene,
935
+ })
936
+ ctx.SUCCESS(result)
937
+ }
938
+
921
939
  exports.qr_code = async (ctx) => {
922
940
  const { app } = getAppByCtx(ctx)
923
941
 
924
- const { page, is_hyaline, scene, is_oss = false } = ctx.request.body
942
+ const {
943
+ page,
944
+ is_hyaline,
945
+ scene,
946
+ is_oss = false,
947
+ config = 'weixin_mp',
948
+ } = ctx.request.body
925
949
 
926
950
  // if (!lodash.get(ctx.request, `${appName}-user.id`, null)) throw new Error('没有登录')
927
951
 
928
952
  const appConfig = getConfig(app)
929
- const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
953
+ const { app_id, app_secrect } = await appConfig.getObject(config)
930
954
 
931
955
  const { site_host } = await appConfig.getObject('base')
932
956
  const weixinMp = new WeixinMp({
@@ -984,10 +1008,16 @@ exports.qr_code = async (ctx) => {
984
1008
  exports.sendMessage = async (ctx) => {
985
1009
  const { app } = getAppByCtx(ctx)
986
1010
 
987
- const { openid, template_id, data, page } = ctx.request.body
1011
+ const {
1012
+ openid,
1013
+ template_id,
1014
+ data,
1015
+ page,
1016
+ config = 'weixin_mp',
1017
+ } = ctx.request.body
988
1018
 
989
1019
  const appConfig = getConfig(app)
990
- const { app_id, app_secrect } = await appConfig.getObject('weixin_mp')
1020
+ const { app_id, app_secrect } = await appConfig.getObject(config)
991
1021
 
992
1022
  const weixinMp = new WeixinMp({
993
1023
  appid: app_id,
@@ -8,6 +8,8 @@ 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'
11
13
  const sendMessageUrl =
12
14
  'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s'
13
15
  const handleMessageUrl =
@@ -41,6 +43,7 @@ const cache = new LRU({
41
43
  maxAge: 1000 * 60 * 60,
42
44
  })
43
45
  const uuid = require('node-uuid')
46
+ const { rest } = require('lodash')
44
47
 
45
48
  const uploadPromise = (options) => {
46
49
  return new Promise((resolve, reject) => {
@@ -296,6 +299,34 @@ module.exports = class Singleton {
296
299
  return result.phone_info
297
300
  }
298
301
 
302
+ async createUrlscheme(options) {
303
+ const { page, scene, ...rest } = options
304
+ const access_token = await this.getAccessToken()
305
+ const url = util.format(createUrlschemeUrl, access_token)
306
+ const payLoad = {
307
+ ...rest,
308
+ jump_wxa: {
309
+ query: options.scene
310
+ ? Object.keys(options.scene)
311
+ .map((item) => `${item}=${options.scene[item]}`)
312
+ .join('&')
313
+ : '',
314
+ path: options.page,
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.openlink
328
+ }
329
+
299
330
  async createQR(options) {
300
331
  if (!this.config.targetPath) throw new Error('没有配置文件夹')
301
332
  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.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {