@yoooloo42/beat 1.0.16 → 1.0.17

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.16",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -22,6 +22,7 @@
22
22
  },
23
23
  "exports": {
24
24
  "./libs/*": "./src/libs/*.js",
25
+ "./libs/Ali/*": "./src/libs/Ali/*.js",
25
26
  "./libs/crypto/*": "./src/libs/crypto/*.js"
26
27
  },
27
28
  "files": [
@@ -0,0 +1,70 @@
1
+ // 优化后的第二个程序:API签名认证调用方法(AppKey & AppSecret)
2
+
3
+ import ali from 'aliyun-api-gateway'
4
+
5
+ /**
6
+ * 使用阿里云市场 API 网关进行车牌识别 (AppKey & AppSecret 签名认证)
7
+ * @param {object} para - 参数对象
8
+ * @param {string} para.imageUrl - 图片的公网 URL
9
+ * @param {string} [para.appKey] - 可选,AppKey,优先从环境变量 ALIBABA_CLOUD_APP_KEY 获取
10
+ * @param {string} [para.appSecret] - 可选,AppSecret,优先从环境变量 ALIBABA_CLOUD_APP_SECRET 获取
11
+ * @returns {Promise<object>} 包含识别结果的对象
12
+ */
13
+ async function carplate(para) {
14
+ // 1. **规范:** 优先从环境变量读取 AppKey 和 AppSecret,提高安全性。
15
+ const appKey = para.appKey || process.env.ALIBABA_CLOUD_APP_KEY;
16
+ const appSecret = para.appSecret || process.env.ALIBABA_CLOUD_APP_SECRET;
17
+
18
+ if (!appKey || !appSecret) {
19
+ return {
20
+ code: -1,
21
+ message: "AppKey 或 AppSecret 缺失,请配置或传入!"
22
+ };
23
+ }
24
+
25
+ const Client = ali.Client
26
+ const client = new Client(appKey, appSecret)
27
+
28
+ try {
29
+ const requestUrl = 'https://ocrcp.market.alicloudapi.com/rest/160601/ocr/ocr_vehicle_plate.json'
30
+
31
+ // 发起 POST 请求
32
+ const result = await client.post(requestUrl, {
33
+ data: {
34
+ 'image': para.imageUrl,
35
+ 'configure': '{"multi_crop":false}'
36
+ },
37
+ headers: {
38
+ accept: 'application/json'
39
+ }
40
+ })
41
+
42
+ // 2. 检查返回结构,确保结果有效
43
+ if (result && result.plates && result.plates.length > 0) {
44
+ return {
45
+ code: 0,
46
+ message: "车牌识别成功",
47
+ result: result.plates[0]
48
+ }
49
+ } else {
50
+ return {
51
+ code: 1,
52
+ message: "车牌识别成功,但未检测到有效车牌。",
53
+ result: null
54
+ };
55
+ }
56
+
57
+ } catch (err) {
58
+ // 3. **规范:** 使用 try...catch 捕获错误,并返回非零 code
59
+ const errorMessage = err.message || err.stack || "未知错误";
60
+
61
+ return {
62
+ code: -1,
63
+ message: "车牌识别失败:" + errorMessage
64
+ }
65
+ }
66
+ }
67
+
68
+ export default {
69
+ carplate
70
+ }
@@ -0,0 +1,68 @@
1
+ // 优化后的第一个程序:API简单身份认证调用方法(APPCODE)
2
+
3
+ import ali from 'aliyun-api-gateway'
4
+
5
+ /**
6
+ * 使用阿里云市场 API 网关进行车牌识别 (APPCODE 认证)
7
+ * @param {object} para - 参数对象
8
+ * @param {string} para.imageUrl - 图片的公网 URL
9
+ * @param {string} [para.appCode] - 可选,APPCODE,优先从环境变量 ALIBABA_CLOUD_APPCODE 获取
10
+ * @returns {Promise<object>} 包含识别结果的对象
11
+ */
12
+ async function carplate(para) {
13
+ // 1. **规范:** 优先从环境变量读取 APPCODE,提高安全性。
14
+ const appCode = para.appCode || process.env.ALIBABA_CLOUD_APPCODE;
15
+
16
+ if (!appCode) {
17
+ return {
18
+ code: -1,
19
+ message: "APPCODE 缺失,请配置环境变量 ALIBABA_CLOUD_APPCODE 或传入!"
20
+ };
21
+ }
22
+
23
+ const SimpleClient = ali.SimpleClient
24
+ const client = new SimpleClient(appCode)
25
+
26
+ try {
27
+ const requestUrl = 'https://ocrcp.market.alicloudapi.com/rest/160601/ocr/ocr_vehicle_plate.json'
28
+
29
+ // 发起 POST 请求
30
+ const result = await client.post(requestUrl, {
31
+ data: {
32
+ 'image': para.imageUrl,
33
+ 'configure': '{"multi_crop":false}'
34
+ },
35
+ headers: {
36
+ accept: 'application/json'
37
+ }
38
+ })
39
+
40
+ // 2. 检查返回结构,确保结果有效
41
+ if (result && result.plates && result.plates.length > 0) {
42
+ return {
43
+ code: 0,
44
+ message: "车牌识别成功",
45
+ result: result.plates[0]
46
+ }
47
+ } else {
48
+ return {
49
+ code: 1,
50
+ message: "车牌识别成功,但未检测到有效车牌。",
51
+ result: null
52
+ };
53
+ }
54
+
55
+ } catch (err) {
56
+ // 3. **规范:** 使用 try...catch 捕获错误,并返回非零 code
57
+ const errorMessage = err.message || err.stack || "未知错误";
58
+
59
+ return {
60
+ code: -1,
61
+ message: "车牌识别失败:" + errorMessage
62
+ }
63
+ }
64
+ }
65
+
66
+ export default {
67
+ carplate
68
+ }
@@ -0,0 +1,85 @@
1
+ // 规范优化后的车牌识别程序 (基于 VIAPI 官方 SDK)
2
+
3
+ import OcrClient from '@alicloud/ocr20191230'
4
+ import OpenapiClient from '@alicloud/openapi-client'
5
+ import TeaUtil from '@alicloud/tea-util'
6
+
7
+ /**
8
+ * 使用阿里云 VIAPI 官方 SDK 进行车牌识别 (AccessKey 认证)
9
+ * @param {object} para - 参数对象
10
+ * @param {string} para.imageUrl - 图片的公网 URL
11
+ * @param {string} [para.accessKeyId] - 可选,AccessKey ID,优先从环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 获取
12
+ * @param {string} [para.accessKeySecret] - 可选,AccessKey Secret,优先从环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET 获取
13
+ * @returns {Promise<object>} 包含识别结果的对象
14
+ */
15
+ async function carplate(para) {
16
+ // 1. **规范:** 优先从环境变量读取密钥,提高安全性。
17
+ const accessKeyId = para.accessKeyId || process.env.ALIBABA_CLOUD_ACCESS_KEY_ID || 'YOUR_DEFAULT_ACCESS_KEY_ID';
18
+ const accessKeySecret = para.accessKeySecret || process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET || 'YOUR_DEFAULT_ACCESS_KEY_SECRET';
19
+
20
+ // **注意:生产环境中必须配置环境变量,并移除默认硬编码值!**
21
+
22
+ if (!accessKeyId || !accessKeySecret) {
23
+ throw new Error("AccessKeyId 和 AccessKeySecret 缺失,请配置或传入!");
24
+ }
25
+
26
+ const config = new OpenapiClient.Config({
27
+ accessKeyId: accessKeyId,
28
+ accessKeySecret: accessKeySecret
29
+ });
30
+
31
+ // 访问的域名
32
+ config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
33
+ const client = new OcrClient.default(config);
34
+
35
+ try {
36
+ // 2. **规范:** 使用 RecognizeLicensePlateRequest,直接传入 ImageURL 字符串,简化调用
37
+ let recognizeLicensePlateRequest = new OcrClient.RecognizeLicensePlateRequest({
38
+ imageURL: para.imageUrl, // 直接传入 URL 字符串
39
+ });
40
+
41
+ let runtime = new TeaUtil.RuntimeOptions({
42
+ // 可以配置网络超时等参数
43
+ connectTimeout: 5000,
44
+ readTimeout: 10000,
45
+ });
46
+
47
+ const response = await client.recognizeLicensePlateWithOptions(recognizeLicensePlateRequest, runtime);
48
+
49
+ // 确保结果有效
50
+ if (response.body && response.body.data && response.body.data.plates && response.body.data.plates.length > 0) {
51
+ return {
52
+ code: 0,
53
+ message: "车牌识别成功",
54
+ result: response.body.data.plates[0]
55
+ };
56
+ } else {
57
+ return {
58
+ code: 1,
59
+ message: "车牌识别成功,但未检测到有效车牌。",
60
+ result: null
61
+ };
62
+ }
63
+
64
+ } catch (error) {
65
+ // 3. **规范:** 使用 try...catch 捕获错误,并 reject 或 throw Error,而不是返回 code: 0。
66
+
67
+ // 提取阿里云的错误码和信息
68
+ const errorCode = error.data ? error.data.Code : 'UNKNOWN_ERROR';
69
+ const errorMessage = error.message || '未知错误';
70
+
71
+ // 统一返回失败信息
72
+ return {
73
+ code: -1, // 使用 -1 或其他负值表示系统级/网络级错误
74
+ message: `车牌识别失败: [${errorCode}] ${errorMessage}`,
75
+ error: error
76
+ };
77
+
78
+ // 另一种规范是直接 throw Error,让调用方处理
79
+ // throw new Error(`车牌识别失败: [${errorCode}] ${errorMessage}`);
80
+ }
81
+ }
82
+
83
+ export default {
84
+ carplate
85
+ }
File without changes