q-koa 8.5.3 → 8.6.0
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/service.js +19 -17
- package/core/file/services/weixinMP.js +1 -2
- package/core/file/utils/index.js +58 -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
|
}
|
|
@@ -182,23 +182,25 @@ exports.qr_code = async ({
|
|
|
182
182
|
return null
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
185
|
+
|
|
186
|
+
return client
|
|
187
|
+
.put(fileName, buffer)
|
|
188
|
+
|
|
189
|
+
.then((result) => {
|
|
190
|
+
if (result && result.res && result.res.status === 200) {
|
|
191
|
+
if (oss_host && cdn_host) {
|
|
192
|
+
const url = result.url.replace(oss_host, cdn_host)
|
|
193
|
+
return Promise.resolve(url)
|
|
194
|
+
} else {
|
|
195
|
+
return Promise.resolve(result.url)
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
return Promise.reject(new Error('小程序码生成错误,请重试'))
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
.catch((e) => {
|
|
202
|
+
return Promise.reject(new Error(e.message))
|
|
203
|
+
})
|
|
202
204
|
}
|
|
203
205
|
}
|
|
204
206
|
|
|
@@ -404,7 +404,6 @@ module.exports = class Singleton {
|
|
|
404
404
|
.join('&')
|
|
405
405
|
: '',
|
|
406
406
|
}
|
|
407
|
-
const creatuuid = uuid.v1()
|
|
408
407
|
|
|
409
408
|
const bufferResult = await axios
|
|
410
409
|
.post(url, payLoad, {
|
|
@@ -419,7 +418,7 @@ module.exports = class Singleton {
|
|
|
419
418
|
if (bufferResult.errcode) {
|
|
420
419
|
if (bufferResult.errcode === 40001) {
|
|
421
420
|
cache.reset()
|
|
422
|
-
return await this.
|
|
421
|
+
return await this.createQRBuffer(options)
|
|
423
422
|
}
|
|
424
423
|
throw new Error(bufferResult.errmsg)
|
|
425
424
|
}
|
package/core/file/utils/index.js
CHANGED
|
@@ -17,7 +17,7 @@ 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)
|
|
@@ -68,3 +68,60 @@ exports.safeSplit = (mainStr, str) => {
|
|
|
68
68
|
}
|
|
69
69
|
return ['', '']
|
|
70
70
|
}
|
|
71
|
+
|
|
72
|
+
exports.getImageType = (fileBuffer) => {
|
|
73
|
+
// 将上文提到的 文件标识头 按 字节 整理到数组中
|
|
74
|
+
const imageBufferHeaders = [
|
|
75
|
+
{ bufBegin: [0xff, 0xd8], bufEnd: [0xff, 0xd9], suffix: '.jpg' },
|
|
76
|
+
{ bufBegin: [0x00, 0x00, 0x02, 0x00, 0x00], suffix: '.tga' },
|
|
77
|
+
{ bufBegin: [0x00, 0x00, 0x10, 0x00, 0x00], suffix: '.rle' },
|
|
78
|
+
{
|
|
79
|
+
bufBegin: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
|
|
80
|
+
suffix: '.png',
|
|
81
|
+
},
|
|
82
|
+
{ bufBegin: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], suffix: '.gif' },
|
|
83
|
+
{ bufBegin: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], suffix: '.gif' },
|
|
84
|
+
{ bufBegin: [0x42, 0x4d], suffix: '.bmp' },
|
|
85
|
+
{ bufBegin: [0x0a], suffix: '.pcx' },
|
|
86
|
+
{ bufBegin: [0x49, 0x49], suffix: '.tif' },
|
|
87
|
+
{ bufBegin: [0x4d, 0x4d], suffix: '.tif' },
|
|
88
|
+
{
|
|
89
|
+
bufBegin: [0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x20],
|
|
90
|
+
suffix: '.ico',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
bufBegin: [0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x20, 0x20],
|
|
94
|
+
suffix: '.cur',
|
|
95
|
+
},
|
|
96
|
+
{ bufBegin: [0x46, 0x4f, 0x52, 0x4d], suffix: '.iff' },
|
|
97
|
+
{ bufBegin: [0x52, 0x49, 0x46, 0x46], suffix: '.ani' },
|
|
98
|
+
]
|
|
99
|
+
for (const imageBufferHeader of imageBufferHeaders) {
|
|
100
|
+
let isEqual
|
|
101
|
+
// 判断标识头前缀
|
|
102
|
+
if (imageBufferHeader.bufBegin) {
|
|
103
|
+
const buf = Buffer.from(imageBufferHeader.bufBegin)
|
|
104
|
+
isEqual = buf.equals(
|
|
105
|
+
//使用 buffer.slice 方法 对 buffer 以字节为单位切割
|
|
106
|
+
fileBuffer.slice(0, imageBufferHeader.bufBegin.length)
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
// 判断标识头后缀
|
|
110
|
+
if (isEqual && imageBufferHeader.bufEnd) {
|
|
111
|
+
const buf = Buffer.from(imageBufferHeader.bufEnd)
|
|
112
|
+
isEqual = buf.equals(fileBuffer.slice(-imageBufferHeader.bufEnd.length))
|
|
113
|
+
}
|
|
114
|
+
if (isEqual) {
|
|
115
|
+
if (
|
|
116
|
+
['.ani', '.iff', '.cur', '.tif', '.pcx', '.rle', '.tga'].includes(
|
|
117
|
+
imageBufferHeader.suffix
|
|
118
|
+
)
|
|
119
|
+
) {
|
|
120
|
+
return '.jpg'
|
|
121
|
+
}
|
|
122
|
+
return imageBufferHeader.suffix
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// 未能识别到该文件类型
|
|
126
|
+
return ''
|
|
127
|
+
}
|
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,
|