@yoooloo42/beat 1.0.29 → 1.0.30
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/dist/index.cjs.js +19 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +19 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +25 -13
- package/src/Ali/Carplate-AppCode.js +0 -71
- package/src/Ali/Carplate-AppKey.js +0 -73
- package/src/Ali/Carplate-VIAPI.js +0 -88
- package/src/Ali/SMS.js +0 -89
- package/src/Ali/index.js +0 -17
- package/src/Email/index.js +0 -113
- package/src/Feie/index.js +0 -170
- package/src/FileDB/ImageSave.js +0 -455
- package/src/FileDB/base64.js +0 -125
- package/src/FileDB/base64.js.test.png +0 -0
- package/src/FileDB/clear.js +0 -85
- package/src/FileDB/convert.js +0 -85
- package/src/FileDB/index.js +0 -26
- package/src/FileDB/richtext.js +0 -32
- package/src/FileDB/upload.js +0 -187
- package/src/FileDB/utf8.js +0 -30
- package/src/WeChat/MPC.js +0 -91
- package/src/WeChat/Token.js +0 -271
- package/src/WeChat/Token.js.txt +0 -50
- package/src/WeChat/index.js +0 -11
- package/src/WeChat-Pay/index.js +0 -23
- package/src/WeChat-Pay/v2micropay.js +0 -80
- package/src/WeChat-Pay/v3close.js +0 -29
- package/src/WeChat-Pay/v3jsapi.js +0 -75
- package/src/WeChat-Pay/v3native.js +0 -50
- package/src/WeChat-Pay/v3out_trade_no.js +0 -31
- package/src/WeChat-Pay/v3sign.js +0 -69
- package/src/Yizoo/index.js +0 -174
- package/src/crypto/AES.js +0 -141
- package/src/crypto/Hash.js +0 -33
- package/src/crypto/RSA.js +0 -145
- package/src/crypto/index.js +0 -14
- package/src/index.js +0 -29
package/src/FileDB/convert.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 将本地文件系统路径转换为可访问的Web URL。
|
|
5
|
-
* * @param filePath - 待转换的本地文件系统绝对路径或相对路径。
|
|
6
|
-
* @param fileRootPath - 文件在服务器上的根存储目录,对应URL中的基础路径之前的部分。
|
|
7
|
-
* 例如: "/Users/user/project/uploads" 或 "D:\\project\\uploads"
|
|
8
|
-
* @param baseUrl - Web访问的基础URL,对应文件路径中的根存储目录。
|
|
9
|
-
* 例如: "https://api.example.com/files"
|
|
10
|
-
* @returns 转换后的Web URL字符串。
|
|
11
|
-
*/
|
|
12
|
-
function pathToUrl(filePath, fileRootPath, baseUrl) {
|
|
13
|
-
// 1. 统一路径分隔符 (解决 Windows/Linux 路径分隔符差异)
|
|
14
|
-
// 将所有 \ 替换为 /
|
|
15
|
-
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
16
|
-
const normalizedRootPath = fileRootPath.replace(/\\/g, '/');
|
|
17
|
-
|
|
18
|
-
// 2. 确保 baseUrl 以 / 结尾 (方便拼接)
|
|
19
|
-
const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
20
|
-
|
|
21
|
-
// 3. 检查文件路径是否包含文件存储根目录
|
|
22
|
-
if (!normalizedPath.startsWith(normalizedRootPath)) {
|
|
23
|
-
// 如果文件路径不在根目录下,可能需要抛出错误或返回空
|
|
24
|
-
console.error(`文件路径不在指定的根目录中:${filePath}`);
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// 4. 提取相对路径部分 (这是路径和URL的"第二部分")
|
|
29
|
-
// 从文件路径中移除根存储目录
|
|
30
|
-
let relativePath = normalizedPath.substring(normalizedRootPath.length);
|
|
31
|
-
|
|
32
|
-
// 5. 确保相对路径以 / 开头,方便拼接
|
|
33
|
-
if (!relativePath.startsWith('/')) {
|
|
34
|
-
relativePath = '/' + relativePath;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 6. 组合 Web URL
|
|
38
|
-
// Web URL = ${Web访问基础URL} + ${相对路径部分}
|
|
39
|
-
const webUrl = normalizedBaseUrl + relativePath;
|
|
40
|
-
|
|
41
|
-
return webUrl;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 将Web URL转换回本地文件系统路径。
|
|
46
|
-
* * @param webUrl - 待转换的Web URL。
|
|
47
|
-
* @param fileRootPath - 文件在服务器上的根存储目录。
|
|
48
|
-
* @param baseUrl - Web访问的基础URL。
|
|
49
|
-
* @returns string
|
|
50
|
-
*/
|
|
51
|
-
function urlToPath(webUrl, baseUrl, fileRootPath) {
|
|
52
|
-
// 1. 确保 baseUrl 以 / 结尾 (方便统一处理)
|
|
53
|
-
const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
54
|
-
|
|
55
|
-
// 2. 检查URL是否包含Web访问的基础URL
|
|
56
|
-
if (!webUrl.startsWith(normalizedBaseUrl)) {
|
|
57
|
-
console.error(`URL不包含指定的Web基础路径:${webUrl}`);
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 3. 提取相对路径部分
|
|
62
|
-
let relativePath = webUrl.substring(normalizedBaseUrl.length);
|
|
63
|
-
|
|
64
|
-
// 4. 确保文件根目录不以 / 结尾 (Node.js的path模块更倾向于不以斜杠结尾)
|
|
65
|
-
const normalizedRootPath = fileRootPath.endsWith('/') ? fileRootPath.slice(0, -1) : fileRootPath;
|
|
66
|
-
|
|
67
|
-
// 5. 组合文件路径
|
|
68
|
-
// 注意:在组合路径时,最好使用 Node.js 内置的 path 模块,以确保跨平台兼容性
|
|
69
|
-
|
|
70
|
-
// path.join 会自动处理多余的斜杠,并使用当前系统的分隔符(Windows下会使用\)
|
|
71
|
-
// 如果您确定只需要 / 分隔符(例如在容器或Linux环境中),可以直接使用字符串拼接
|
|
72
|
-
// return normalizedRootPath + relativePath;
|
|
73
|
-
|
|
74
|
-
// 使用 path 模块(推荐)
|
|
75
|
-
return path.join(normalizedRootPath, relativePath);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export {
|
|
79
|
-
pathToUrl,
|
|
80
|
-
urlToPath
|
|
81
|
-
}
|
|
82
|
-
export default {
|
|
83
|
-
pathToUrl,
|
|
84
|
-
urlToPath
|
|
85
|
-
}
|
package/src/FileDB/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import base64 from './base64.js'
|
|
2
|
-
import clear from './clear.js'
|
|
3
|
-
import convert from './convert.js'
|
|
4
|
-
import ImageSave from './ImageSave.js'
|
|
5
|
-
import richtext from './richtext.js'
|
|
6
|
-
import upload from './upload.js'
|
|
7
|
-
import utf8 from './utf8.js'
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
base64,
|
|
11
|
-
clear,
|
|
12
|
-
convert,
|
|
13
|
-
ImageSave,
|
|
14
|
-
richtext,
|
|
15
|
-
upload,
|
|
16
|
-
utf8
|
|
17
|
-
}
|
|
18
|
-
export default {
|
|
19
|
-
base64,
|
|
20
|
-
clear,
|
|
21
|
-
convert,
|
|
22
|
-
ImageSave,
|
|
23
|
-
richtext,
|
|
24
|
-
upload,
|
|
25
|
-
utf8
|
|
26
|
-
}
|
package/src/FileDB/richtext.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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 {
|
|
28
|
-
extractAllSrc
|
|
29
|
-
}
|
|
30
|
-
export default {
|
|
31
|
-
extractAllSrc
|
|
32
|
-
}
|
package/src/FileDB/upload.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import multer from 'multer';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { access, mkdir, constants } from 'fs/promises';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 运行 Multer 中间件的辅助函数。
|
|
7
|
-
* Multer 中间件需要 (req, res, next) 参数,此函数将其封装为 Promise。
|
|
8
|
-
* @param {Function} uploadMiddleware - Multer 返回的中间件函数 (如 upload.single('file'))
|
|
9
|
-
* @param {object} req - Express 请求对象
|
|
10
|
-
* @param {object} res - Express 响应对象
|
|
11
|
-
* @returns {Promise<{request: object, response: object}>} - 包含 req/res 对象的 Promise
|
|
12
|
-
*/
|
|
13
|
-
function runMulterMiddleware(uploadMiddleware, req, res) {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
// 执行 Multer 中间件
|
|
16
|
-
uploadMiddleware(req, res, (err) => {
|
|
17
|
-
if (err) {
|
|
18
|
-
// 如果 Multer 发生错误 (如文件类型、大小限制),reject Promise
|
|
19
|
-
return reject(err);
|
|
20
|
-
}
|
|
21
|
-
// 成功后,req 对象中会包含 file/files 属性
|
|
22
|
-
resolve({ request: req, response: res });
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 检查并创建目录(如果不存在)
|
|
29
|
-
* @param {string} dirPath - 目录路径
|
|
30
|
-
*/
|
|
31
|
-
async function ensureDirectoryExists(dirPath) {
|
|
32
|
-
try {
|
|
33
|
-
// 尝试访问目录,检查它是否存在
|
|
34
|
-
await access(dirPath, constants.F_OK);
|
|
35
|
-
} catch (error) {
|
|
36
|
-
// 如果目录不存在 (ENOENT)
|
|
37
|
-
if (error.code === 'ENOENT') {
|
|
38
|
-
console.log(`上传目录不存在,正在创建: ${dirPath}`);
|
|
39
|
-
try {
|
|
40
|
-
// 递归创建目录
|
|
41
|
-
await mkdir(dirPath, { recursive: true });
|
|
42
|
-
console.log(`目录创建成功: ${dirPath}`);
|
|
43
|
-
} catch (mkdirError) {
|
|
44
|
-
// 如果创建失败,抛出错误
|
|
45
|
-
throw new Error(`无法创建上传目录 ${dirPath}: ${mkdirError.message}`);
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
// 其他访问错误 (例如权限问题)
|
|
49
|
-
throw new Error(`访问目录 ${dirPath} 时发生错误: ${error.message}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 初始化 Multer 配置
|
|
56
|
-
* @param {object} options
|
|
57
|
-
* @param {string} options.destination - 目标存储目录
|
|
58
|
-
* @param {number} options.fileSize - 文件大小限制
|
|
59
|
-
* @param {string[]} options.fileMimetype - 允许的文件 MimeType 列表
|
|
60
|
-
*/
|
|
61
|
-
async function init({destination, fileSize, fileMimetype}){
|
|
62
|
-
// 1. 确保目标目录在 Multer 初始化前就存在
|
|
63
|
-
await ensureDirectoryExists(destination);
|
|
64
|
-
|
|
65
|
-
// 2. 配置 DiskStorage
|
|
66
|
-
const storage = multer.diskStorage({
|
|
67
|
-
// destination 确定文件存储的目录
|
|
68
|
-
destination: function (req, file, cb) {
|
|
69
|
-
// 目录检查已在 init() 中提前完成,这里直接返回目标路径
|
|
70
|
-
cb(null, destination);
|
|
71
|
-
},
|
|
72
|
-
// filename 确定文件的名称
|
|
73
|
-
filename: function (req, file, cb) {
|
|
74
|
-
// 拼接原始文件名和时间戳,确保文件名的唯一性
|
|
75
|
-
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
|
76
|
-
// 使用 path.extname 获取文件扩展名
|
|
77
|
-
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// 3. 初始化 Multer 实例
|
|
82
|
-
return multer({
|
|
83
|
-
storage: storage,
|
|
84
|
-
// 文件大小限制
|
|
85
|
-
limits: { fileSize },
|
|
86
|
-
// 文件过滤
|
|
87
|
-
fileFilter: (req, file, cb) => {
|
|
88
|
-
if (fileMimetype.includes(file.mimetype)) {
|
|
89
|
-
cb(null, true); // 接受文件
|
|
90
|
-
} else {
|
|
91
|
-
// 拒绝文件,并返回一个错误 (已本地化为中文)
|
|
92
|
-
cb(new Error('文件类型无效,只允许 ' + fileMimetype.join(', ') + ' 格式!'), false);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 处理单个文件上传
|
|
100
|
-
* @param {object} request - Express 请求对象
|
|
101
|
-
* @param {object} response - Express 响应对象
|
|
102
|
-
* @param {object} options - 配置选项
|
|
103
|
-
*/
|
|
104
|
-
async function holdSingle(request, response, {
|
|
105
|
-
destination = 'uploads', // 项目执行的相对路径
|
|
106
|
-
fileSize = 1024 * 1024 * 1, // 默认 1MB
|
|
107
|
-
fileMimetype = [
|
|
108
|
-
'image/jpeg',
|
|
109
|
-
'image/png'
|
|
110
|
-
],
|
|
111
|
-
fieldName = 'file' // 字段名
|
|
112
|
-
}){
|
|
113
|
-
const upload = await init({destination, fileSize, fileMimetype});
|
|
114
|
-
// 获取 Multer 中间件
|
|
115
|
-
const uploadMiddleware = upload.single(fieldName);
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
// 使用 Promise 运行中间件,并等待结果
|
|
119
|
-
const result = await runMulterMiddleware(uploadMiddleware, request, response);
|
|
120
|
-
|
|
121
|
-
// 成功,检查文件信息是否在请求对象中
|
|
122
|
-
if(result.request.file && result.request.file.filename){
|
|
123
|
-
return {code: 0, message: '上传成功',
|
|
124
|
-
file: result.request.file
|
|
125
|
-
/* file中的关键信息
|
|
126
|
-
* destination: 文件在服务器上存储的目录路径
|
|
127
|
-
* filename: 文件在服务器上存储时的新名称(通常是 Multer 自动生成的随机字符串或在 storage 配置中定义的名称)
|
|
128
|
-
* path: 文件的完整路径(destination + filename)
|
|
129
|
-
* */
|
|
130
|
-
};
|
|
131
|
-
} else {
|
|
132
|
-
// 这通常发生在未选择文件但 Multer 成功处理请求时
|
|
133
|
-
return {code: 1, message: '上传失败或未选择文件'};
|
|
134
|
-
}
|
|
135
|
-
} catch (err) {
|
|
136
|
-
// 捕获并拒绝 Multer 产生的错误 (如大小限制、文件类型错误等)
|
|
137
|
-
// 这样外部调用者就可以使用 try...catch 来捕获这些错误
|
|
138
|
-
return Promise.reject(err);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* 处理多个文件上传 (数组)
|
|
144
|
-
* @param {object} request - Express 请求对象
|
|
145
|
-
* @param {object} response - Express 响应对象
|
|
146
|
-
* @param {object} options - 配置选项
|
|
147
|
-
*/
|
|
148
|
-
async function holdArray(request, response, {
|
|
149
|
-
destination = 'uploads',
|
|
150
|
-
fileSize = 1024 * 1024 * 1, // 默认 1MB
|
|
151
|
-
fileMimetype = [
|
|
152
|
-
'image/jpeg',
|
|
153
|
-
'image/png'
|
|
154
|
-
],
|
|
155
|
-
fieldName = 'files',
|
|
156
|
-
maxCount = 10
|
|
157
|
-
}){
|
|
158
|
-
const upload = await init({destination, fileSize, fileMimetype});
|
|
159
|
-
// 获取 Multer 中间件
|
|
160
|
-
const uploadMiddleware = upload.array(fieldName, maxCount);
|
|
161
|
-
|
|
162
|
-
try {
|
|
163
|
-
// 使用 Promise 运行中间件,并等待结果
|
|
164
|
-
const result = await runMulterMiddleware(uploadMiddleware, request, response);
|
|
165
|
-
|
|
166
|
-
// 成功,检查文件列表是否在请求对象中
|
|
167
|
-
if(result.request.files && result.request.files.length > 0){
|
|
168
|
-
return {code: 0, message: '上传成功',
|
|
169
|
-
files: result.request.files
|
|
170
|
-
};
|
|
171
|
-
} else {
|
|
172
|
-
return {code: 1, message: '上传失败或未选择文件'};
|
|
173
|
-
}
|
|
174
|
-
} catch (err) {
|
|
175
|
-
// 捕获并拒绝 Multer 产生的错误
|
|
176
|
-
return Promise.reject(err);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export {
|
|
181
|
-
holdSingle,
|
|
182
|
-
holdArray
|
|
183
|
-
}
|
|
184
|
-
export default {
|
|
185
|
-
holdSingle,
|
|
186
|
-
holdArray
|
|
187
|
-
}
|
package/src/FileDB/utf8.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
|
|
3
|
-
// 异步读取
|
|
4
|
-
async function readFileAsync(filePath) {
|
|
5
|
-
try {
|
|
6
|
-
const data = await fs.readFile(filePath, 'utf8');
|
|
7
|
-
return {code: 0, message: '读取文件内容成功', data}
|
|
8
|
-
} catch (err) {
|
|
9
|
-
return {code: 1, message: '读取文件内容失败', err}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// 同步读取
|
|
14
|
-
function readFileSync(filePath){
|
|
15
|
-
try {
|
|
16
|
-
const data = fs.readFileSync(filePath, 'utf8');
|
|
17
|
-
return {code: 0, message: '读取文件内容成功', data}
|
|
18
|
-
} catch (err) {
|
|
19
|
-
return {code: 1, message: '读取文件内容失败', err}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export {
|
|
24
|
-
readFileAsync,
|
|
25
|
-
readFileSync
|
|
26
|
-
}
|
|
27
|
-
export default {
|
|
28
|
-
readFileAsync,
|
|
29
|
-
readFileSync
|
|
30
|
-
}
|
package/src/WeChat/MPC.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import axios from 'axios'
|
|
2
|
-
import Token from './Token.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @function getMiniProgramCode
|
|
6
|
-
* @description 获取微信小程序的不限制数量二维码(A 或 C 码)。
|
|
7
|
-
* @param {object} para
|
|
8
|
-
* @param {string} para.appid - 小程序 AppID
|
|
9
|
-
* @param {string} para.secret - 小程序 AppSecret
|
|
10
|
-
* @param {string} para.scene - 小程序码中可以附加的场景值(最大 32 个可见字符)
|
|
11
|
-
* @param {string} [para.page] - 必须是已经发布的小程序页面路径,例如 pages/index/index,非必填。
|
|
12
|
-
* @returns {Promise<object>} {code, message, data: {base64, scene}}
|
|
13
|
-
*/
|
|
14
|
-
async function getMiniProgramCode(para) {
|
|
15
|
-
const { appid, secret, scene, page } = para;
|
|
16
|
-
|
|
17
|
-
// 1. 获取全局接口调用凭证 access_token (这里假设 getGlobalAccessToken 已定义)
|
|
18
|
-
// 注意:小程序和公众号共用这一个全局 access_token 接口
|
|
19
|
-
const tokenResult = await Token.getGlobalAccessToken({ appid, secret });
|
|
20
|
-
|
|
21
|
-
if (tokenResult.code !== 0) {
|
|
22
|
-
return {
|
|
23
|
-
code: 1,
|
|
24
|
-
message: `获取接口令牌access_token失败: ${tokenResult.message}`
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
const access_token = tokenResult.data.access_token;
|
|
28
|
-
|
|
29
|
-
// 2. 构造请求参数和 URL
|
|
30
|
-
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${access_token}`;
|
|
31
|
-
|
|
32
|
-
// 微信接口要求 POST 请求体是 JSON 格式
|
|
33
|
-
const requestBody = {
|
|
34
|
-
scene: scene,
|
|
35
|
-
page: page || '', // 默认值,如果未提供
|
|
36
|
-
// width: 430, // 默认宽度,可以根据需要添加
|
|
37
|
-
// auto_color: false, // 默认不自动配置线条颜色,可以根据需要添加
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
// 使用 axios 发送 POST 请求
|
|
42
|
-
const response = await axios.post(url, requestBody, {
|
|
43
|
-
// 关键点:指定 responseType 为 arraybuffer,以正确处理二进制图片流
|
|
44
|
-
responseType: 'arraybuffer',
|
|
45
|
-
// 指定请求头为 JSON
|
|
46
|
-
headers: {
|
|
47
|
-
'Content-Type': 'application/json'
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// 3. 检查微信返回的数据是否为错误信息(错误信息是 JSON 格式)
|
|
52
|
-
const contentType = response.headers['content-type'];
|
|
53
|
-
|
|
54
|
-
// 微信接口返回图片成功时,contentType 是 image/jpeg;返回错误时,是 application/json
|
|
55
|
-
if (contentType && contentType.includes('application/json')) {
|
|
56
|
-
// 将 ArrayBuffer 转换为字符串,然后解析 JSON 错误信息
|
|
57
|
-
const errorBuffer = Buffer.from(response.data);
|
|
58
|
-
const errorData = JSON.parse(errorBuffer.toString('utf8'));
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
code: 1,
|
|
62
|
-
message: `获取微信小程序码失败: ${errorData.errmsg || '未知错误'}`,
|
|
63
|
-
error_data: errorData
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 4. 处理成功返回的图片流
|
|
68
|
-
// response.data 是一个 Buffer 对象 (Node.js 环境下)
|
|
69
|
-
const imageBuffer = Buffer.from(response.data);
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
code: 0,
|
|
73
|
-
message: '获取微信小程序码成功',
|
|
74
|
-
data: {
|
|
75
|
-
// 将二进制 Buffer 转换为 Base64 字符串,并去除 Base64 编码中可能存在的换行符
|
|
76
|
-
base64: imageBuffer.toString('base64').replace(/[\r\n]/g, ''),
|
|
77
|
-
scene: scene
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
} catch (error) {
|
|
82
|
-
// 处理网络请求等底层错误
|
|
83
|
-
return {
|
|
84
|
-
code: 2,
|
|
85
|
-
message: `网络请求失败: ${error.message}`
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export {getMiniProgramCode}
|
|
91
|
-
export default {getMiniProgramCode}
|