@yoooloo42/beat 1.0.26 → 1.0.27

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yoooloo42/beat",
3
- "version": "1.0.26",
3
+ "version": "1.0.27",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -23,6 +23,7 @@
23
23
  "@yoooloo42/bean": "^1.0.14",
24
24
  "aliyun-api-gateway": "^1.1.6",
25
25
  "axios": "^1.13.2",
26
+ "cheerio": "^1.1.2",
26
27
  "multer": "^2.0.2",
27
28
  "nodemailer": "^7.0.10",
28
29
  "xml2js": "^0.6.2"
@@ -31,8 +32,7 @@
31
32
  "./libs/*": "./src/libs/*.js",
32
33
  "./libs/Ali/*": "./src/libs/Ali/*.js",
33
34
  "./libs/crypto/*": "./src/libs/crypto/*.js",
34
- "./libs/multer/*": "./src/libs/multer/*.js",
35
- "./libs/readfile/*": "./src/libs/readfile/*.js",
35
+ "./libs/FileDB/*": "./src/libs/FileDB/*.js",
36
36
  "./libs/WeChat/*": "./src/libs/WeChat/*.js",
37
37
  "./libs/WeChat-Pay/*": "./src/libs/WeChat-Pay/*.js"
38
38
  },
@@ -1,5 +1,6 @@
1
1
  import fs from 'fs'
2
2
  import path from 'path'
3
+ import Richtext from './richtext.js'
3
4
  const thisTime = new Date()
4
5
 
5
6
  // 图片新增
@@ -14,7 +15,7 @@ function imageAppend (para) {
14
15
  // para.dataunitId 数据单元ID
15
16
  // para.tblName 表名
16
17
  // para.fieldName 字段名
17
- // para.fieldIndex 数组类型字段的索引
18
+ // para.fieldIndex 多文件索引
18
19
  // para.dataId 数据ID
19
20
 
20
21
  return new Promise(function (resolve, reject) {
@@ -22,7 +23,7 @@ function imageAppend (para) {
22
23
  return resolve('')
23
24
  }
24
25
 
25
- // 数据库文件夹:数据单元ID + 表名 + 字段名 + 数组类型字段的索引 + 当年 + 当月
26
+ // 数据库文件夹:数据单元ID + 表名 + 字段名 + 多文件索引 + 当年 + 当月
26
27
  const dbFolder = para.pathHead.dbFolder +
27
28
  (para.dataunitId ? '/' + para.dataunitId : '') +
28
29
  '/' + para.tblName +
@@ -30,7 +31,7 @@ function imageAppend (para) {
30
31
  '[' + ('fieldIndex' in para ? para.fieldIndex : 0) + "]" +
31
32
  '/' + thisTime.getFullYear() +
32
33
  '/' + (thisTime.getMonth() + 1)
33
- // 数据库文件名:数据单元ID + 表名 + 字段名 + 数组类型字段的索引 + 数据ID + 随机数 + 扩展名
34
+ // 数据库文件名:数据单元ID + 表名 + 字段名 + 多文件索引 + 数据ID + 随机数 + 扩展名
34
35
  const dbFileName = (para.dataunitId ? para.dataunitId + '.' : '') +
35
36
  para.tblName + '.' +
36
37
  para.fieldName + '.' +
@@ -39,22 +40,22 @@ function imageAppend (para) {
39
40
  Math.floor((999999 - 0) * Math.random() + 0) +
40
41
  path.parse(para.uploaded).ext
41
42
 
42
- // 上传文件路径:来自于 已上传文件的URL 头部置换
43
+ // 上传文件路径
43
44
  const uploadFilePath = para.uploaded.replace(para.pathHead.uploadUrl, para.pathHead.uploadFolder)
44
45
  // 数据库文件路径
45
46
  const dbFilePath = dbFolder + '/' + dbFileName
46
- // 数据库URL:来自于 数据库文件夹中的文件路径 头部置换
47
+ // 数据库URL
47
48
  const dbUrl = dbFilePath.replace(para.pathHead.dbFolder, para.pathHead.dbUrl)
48
49
  new Promise(function (resolve, reject) {
49
50
  // 创建数据库文件夹
50
- fs.mkdir(dbFolder, {recursive: true}, (err) => {
51
+ fs.mkdir(dbFolder, {recursive: true}, err => {
51
52
  if (err) throw err
52
53
  resolve()
53
54
  })
54
- }).then(function () { //
55
+ }).then(function () {
55
56
  new Promise(function (resolve, reject) {
56
57
  // 已上传文件转存至数据库文件夹
57
- fs.rename(uploadFilePath, dbFilePath, (err) => {
58
+ fs.rename(uploadFilePath, dbFilePath, err => {
58
59
  if (err) throw err
59
60
  resolve(dbUrl) // 返回数据库URL
60
61
  })
@@ -76,8 +77,7 @@ function imageDelete (para) {
76
77
  if (!para.url) {
77
78
  return resolve()
78
79
  }
79
-
80
- fs.unlink(para.url.replace(para.pathHead.dbUrl, para.pathHead.dbFolder), (err) => {
80
+ fs.unlink(para.url.replace(para.pathHead.dbUrl, para.pathHead.dbFolder), err => {
81
81
  if (err) throw err
82
82
  resolve()
83
83
  })
@@ -93,21 +93,28 @@ function imageUpdate (para) {
93
93
  // para.pathHead.uploadUrl 上传URL
94
94
  // para.uploaded 已上传文件的URL
95
95
  // para.old 原文件的URL
96
- // para.delete 是否删除原文件
96
+ // para.delete 如果没有上传新文件,是否删除原文件
97
97
 
98
98
  // para.dataunitId 数据单元ID
99
99
  // para.tblName 表名
100
100
  // para.fieldName 字段名
101
- // para.fieldIndex 数组类型字段的索引
101
+ // para.fieldIndex 多文件索引
102
102
  // para.dataId 数据ID
103
103
 
104
104
  return new Promise(function (resolve, reject) {
105
105
  if (!!para.uploaded || para.delete === true || para.delete === 'true') {
106
- imageDelete(para.old)
106
+ imageDelete({
107
+ pathHead: {
108
+ dbFolder: para.pathHead.dbFolder,
109
+ dbUrl: para.pathHead.dbUrl
110
+ },
111
+ url: para.old
112
+ })
107
113
  }
108
114
  if (!para.uploaded) {
109
115
  return resolve(para.old)
110
116
  }
117
+
111
118
  imageAppend({
112
119
  pathHead: {
113
120
  dbFolder: para.pathHead.dbFolder,
@@ -143,18 +150,13 @@ function imagesAppend (para) {
143
150
  // para.dataId 数据ID
144
151
 
145
152
  return new Promise(function (resolve, reject) {
146
- if(!para.uploaded || para.uploaded.length === 0){
153
+ if(!para.arrUploaded || para.arrUploaded.length === 0){
147
154
  return resolve([])
148
155
  }
149
- let arrPromise = []
156
+ let arrPrm = []
150
157
  para.arrUploaded.forEach((item, index)=>{
151
- arrPromise.push(imageAppend ({
152
- pathHead: {
153
- dbFolder: para.pathHead.dbFolder,
154
- dbUrl: para.pathHead.dbUrl,
155
- uploadFolder: para.pathHead.uploadFolder,
156
- uploadUrl: para.pathHead.uploadUrl
157
- },
158
+ arrPrm.push(imageAppend ({
159
+ pathHead: para.pathHead,
158
160
  uploaded: item,
159
161
 
160
162
  dataunitId: para.dataunitId,
@@ -164,7 +166,7 @@ function imagesAppend (para) {
164
166
  dataId: para.dataId
165
167
  }))
166
168
  })
167
- Promise.all(arrPromise).then(result=>{
169
+ Promise.all(arrPrm).then(result=>{
168
170
  resolve(result)
169
171
  })
170
172
  })
@@ -182,17 +184,14 @@ function imagesDelete(para){
182
184
  return resolve()
183
185
  }
184
186
 
185
- let arrPromise = []
187
+ let arrPrm = []
186
188
  para.arrUrl.forEach(i=>{
187
- arrPromise.push(imageDelete({
188
- pathHead: {
189
- dbFolder: para.pathHead.dbFolder,
190
- dbUrl: para.pathHead.dbUrl
191
- },
189
+ arrPrm.push(imageDelete({
190
+ pathHead: para.pathHead,
192
191
  url: i
193
192
  }))
194
193
  })
195
- Promise.all((arrPromise)).then(()=>{
194
+ Promise.all((arrPrm)).then(()=>{
196
195
  resolve()
197
196
  })
198
197
  })
@@ -223,12 +222,7 @@ function imagesUpdate (para) {
223
222
  arrUrl: para.arrDelete
224
223
  }).then(()=>{
225
224
  imagesAppend ({
226
- pathHead: {
227
- dbFolder: para.pathHead.dbFolder,
228
- dbUrl: para.pathHead.dbUrl,
229
- uploadFolder: para.pathHead.uploadFolder,
230
- uploadUrl: para.pathHead.uploadUrl
231
- },
225
+ pathHead: para.pathHead,
232
226
  arrUploaded: para.arrUploaded,
233
227
 
234
228
  dataunitId: para.dataunitId,
@@ -236,6 +230,7 @@ function imagesUpdate (para) {
236
230
  fieldName: para.fieldName,
237
231
  dataId: para.dataId
238
232
  }).then(result=>{
233
+ // 原文件中未删除的并入返回结果
239
234
  let arrHoldon = []
240
235
  para.arrOld.forEach(i=>{
241
236
  let holdon = true
@@ -244,7 +239,7 @@ function imagesUpdate (para) {
244
239
  holdon = false
245
240
  }
246
241
  })
247
- if(!!holdon){
242
+ if(holdon){
248
243
  arrHoldon.push(i)
249
244
  }
250
245
  })
@@ -254,19 +249,6 @@ function imagesUpdate (para) {
254
249
  })
255
250
  }
256
251
 
257
- // 内部模块:获取富文本中资源文件(图片等)的src
258
- function richtextGetSrc (richtext) {
259
- let arrSrc = []
260
- if (richtext) {
261
- arrSrc = richtext.match(/src=[\"\'][^\"\']{0,}[\"\']/g)
262
- for (let i in arrSrc) {
263
- let a = arrSrc [i]
264
- arrSrc [i] = a.slice(5, a.length - 1)
265
- }
266
- }
267
- return arrSrc
268
- }
269
-
270
252
  // 富文本新增
271
253
  function richtextAppend (para) {
272
254
  // para.pathHead 路径头部
@@ -283,7 +265,7 @@ function richtextAppend (para) {
283
265
 
284
266
  return new Promise(function (resolve, reject) {
285
267
  let richtextReturn = para.richtext,
286
- arrSrc = richtextGetSrc(para.richtext);
268
+ arrSrc = Richtext.extractAllSrc(para.richtext);
287
269
 
288
270
  let dbFolder = para.pathHead.dbFolder +
289
271
  (para.dataunitId ? '/' + para.dataunitId : '') +
@@ -294,13 +276,12 @@ function richtextAppend (para) {
294
276
 
295
277
  new Promise(function (resolve, reject) {
296
278
  // 创建数据库文件夹
297
- fs.mkdir(dbFolder, {recursive: true}, (err) => {
279
+ fs.mkdir(dbFolder, {recursive: true}, err => {
298
280
  if (err) throw err
299
281
  resolve()
300
282
  })
301
- }).then(function () { //上传文件转存,富文本处理
302
- let arrPromise = []
303
-
283
+ }).then(function () {
284
+ let arrPrm = []
304
285
  for (let i in arrSrc) {
305
286
  let uploadFilePath = arrSrc[i].replace(para.pathHead.uploadUrl, para.pathHead.uploadFolder),
306
287
  dbFilePath = dbFolder + '/' +
@@ -312,20 +293,19 @@ function richtextAppend (para) {
312
293
  path.parse(arrSrc [i]).ext,
313
294
  dbUrl = dbFilePath.replace(para.pathHead.dbFolder, para.pathHead.dbUrl)
314
295
 
315
- arrPromise[i] = new Promise(function (resolve, reject) {
296
+ arrPrm.push(new Promise(function (resolve, reject) {
316
297
  // 已上传文件转存至数据库文件夹
317
- fs.rename(uploadFilePath, dbFilePath, (err) => {
298
+ fs.rename(uploadFilePath, dbFilePath, err => {
318
299
  if (err) throw err
319
300
 
320
301
  // 重置富文本内的src
321
302
  richtextReturn = richtextReturn.replace(arrSrc[i], dbUrl)
322
-
323
303
  resolve()
324
304
  })
325
- })
305
+ }))
326
306
  }
327
307
 
328
- Promise.all(arrPromise).then(function () {
308
+ Promise.all(arrPrm).then(function () {
329
309
  resolve(richtextReturn)
330
310
  })
331
311
  })
@@ -340,26 +320,25 @@ function richtextDelete (para) {
340
320
  // para.richtext 富文本
341
321
 
342
322
  return new Promise(function (resolve, reject) {
343
- let arrSrc = richtextGetSrc(para.richtext),
344
- arrPromise = []
345
-
323
+ let arrSrc = Richtext.extractAllSrc(para.richtext),
324
+ arrPrm = []
346
325
  for (let i in arrSrc) {
347
- arrPromise[i] = new Promise(function (resolve, reject) {
348
- fs.unlink(arrSrc [i].replace(para.pathHead.dbUrl, para.pathHead.dbFolder), err => {
326
+ arrPrm[i] = new Promise(function (resolve, reject) {
327
+ fs.unlink(arrSrc[i].replace(para.pathHead.dbUrl, para.pathHead.dbFolder), err => {
349
328
  if (err) throw err
350
329
  resolve()
351
330
  })
352
331
  })
353
332
  }
354
333
 
355
- Promise.all(arrPromise).then(function () {
334
+ Promise.all(arrPrm).then(function () {
356
335
  resolve({code: 0, message: '删除成功'})
357
336
  })
358
337
  })
359
338
  }
360
339
 
361
340
  // 富文本更新
362
- function richtextReturn (para) {
341
+ function richtextUpdate (para) {
363
342
  // para.pathHead 路径头部
364
343
  // para.pathHead.dbFolder 数据库文件夹
365
344
  // para.pathHead.dbUrl 数据库URL
@@ -375,8 +354,8 @@ function richtextReturn (para) {
375
354
 
376
355
  return new Promise(function (resolve, reject) {
377
356
  let richtextReturn = para.richtextNew,
378
- arrSrcNew = richtextGetSrc(para.richtextNew),
379
- arrSrcOld = richtextGetSrc(para.richtextOld)
357
+ arrSrcNew = Richtext.extractAllSrc(para.richtextNew),
358
+ arrSrcOld = Richtext.extractAllSrc(para.richtextOld)
380
359
 
381
360
  let dbFolder = para.pathHead.dbFolder +
382
361
  (para.dataunitId ? '/' + para.dataunitId : '') +
@@ -387,17 +366,17 @@ function richtextReturn (para) {
387
366
 
388
367
  new Promise(function (resolve, reject) {
389
368
  // 创建数据库文件夹
390
- fs.mkdir(dbFolder, {recursive: true}, (err) => {
369
+ fs.mkdir(dbFolder, {recursive: true}, err => {
391
370
  if (err) console.log(err)
392
371
  resolve()
393
372
  })
394
373
  }).then(function () {
395
374
  new Promise(function (resolve, reject) {
396
- let arrPromise = []
375
+ let arrPrm = []
397
376
  for (let i in arrSrcNew) {
398
377
  // 处理富文本 richtextNew 内的新增src
399
378
  if (arrSrcNew [i].startsWith(para.pathHead.uploadUrl)) {
400
- let uploadFilePath = arrSrcNew [i].replace(para.pathHead.uploadUrl, para.pathHead.uploadFolder),
379
+ let uploadFilePath = arrSrcNew[i].replace(para.pathHead.uploadUrl, para.pathHead.uploadFolder),
401
380
  dbFilePath = dbFolder + '/' +
402
381
  (para.dataunitId ? para.dataunitId + '.' : '') +
403
382
  para.tblName + '.' +
@@ -407,19 +386,18 @@ function richtextReturn (para) {
407
386
  path.parse(arrSrcNew [i]).ext,
408
387
  dbUrl = dbFilePath.replace(para.pathHead.dbFolder, para.pathHead.dbUrl)
409
388
 
410
- arrPromise.push(new Promise(function (resolve, reject) {
389
+ arrPrm.push(new Promise(function (resolve, reject) {
411
390
  // 已上传文件转存至数据库文件夹
412
- fs.rename(uploadFilePath, dbFilePath, (err) => {
391
+ fs.rename(uploadFilePath, dbFilePath, err => {
413
392
  if (err) throw err
414
393
 
415
394
  // 重置富文本内新增的src
416
395
  richtextReturn = richtextReturn.replace(arrSrcNew [i], dbUrl)
417
-
418
396
  resolve()
419
397
  })
420
398
  }))
421
399
  } else {
422
- // 处理富文本 richtextNew 内的原src,原文件保留
400
+ // src与原src重复处理:不能删除
423
401
  for (let j in arrSrcOld) {
424
402
  if (arrSrcOld [j] === arrSrcNew [i]) {
425
403
  arrSrcOld [j] = ''
@@ -428,14 +406,14 @@ function richtextReturn (para) {
428
406
  }
429
407
  }
430
408
 
431
- Promise.all(arrPromise).then(function () {
409
+ Promise.all(arrPrm).then(function () {
432
410
  resolve()
433
411
  })
434
412
  }).then(function () {
435
- let arrPromise = []
413
+ let arrPrm = []
436
414
  for (let i in arrSrcOld) {
437
415
  if (arrSrcOld [i]) {
438
- arrPromise.push(new Promise(function (resolve, reject) {
416
+ arrPrm.push(new Promise(function (resolve, reject) {
439
417
  // 删除垃圾文件
440
418
  fs.unlink(arrSrcOld [i].replace(para.pathHead.dbUrl, para.pathHead.dbFolder), err => {
441
419
  if (err) throw err
@@ -445,7 +423,7 @@ function richtextReturn (para) {
445
423
  }
446
424
  }
447
425
 
448
- Promise.all(arrPromise).then(function () {
426
+ Promise.all(arrPrm).then(function () {
449
427
  resolve(richtextReturn)
450
428
  })
451
429
  })
@@ -462,5 +440,5 @@ export default {
462
440
  imagesUpdate,
463
441
  richtextAppend,
464
442
  richtextDelete,
465
- richtextReturn
443
+ richtextUpdate
466
444
  }
@@ -0,0 +1,29 @@
1
+ // 导入 cheerio
2
+ import * as cheerio from 'cheerio';
3
+
4
+ /**
5
+ * 从 HTML 字符串中提取所有具有 src 属性的标签的 src 值。
6
+ * @param {string} htmlString 富文本 HTML 字符串
7
+ * @returns {string[]} 包含所有 src 属性值的数组
8
+ */
9
+ function extractAllSrc(htmlString) {
10
+ // 注意:cheerio v1.0.0-rc.10 及更高版本需要使用 .load() 的方式来初始化
11
+ const $ = cheerio.load(htmlString);
12
+ const srcList = [];
13
+
14
+ // 查找所有可能带有 src 属性的标签
15
+ const elementsWithSrc = $('img, script[src], iframe, source, embed, track, audio, video');
16
+
17
+ elementsWithSrc.each((index, element) => {
18
+ const src = $(element).attr('src');
19
+ if (src) {
20
+ srcList.push(src);
21
+ }
22
+ });
23
+
24
+ return srcList;
25
+ }
26
+
27
+ export default {
28
+ extractAllSrc
29
+ }
@@ -121,10 +121,13 @@ async function holdSingle(request, response, {
121
121
 
122
122
  // 成功,检查文件信息是否在请求对象中
123
123
  if(result.request.file && result.request.file.filename){
124
- return {
125
- code: 0,
126
- message: '上传成功',
124
+ return {code: 0, message: '上传成功',
127
125
  file: result.request.file
126
+ /* file中的关键信息
127
+ * destination: 文件在服务器上存储的目录路径
128
+ * filename: 文件在服务器上存储时的新名称(通常是 Multer 自动生成的随机字符串或在 storage 配置中定义的名称)
129
+ * path: 文件的完整路径(destination + filename)
130
+ * */
128
131
  };
129
132
  } else {
130
133
  // 这通常发生在未选择文件但 Multer 成功处理请求时
@@ -163,9 +166,7 @@ async function holdArray(request, response, {
163
166
 
164
167
  // 成功,检查文件列表是否在请求对象中
165
168
  if(result.request.files && result.request.files.length > 0){
166
- return {
167
- code: 0,
168
- message: '上传成功',
169
+ return {code: 0, message: '上传成功',
169
170
  files: result.request.files
170
171
  };
171
172
  } else {
File without changes
File without changes
File without changes