js_ryl 1.0.31 → 1.0.32

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": "js_ryl",
3
- "version": "1.0.31",
3
+ "version": "1.0.32",
4
4
  "private": false,
5
5
  "description": "自定义通用js",
6
6
  "author": "renyuliang <785788909@qq.com>",
@@ -0,0 +1,54 @@
1
+ // 处理字符串中的base64图片上传和替换
2
+ export default function base64Img(content, apiUrl, token, data) {
3
+ // 匹配所有 base64 图片(支持多种格式)
4
+ const base64Regex = /<img [^>]*src=['"](data:image[^'"]+base64,[^'"]+)['"][^>]*>/gi;
5
+ let match;
6
+ const replacements = [];
7
+
8
+ // 第一步:收集所有需要替换的 base64 数据
9
+ while ((match = base64Regex.exec(content)) !== null) {
10
+ replacements.push({
11
+ original: match[1],
12
+ base64: match[1]
13
+ });
14
+ }
15
+
16
+ // 第二步:并行上传所有图片
17
+ if (replacements.length > 0) {
18
+ const uploadPromises = replacements.map((replacement) => {
19
+ return fetch(apiUrl, {
20
+ method: "POST",
21
+ headers: {
22
+ "Authorization": "Bearer " + token,
23
+ "Content-Type": "application/json"
24
+ },
25
+ body: JSON.stringify({image: replacement.base64,...data})
26
+ })
27
+ .then(res => res.json())
28
+ .then(data => {
29
+ if (data.code === 200) {
30
+ replacement.url = data.data.url;
31
+ }
32
+ return replacement;
33
+ })
34
+ .catch(error => {
35
+ console.error('上传图片失败:', error);
36
+ return replacement;
37
+ });
38
+ });
39
+
40
+ // 等待所有上传完成
41
+ return Promise.all(uploadPromises)
42
+ .then(results => {
43
+ // 第三步:替换所有 base64 为图片 URL
44
+ results.forEach((result) => {
45
+ if (result.url) {
46
+ content = content.replace(result.original, result.url);
47
+ }
48
+ });
49
+ return content;
50
+ });
51
+ }
52
+
53
+ return Promise.resolve(content);
54
+ }
package/src/index.js CHANGED
@@ -27,6 +27,8 @@ import highLight from "./highLight";
27
27
  import flyToCart from "./flyToCart";
28
28
  // 下载excel
29
29
  import downExcel from "./downExcel";
30
+ // 处理base64的图片为链接
31
+ import base64Img from "./base64Img";
30
32
 
31
33
  export default {
32
34
  reg,
@@ -42,5 +44,6 @@ export default {
42
44
  emoji,
43
45
  highLight,
44
46
  flyToCart,
45
- downExcel
47
+ downExcel,
48
+ base64Img
46
49
  };