q-koa 13.6.4 → 13.6.6

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,20 +1,13 @@
1
1
  const Core = require('@alicloud/pop-core')
2
- const { lodash } = require('q-koa')
3
2
  module.exports = class Singleton {
3
+ static _instanceMap = new Map()
4
4
  constructor(key) {
5
- this.key = key
6
-
7
- /**
8
- * 单例模式
9
- */
10
- if (!Singleton.instance) {
11
- Singleton.instance = this
5
+ const cacheKey = key.accessKeyId
6
+ if (Singleton._instanceMap.has(cacheKey)) {
7
+ return Singleton._instanceMap.get(cacheKey)
12
8
  }
13
- const previous = Singleton.instance.getConfig()
14
- if (!lodash.isEqual(previous, key)) {
15
- Singleton.instance = this
16
- }
17
- return Singleton.instance
9
+ this.key = key
10
+ Singleton._instanceMap.set(cacheKey, this)
18
11
  }
19
12
 
20
13
  init() {
@@ -1,7 +1,11 @@
1
1
  const crypto = require('crypto')
2
- const { lodash } = require('q-koa')
3
2
  module.exports = class Singleton {
3
+ static _instanceMap = new Map()
4
4
  constructor(config) {
5
+ const key = config.partner
6
+ if (Singleton._instanceMap.has(key)) {
7
+ return Singleton._instanceMap.get(key)
8
+ }
5
9
  this.config = {
6
10
  service: 'alipay.wap.create.direct.pay.by.user',
7
11
  partner: '',
@@ -19,17 +23,7 @@ module.exports = class Singleton {
19
23
  return_url: 'http://www.baidu.com',
20
24
  ...config,
21
25
  }
22
- /**
23
- * 单例模式
24
- */
25
- if (!Singleton.instance) {
26
- Singleton.instance = this
27
- }
28
- const previous = Singleton.instance.getConfig()
29
- if (!lodash.isEqual(previous, config)) {
30
- Singleton.instance = this
31
- }
32
- return Singleton.instance
26
+ Singleton._instanceMap.set(key, this)
33
27
  }
34
28
 
35
29
  async create() {
@@ -1,5 +1,4 @@
1
1
  const axios = require('axios')
2
- const { lodash } = require('q-koa')
3
2
 
4
3
  const queryFn = (obj) => {
5
4
  const query = Object.keys(obj)
@@ -10,20 +9,13 @@ const queryFn = (obj) => {
10
9
  return query.replace('&', '?')
11
10
  }
12
11
  module.exports = class Singleton {
12
+ static _instanceMap = new Map()
13
13
  constructor(key) {
14
- this.key = key
15
-
16
- /**
17
- * 单例模式
18
- */
19
- if (!Singleton.instance) {
20
- Singleton.instance = this
14
+ if (Singleton._instanceMap.has(key)) {
15
+ return Singleton._instanceMap.get(key)
21
16
  }
22
- const previous = Singleton.instance.getConfig()
23
- if (!lodash.isEqual(previous, key)) {
24
- Singleton.instance = this
25
- }
26
- return Singleton.instance
17
+ this.key = key
18
+ Singleton._instanceMap.set(key, this)
27
19
  }
28
20
 
29
21
  async geo({ address }) {
@@ -1,6 +1,5 @@
1
1
  const util = require('util')
2
2
  const axios = require('axios')
3
- const { lodash } = require('q-koa')
4
3
 
5
4
  const commonTokenUrl =
6
5
  'https://open.douyin.com/oauth/client_token/?client_key=%s&client_secret=%s&grant_type=client_credential'
@@ -20,28 +19,23 @@ const cache = new LRUCache({
20
19
  })
21
20
 
22
21
  module.exports = class Singleton {
22
+ static _instanceMap = new Map()
23
23
  constructor(config) {
24
- this.config = {
25
- ...config,
24
+ const key = config.client_key
25
+ if (Singleton._instanceMap.has(key)) {
26
+ return Singleton._instanceMap.get(key)
26
27
  }
27
-
28
- /**
29
- * 单例模式
30
- */
31
- if (!Singleton.instance) {
32
- Singleton.instance = this
28
+ if (!config.client_key || !config.client_secret) {
29
+ throw new Error('没有配置client_key或client_secret')
33
30
  }
34
- const previous = Singleton.instance.getConfig()
35
- if (!lodash.isEqual(previous, config)) {
36
- Singleton.instance = this
31
+ this.config = {
32
+ ...config,
37
33
  }
38
- return Singleton.instance
34
+ Singleton._instanceMap.set(key, this)
39
35
  }
40
36
 
41
37
  init() {
42
- if (!this.config.client_key || !this.config.client_secret) {
43
- throw new Error('没有配置client_key或client_secret')
44
- }
38
+ return this
45
39
  }
46
40
 
47
41
  async getAccessToken() {
@@ -1,25 +1,18 @@
1
1
  const axios = require('axios')
2
- const { lodash } = require('q-koa')
3
2
  const { push } = require('../plugins/log/service')
4
3
 
5
4
  const send_url = 'http://wuliu.market.alicloudapi.com/kdi'
6
5
  module.exports = class Singleton {
6
+ static _instanceMap = new Map()
7
7
  constructor(config) {
8
+ const key = config.app_code
9
+ if (Singleton._instanceMap.has(key)) {
10
+ return Singleton._instanceMap.get(key)
11
+ }
8
12
  this.config = {
9
13
  app_code: config.app_code,
10
14
  }
11
-
12
- /**
13
- * 单例模式
14
- */
15
- if (!Singleton.instance) {
16
- Singleton.instance = this
17
- }
18
- const previous = Singleton.instance.getConfig()
19
- if (!lodash.isEqual(previous, config)) {
20
- Singleton.instance = this
21
- }
22
- return Singleton.instance
15
+ Singleton._instanceMap.set(key, this)
23
16
  }
24
17
 
25
18
  async send({ no }) {
@@ -1,25 +1,17 @@
1
1
  const axios = require('axios')
2
2
  const util = require('util')
3
- const { lodash } = require('q-koa')
4
3
  const toLocationUrl =
5
4
  'https://apis.map.qq.com/ws/geocoder/v1/?location=%s&key=%s'
6
5
  const toLngAndLatUrl =
7
6
  'https://apis.map.qq.com/ws/geocoder/v1/?address=%s&key=%s'
8
7
  module.exports = class Singleton {
8
+ static _instanceMap = new Map()
9
9
  constructor(key) {
10
- this.key = key
11
-
12
- /**
13
- * 单例模式
14
- */
15
- if (!Singleton.instance) {
16
- Singleton.instance = this
10
+ if (Singleton._instanceMap.has(key)) {
11
+ return Singleton._instanceMap.get(key)
17
12
  }
18
- const previous = Singleton.instance.getConfig()
19
- if (!lodash.isEqual(previous, key)) {
20
- Singleton.instance = this
21
- }
22
- return Singleton.instance
13
+ this.key = key
14
+ Singleton._instanceMap.set(key, this)
23
15
  }
24
16
 
25
17
  async direction({
@@ -1,4 +1,4 @@
1
- const { lodash, getConfig } = require('q-koa')
1
+ const { getConfig } = require('q-koa')
2
2
  const yly = require('yly-nodejs-sdk')
3
3
  // const LRU = require('lru-cache')
4
4
  // const cache = new LRU({
@@ -7,22 +7,16 @@ const yly = require('yly-nodejs-sdk')
7
7
  // })
8
8
  // const cacheKey = Symbol('print')
9
9
  module.exports = class Singleton {
10
+ static _instanceMap = new Map()
10
11
  constructor(config) {
12
+ const key = config.cid
13
+ if (Singleton._instanceMap.has(key)) {
14
+ return Singleton._instanceMap.get(key)
15
+ }
11
16
  this.config = {
12
17
  ...config,
13
18
  }
14
-
15
- /**
16
- * 单例模式
17
- */
18
- if (!Singleton.instance) {
19
- Singleton.instance = this
20
- }
21
- const previous = Singleton.instance.getConfig()
22
- if (!lodash.isEqual(previous, config)) {
23
- Singleton.instance = this
24
- }
25
- return Singleton.instance
19
+ Singleton._instanceMap.set(key, this)
26
20
  }
27
21
 
28
22
  async getAccessToken() {
@@ -1,7 +1,6 @@
1
1
  const util = require('util')
2
2
  const axios = require('axios')
3
3
  const fsPromise = require('fs/promises')
4
- const { lodash } = require('q-koa')
5
4
  const getAccessTokenUrl =
6
5
  'https://developer.toutiao.com/api/apps/token?grant_type=%s&appid=%s&secret=%s'
7
6
  const createQRCodeUrl = 'https://developer.toutiao.com/api/apps/qrcode'
@@ -15,28 +14,24 @@ const cache = new LRUCache({
15
14
  const uuid = require('node-uuid')
16
15
 
17
16
  module.exports = class Singleton {
17
+ static _instanceMap = new Map()
18
18
  constructor(config) {
19
+ const key = config.appid
20
+ if (Singleton._instanceMap.has(key)) {
21
+ return Singleton._instanceMap.get(key)
22
+ }
23
+ if (!config.appid || !config.secrect) {
24
+ throw new Error('没有配置appid或secrect')
25
+ }
19
26
  this.config = {
20
27
  ...config,
21
28
  grant_type: 'client_credential',
22
29
  }
23
-
24
- /**
25
- * 单例模式
26
- */
27
- if (!Singleton.instance) {
28
- Singleton.instance = this
29
- }
30
- const previous = Singleton.instance.getConfig()
31
- if (!lodash.isEqual(previous, config)) {
32
- Singleton.instance = this
33
- }
34
- return Singleton.instance
30
+ Singleton._instanceMap.set(key, this)
35
31
  }
36
32
 
37
33
  init() {
38
- if (!this.config.appid || !this.config.secrect)
39
- throw new Error('没有配置appid或secrect')
34
+ return this
40
35
  }
41
36
 
42
37
  async getAccessToken() {
@@ -9,31 +9,27 @@ const cache = new LRUCache({
9
9
  max: 100,
10
10
  ttl: 1000 * 60 * 60,
11
11
  })
12
- const { lodash } = require('q-koa')
13
12
 
14
13
  module.exports = class Singleton {
14
+ static _instanceMap = new Map()
15
15
  constructor(config) {
16
+ const key = config.appid
17
+ if (Singleton._instanceMap.has(key)) {
18
+ return Singleton._instanceMap.get(key)
19
+ }
20
+ if (!config.appid || !config.secrect) {
21
+ throw new Error('没有配置appid或secrect')
22
+ }
16
23
  this.config = {
17
24
  ...config,
18
25
 
19
26
  noncestr: 'kuashou',
20
27
  grant_type: 'client_credential',
21
28
  }
22
- /**
23
- * 单例模式
24
- */
25
- if (!Singleton.instance) {
26
- Singleton.instance = this
27
- }
28
- const previous = Singleton.instance.getConfig()
29
- if (!lodash.isEqual(previous, config)) {
30
- Singleton.instance = this
31
- }
32
- return Singleton.instance
29
+ Singleton._instanceMap.set(key, this)
33
30
  }
34
31
  init() {
35
- if (!this.config.appid || !this.config.secrect)
36
- throw new Error('没有配置appid或secrect')
32
+ return this
37
33
  }
38
34
  async getAccessToken() {
39
35
  const { appid, secrect, grant_type } = this.config
@@ -1,3 +1,13 @@
1
+ /**
2
+ * weixinMP.js — 按静态 Map 实例缓存优化后
3
+ *
4
+ * 改动点:
5
+ * 1. 删除 Singleton.instance + lodash.isEqual 的假单例
6
+ * 2. 新增 static _instanceMap = new Map(),key 为 config.appid
7
+ * 3. 构造函数按 appid 直接命中缓存,不再做深比较
8
+ * 4. 其余逻辑(url 常量 / cache LRU / 所有方法)保持不变
9
+ */
10
+
1
11
  const util = require('util')
2
12
  const axios = require('axios')
3
13
  const crypto = require('crypto')
@@ -105,7 +115,7 @@ const b2bFinishUrl =
105
115
 
106
116
  const fsPromise = require('fs/promises')
107
117
  const { LRUCache } = require('lru-cache')
108
- const request = require('request')
118
+ const FormData = require('form-data')
109
119
  const cache = new LRUCache({
110
120
  max: 100,
111
121
  ttl: 1000 * 60 * 60,
@@ -114,20 +124,11 @@ const uuid = require('node-uuid')
114
124
  const hmacSha256 = (data, secret) => {
115
125
  return crypto.createHmac('sha256', secret).update(data).digest('hex')
116
126
  }
117
- const uploadPromise = (options) => {
118
- return new Promise((resolve, reject) => {
119
- request.post(
120
- {
121
- ...options,
122
- },
123
- (err, response, body) => {
124
- if (err || response.statusCode !== 200) {
125
- reject(err)
126
- }
127
- resolve(JSON.parse(body))
128
- }
129
- )
127
+ const uploadPromise = async (url, formData) => {
128
+ const response = await axios.post(url, formData, {
129
+ headers: formData.getHeaders(),
130
130
  })
131
+ return response.data
131
132
  }
132
133
 
133
134
  const https = require('https')
@@ -145,28 +146,26 @@ const wxHttpsAgent = new https.Agent({
145
146
  axios.defaults.httpsAgent = wxHttpsAgent
146
147
 
147
148
  module.exports = class Singleton {
149
+ static _instanceMap = new Map()
150
+
148
151
  constructor(config) {
152
+ const key = config.appid
153
+ const cached = Singleton._instanceMap.get(key)
154
+ if (cached) {
155
+ return cached
156
+ }
157
+ if (!config.appid || !config.secrect) {
158
+ throw new Error('没有配置appid或secrect')
159
+ }
149
160
  this.config = {
150
161
  ...config,
151
162
  grant_type: 'client_credential',
152
163
  }
153
- /**
154
- * 单例模式
155
- */
156
- if (!Singleton.instance) {
157
- Singleton.instance = this
158
- }
159
- const previous = Singleton.instance.getConfig()
160
- if (!lodash.isEqual(previous, config)) {
161
- Singleton.instance = this
162
- }
163
- return Singleton.instance
164
+ Singleton._instanceMap.set(key, this)
164
165
  }
165
166
 
166
167
  init() {
167
- if (!this.config.appid || !this.config.secrect) {
168
- throw new Error('没有配置appid或secrect')
169
- }
168
+ return this
170
169
  }
171
170
 
172
171
  async getAccessToken() {
@@ -315,24 +314,24 @@ module.exports = class Singleton {
315
314
  const url = util.format(createLiveUrl, access_token)
316
315
 
317
316
  const formatOptions = {
318
- name, // 房间名字
319
- coverImg, // 通过 uploadfile 上传,填写 mediaID
320
- startTime, // 开始时间
321
- endTime, // 结束时间
322
- anchorName, // 主播昵称
323
- anchorWechat, // 主播微信号
324
- subAnchorWechat, // 主播副号微信号
325
- createrWechat, // 创建者微信号
326
- shareImg, // 通过 uploadfile 上传,填写 mediaID
327
- feedsImg, // 通过 uploadfile 上传,填写 mediaID
328
- isFeedsPublic: 0, // 是否开启官方收录,1 开启,0 关闭
329
- type: 0, // 直播类型,1 推流 0 手机直播
330
- closeLike: 0, // 是否关闭点赞 1:关闭
331
- closeGoods: 1, // 是否关闭商品货架,1:关闭
332
- closeComment: 1, // 是否开启评论,1:关闭
333
- closeReplay, // 是否关闭回放 1 关闭
334
- closeShare: 1, // 是否关闭分享 1 关闭
335
- closeKf: 1, // 是否关闭客服,1 关闭
317
+ name,
318
+ coverImg,
319
+ startTime,
320
+ endTime,
321
+ anchorName,
322
+ anchorWechat,
323
+ subAnchorWechat,
324
+ createrWechat,
325
+ shareImg,
326
+ feedsImg,
327
+ isFeedsPublic: 0,
328
+ type: 0,
329
+ closeLike: 0,
330
+ closeGoods: 1,
331
+ closeComment: 1,
332
+ closeReplay,
333
+ closeShare: 1,
334
+ closeKf: 1,
336
335
  }
337
336
  const result = await axios.post(url, formatOptions).then((res) => res.data)
338
337
  if (result.errcode) {
@@ -625,13 +624,11 @@ module.exports = class Singleton {
625
624
 
626
625
  const access_token = await this.getAccessToken()
627
626
  const url = util.format(uploadFile, access_token, 'image')
628
- const formData = {
629
- media: request(imgUrl.startsWith('//') ? `http:${imgUrl}` : imgUrl),
630
- }
631
- const uploadResult = await uploadPromise({
632
- url,
633
- formData,
634
- })
627
+ const fullUrl = imgUrl.startsWith('//') ? `http:${imgUrl}` : imgUrl
628
+ const stream = (await axios.get(fullUrl, { responseType: 'stream' })).data
629
+ const formData = new FormData()
630
+ formData.append('media', stream)
631
+ const uploadResult = await uploadPromise(url, formData)
635
632
  if (uploadResult.errcode) {
636
633
  if (uploadResult.errcode === 40001) {
637
634
  cache.reset()
@@ -1203,21 +1200,6 @@ module.exports = class Singleton {
1203
1200
  return result
1204
1201
  }
1205
1202
 
1206
- // async b2bFinish(payLoad) {
1207
- // const access_token = await this.getAccessToken()
1208
- // const url = util.format(b2bFinishUrl, access_token)
1209
-
1210
- // const result = await axios.post(url, payLoad).then((res) => res.data)
1211
- // if (result.errcode) {
1212
- // if (result.errcode === 40001) {
1213
- // cache.reset()
1214
- // return await this.b2bFinish(payLoad)
1215
- // }
1216
- // throw new Error(`${result.errcode};${result.errmsg}`)
1217
- // }
1218
- // return result
1219
- // }
1220
-
1221
1203
  async b2bFinish({ mchid, app_key, out_trade_no }) {
1222
1204
  const requestUrl = b2bFinishUrl
1223
1205
 
@@ -38,28 +38,24 @@ const cache = new LRUCache({
38
38
  })
39
39
 
40
40
  module.exports = class Singleton {
41
+ static _instanceMap = new Map()
41
42
  constructor(config) {
43
+ const key = config.appid
44
+ if (Singleton._instanceMap.has(key)) {
45
+ return Singleton._instanceMap.get(key)
46
+ }
47
+ if (!config.appid || !config.secrect) {
48
+ throw new Error('没有配置appid或secrect')
49
+ }
42
50
  this.config = {
43
51
  ...config,
44
52
  grant_type: 'client_credential',
45
53
  }
46
- /**
47
- * 单例模式
48
- */
49
- if (!Singleton.instance) {
50
- Singleton.instance = this
51
- }
52
- const previous = Singleton.instance.getConfig()
53
- if (!lodash.isEqual(previous, config)) {
54
- Singleton.instance = this
55
- }
56
- return Singleton.instance
54
+ Singleton._instanceMap.set(key, this)
57
55
  }
58
56
 
59
57
  init() {
60
- if (!this.config.appid || !this.config.secrect) {
61
- throw new Error('没有配置appid或secrect')
62
- }
58
+ return this
63
59
  }
64
60
 
65
61
  async getAccessToken() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.6.4",
3
+ "version": "13.6.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -63,7 +63,7 @@
63
63
  "xlsx": "^0.18.5",
64
64
  "pm2": "^7.0.1",
65
65
  "dayjs": "^1.11.13",
66
- "request": "^2.88.2"
66
+ "form-data": "^4.0.0"
67
67
  },
68
68
  "devDependencies": {
69
69
  "eslint": "^4.19.1",