q-koa 8.5.5 → 8.6.2
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 +1 -1
- package/core/file/plugins/common/controller.js +55 -0
- package/core/file/plugins/system/service.js +20 -0
- package/core/file/plugins/weixin/controller.js +8 -6
- package/core/file/plugins/weixin/service.js +8 -4
- package/core/file/utils/index.js +73 -1
- package/core/middlewares.js +2 -6
- package/package.json +1 -1
package/core/app.js
CHANGED
|
@@ -1835,7 +1835,7 @@ APP.getClientTypeByCtx = (ctx) => {
|
|
|
1835
1835
|
|
|
1836
1836
|
APP.getConfig = (app) => ({
|
|
1837
1837
|
async getObject(type) {
|
|
1838
|
-
let configList =
|
|
1838
|
+
let configList = app.cache.get('configList')
|
|
1839
1839
|
if (!configList) {
|
|
1840
1840
|
configList = await app.model.setting.findAll({
|
|
1841
1841
|
raw: true,
|
|
@@ -10,6 +10,7 @@ const axios = require('axios')
|
|
|
10
10
|
const util = require('util')
|
|
11
11
|
const jwt = require('jsonwebtoken')
|
|
12
12
|
|
|
13
|
+
const { getImageType } = require('../../utils')
|
|
13
14
|
const Email = require('../../services/email')
|
|
14
15
|
const Geo = require('../../services/geo')
|
|
15
16
|
const Express = require('../../services/express')
|
|
@@ -419,3 +420,57 @@ exports.express = async (ctx) => {
|
|
|
419
420
|
|
|
420
421
|
ctx.SUCCESS(result)
|
|
421
422
|
}
|
|
423
|
+
|
|
424
|
+
exports.urlToOss = async (ctx) => {
|
|
425
|
+
const { app, appName } = getAppByCtx(ctx)
|
|
426
|
+
const appConfig = getConfig(app)
|
|
427
|
+
const { is_dev } = await appConfig.getObject('base')
|
|
428
|
+
const { url } = ctx.request.body
|
|
429
|
+
const {
|
|
430
|
+
accessKeyId,
|
|
431
|
+
accessKeySecret,
|
|
432
|
+
bucket,
|
|
433
|
+
region,
|
|
434
|
+
cdn_host,
|
|
435
|
+
oss_host,
|
|
436
|
+
} = await appConfig.getObject('oss')
|
|
437
|
+
|
|
438
|
+
if (!url || ['aliyun', cdn_host, oss_host].some((i) => url.includes(i)))
|
|
439
|
+
return ctx.SUCCESS(url)
|
|
440
|
+
|
|
441
|
+
if (!(accessKeyId && accessKeySecret && bucket && region)) {
|
|
442
|
+
throw new Error('没有配置oss设置')
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const client = new OSS({
|
|
446
|
+
accessKeyId,
|
|
447
|
+
accessKeySecret,
|
|
448
|
+
bucket,
|
|
449
|
+
region,
|
|
450
|
+
secure: true,
|
|
451
|
+
})
|
|
452
|
+
const buffer = await axios
|
|
453
|
+
.get(url, {
|
|
454
|
+
responseType: 'arraybuffer',
|
|
455
|
+
})
|
|
456
|
+
.then((res) => res.data)
|
|
457
|
+
|
|
458
|
+
const type = getImageType(buffer)
|
|
459
|
+
console.log('type', type)
|
|
460
|
+
const fileName = `${
|
|
461
|
+
String(new Date().getTime()).split('').reverse().join('') + Math.random()
|
|
462
|
+
}${type}`
|
|
463
|
+
|
|
464
|
+
const result = await client.put(fileName, buffer)
|
|
465
|
+
|
|
466
|
+
if (result && result.res && result.res.status === 200) {
|
|
467
|
+
if (oss_host && cdn_host) {
|
|
468
|
+
const url = result.url.replace(oss_host, cdn_host)
|
|
469
|
+
return ctx.SUCCESS(url)
|
|
470
|
+
} else {
|
|
471
|
+
return ctx.SUCCESS(result.url)
|
|
472
|
+
}
|
|
473
|
+
} else {
|
|
474
|
+
throw new Error('上传失败')
|
|
475
|
+
}
|
|
476
|
+
}
|
|
@@ -7,6 +7,7 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
|
|
|
7
7
|
const appConfig = getConfig(app)
|
|
8
8
|
const { version: cacheVersion } = await appConfig.getObject('base')
|
|
9
9
|
const apiVersion = ctx.request.header.version
|
|
10
|
+
const config = ctx.request.header.config
|
|
10
11
|
|
|
11
12
|
if (!(excludes instanceof Array)) {
|
|
12
13
|
excludes = [excludes]
|
|
@@ -218,6 +219,14 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
|
|
|
218
219
|
{}
|
|
219
220
|
)
|
|
220
221
|
if (obj.setting && apiVersion && cacheVersion) {
|
|
222
|
+
let target = null
|
|
223
|
+
if (config) {
|
|
224
|
+
const application = app.cache.get('application')
|
|
225
|
+
if (application) {
|
|
226
|
+
target = application.find((a) => a.config === config)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
221
230
|
return {
|
|
222
231
|
...obj,
|
|
223
232
|
setting: obj.setting.map((i) => {
|
|
@@ -227,6 +236,17 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
|
|
|
227
236
|
value: apiVersion === cacheVersion,
|
|
228
237
|
}
|
|
229
238
|
}
|
|
239
|
+
|
|
240
|
+
if (
|
|
241
|
+
target &&
|
|
242
|
+
target.hasOwnProperty(i.code) &&
|
|
243
|
+
i.hasOwnProperty('value')
|
|
244
|
+
) {
|
|
245
|
+
return {
|
|
246
|
+
...(i.toJSON ? i.toJSON() : i),
|
|
247
|
+
value: target[i.code],
|
|
248
|
+
}
|
|
249
|
+
}
|
|
230
250
|
return i
|
|
231
251
|
}),
|
|
232
252
|
}
|
|
@@ -743,12 +743,14 @@ exports.h5_pay = async (ctx) => {
|
|
|
743
743
|
} = ctx.request.body
|
|
744
744
|
|
|
745
745
|
const appConfig = getConfig(app)
|
|
746
|
-
const { appId, key, mchId } = await appConfig.getObject(
|
|
746
|
+
const { appId, key, mchId, partner_key } = await appConfig.getObject(
|
|
747
|
+
pay_config
|
|
748
|
+
)
|
|
747
749
|
const { is_dev, site_host } = await appConfig.getObject('base')
|
|
748
750
|
|
|
749
751
|
const payObj = new WeixinPay({
|
|
750
752
|
appId,
|
|
751
|
-
key,
|
|
753
|
+
key: key.length > 10 ? key : partner_key,
|
|
752
754
|
mchId,
|
|
753
755
|
})
|
|
754
756
|
const result = await payObj.run({
|
|
@@ -803,7 +805,7 @@ exports.mp_pay = async (ctx) => {
|
|
|
803
805
|
}
|
|
804
806
|
const appConfig = getConfig(app)
|
|
805
807
|
|
|
806
|
-
const { mchId, key } = await appConfig.getObject(pay_config)
|
|
808
|
+
const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
|
|
807
809
|
const { is_dev, site_host } = await appConfig.getObject('base')
|
|
808
810
|
// 公众号支付用 服务号 appid,其他用小程序 appid
|
|
809
811
|
const app_id =
|
|
@@ -814,7 +816,7 @@ exports.mp_pay = async (ctx) => {
|
|
|
814
816
|
const wxpay = WXPay({
|
|
815
817
|
appid: app_id,
|
|
816
818
|
mch_id: mchId,
|
|
817
|
-
partner_key: key, // 微信商户平台API密钥
|
|
819
|
+
partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
|
|
818
820
|
})
|
|
819
821
|
|
|
820
822
|
const pay = (options) =>
|
|
@@ -1152,12 +1154,12 @@ exports.checkPay = async (ctx) => {
|
|
|
1152
1154
|
|
|
1153
1155
|
const appConfig = getConfig(app)
|
|
1154
1156
|
const app_id = (await appConfig.getObject(config)).app_id
|
|
1155
|
-
const { mchId, key } = await appConfig.getObject(pay_config)
|
|
1157
|
+
const { mchId, key, partner_key } = await appConfig.getObject(pay_config)
|
|
1156
1158
|
|
|
1157
1159
|
const wxpay = WXPay({
|
|
1158
1160
|
appid: app_id,
|
|
1159
1161
|
mch_id: mchId,
|
|
1160
|
-
partner_key: key, // 微信商户平台API密钥
|
|
1162
|
+
partner_key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥
|
|
1161
1163
|
})
|
|
1162
1164
|
|
|
1163
1165
|
const checkOrder = (obj) => {
|
|
@@ -18,13 +18,15 @@ exports.refund = async ({
|
|
|
18
18
|
}) => {
|
|
19
19
|
const { app, appName } = getAppByCtx(ctx)
|
|
20
20
|
const appConfig = getConfig(app)
|
|
21
|
-
const { mchId, key, appId } = await appConfig.getObject(
|
|
21
|
+
const { mchId, key, appId, partner_key } = await appConfig.getObject(
|
|
22
|
+
pay_config
|
|
23
|
+
)
|
|
22
24
|
const { site_host } = await appConfig.getObject('base')
|
|
23
25
|
|
|
24
26
|
const payObj = new Pay({
|
|
25
27
|
appId: appId,
|
|
26
28
|
mchId: mchId,
|
|
27
|
-
key: key, // 微信商户平台API密钥,
|
|
29
|
+
key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥,
|
|
28
30
|
pfx: await fsPromise.readFile(
|
|
29
31
|
path.resolve(
|
|
30
32
|
__dirname,
|
|
@@ -72,14 +74,16 @@ exports.cash = async ({
|
|
|
72
74
|
}) => {
|
|
73
75
|
const { app, appName } = getAppByCtx(ctx)
|
|
74
76
|
const appConfig = getConfig(app)
|
|
75
|
-
const { mchId, key, appId } = await appConfig.getObject(
|
|
77
|
+
const { mchId, key, appId, partner_key } = await appConfig.getObject(
|
|
78
|
+
pay_config
|
|
79
|
+
)
|
|
76
80
|
|
|
77
81
|
const { is_dev } = await appConfig.getObject('base')
|
|
78
82
|
|
|
79
83
|
const payObj = new Pay({
|
|
80
84
|
appId: appId,
|
|
81
85
|
mchId: mchId,
|
|
82
|
-
key: key, // 微信商户平台API密钥,
|
|
86
|
+
key: key.length > 10 ? key : partner_key, // 微信商户平台API密钥,
|
|
83
87
|
pfx: await fsPromise.readFile(
|
|
84
88
|
path.resolve(
|
|
85
89
|
__dirname,
|
package/core/file/utils/index.js
CHANGED
|
@@ -17,13 +17,28 @@ exports.getList = (config) => (type) => {
|
|
|
17
17
|
|
|
18
18
|
exports.getObject = (config, app) => async (type) => {
|
|
19
19
|
if (app) {
|
|
20
|
-
const applicationList =
|
|
20
|
+
const applicationList = app.cache.get('application')
|
|
21
21
|
if (
|
|
22
22
|
applicationList &&
|
|
23
23
|
applicationList.find((item) => item.config === type)
|
|
24
24
|
) {
|
|
25
25
|
return applicationList.find((item) => item.config === type)
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
if (!applicationList) {
|
|
29
|
+
if (app.model.application) {
|
|
30
|
+
const result = await app.model.application.findAll()
|
|
31
|
+
|
|
32
|
+
if (result.length > 0) {
|
|
33
|
+
app.cache.set('application', JSON.parse(JSON.stringify(result)))
|
|
34
|
+
|
|
35
|
+
const target = result.find((item) => item.config === type)
|
|
36
|
+
if (target) {
|
|
37
|
+
return target
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
27
42
|
}
|
|
28
43
|
const obj = config.find((i) => i.code === type)
|
|
29
44
|
if (!obj) throw new Error(`找不到${type}相关设置`)
|
|
@@ -68,3 +83,60 @@ exports.safeSplit = (mainStr, str) => {
|
|
|
68
83
|
}
|
|
69
84
|
return ['', '']
|
|
70
85
|
}
|
|
86
|
+
|
|
87
|
+
exports.getImageType = (fileBuffer) => {
|
|
88
|
+
// 将上文提到的 文件标识头 按 字节 整理到数组中
|
|
89
|
+
const imageBufferHeaders = [
|
|
90
|
+
{ bufBegin: [0xff, 0xd8], bufEnd: [0xff, 0xd9], suffix: '.jpg' },
|
|
91
|
+
{ bufBegin: [0x00, 0x00, 0x02, 0x00, 0x00], suffix: '.tga' },
|
|
92
|
+
{ bufBegin: [0x00, 0x00, 0x10, 0x00, 0x00], suffix: '.rle' },
|
|
93
|
+
{
|
|
94
|
+
bufBegin: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
|
|
95
|
+
suffix: '.png',
|
|
96
|
+
},
|
|
97
|
+
{ bufBegin: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], suffix: '.gif' },
|
|
98
|
+
{ bufBegin: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], suffix: '.gif' },
|
|
99
|
+
{ bufBegin: [0x42, 0x4d], suffix: '.bmp' },
|
|
100
|
+
{ bufBegin: [0x0a], suffix: '.pcx' },
|
|
101
|
+
{ bufBegin: [0x49, 0x49], suffix: '.tif' },
|
|
102
|
+
{ bufBegin: [0x4d, 0x4d], suffix: '.tif' },
|
|
103
|
+
{
|
|
104
|
+
bufBegin: [0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x20],
|
|
105
|
+
suffix: '.ico',
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
bufBegin: [0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x20, 0x20],
|
|
109
|
+
suffix: '.cur',
|
|
110
|
+
},
|
|
111
|
+
{ bufBegin: [0x46, 0x4f, 0x52, 0x4d], suffix: '.iff' },
|
|
112
|
+
{ bufBegin: [0x52, 0x49, 0x46, 0x46], suffix: '.ani' },
|
|
113
|
+
]
|
|
114
|
+
for (const imageBufferHeader of imageBufferHeaders) {
|
|
115
|
+
let isEqual
|
|
116
|
+
// 判断标识头前缀
|
|
117
|
+
if (imageBufferHeader.bufBegin) {
|
|
118
|
+
const buf = Buffer.from(imageBufferHeader.bufBegin)
|
|
119
|
+
isEqual = buf.equals(
|
|
120
|
+
//使用 buffer.slice 方法 对 buffer 以字节为单位切割
|
|
121
|
+
fileBuffer.slice(0, imageBufferHeader.bufBegin.length)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
// 判断标识头后缀
|
|
125
|
+
if (isEqual && imageBufferHeader.bufEnd) {
|
|
126
|
+
const buf = Buffer.from(imageBufferHeader.bufEnd)
|
|
127
|
+
isEqual = buf.equals(fileBuffer.slice(-imageBufferHeader.bufEnd.length))
|
|
128
|
+
}
|
|
129
|
+
if (isEqual) {
|
|
130
|
+
if (
|
|
131
|
+
['.ani', '.iff', '.cur', '.tif', '.pcx', '.rle', '.tga'].includes(
|
|
132
|
+
imageBufferHeader.suffix
|
|
133
|
+
)
|
|
134
|
+
) {
|
|
135
|
+
return '.jpg'
|
|
136
|
+
}
|
|
137
|
+
return imageBufferHeader.suffix
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// 未能识别到该文件类型
|
|
141
|
+
return ''
|
|
142
|
+
}
|
package/core/middlewares.js
CHANGED
|
@@ -21,12 +21,8 @@ exports.miErrors = () => async (ctx, next) => {
|
|
|
21
21
|
try {
|
|
22
22
|
return await next()
|
|
23
23
|
} catch (e) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// payload: {
|
|
27
|
-
// message: e.message,
|
|
28
|
-
// },
|
|
29
|
-
// });
|
|
24
|
+
console.log(e.message)
|
|
25
|
+
console.log(e.stack)
|
|
30
26
|
const errorData = {
|
|
31
27
|
url: ctx.request.href,
|
|
32
28
|
body: ctx.request.body,
|