@hzab/utils 1.0.1-beta → 1.0.1-beta1
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 +1 -1
- package/src/scan-qr.ts +79 -0
package/package.json
CHANGED
package/src/scan-qr.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import OssUploads from "./upload//OssUploadUtil";
|
|
2
|
+
import { nanoidNumALetters } from './nanoid'
|
|
3
|
+
/**
|
|
4
|
+
* 将对象序列化为 JSON 字符串,然后转换为 Blob 二进制流
|
|
5
|
+
* @param {Object} obj - 需要转换的对象
|
|
6
|
+
* @param {string} [charset='utf-8'] - 字符集的 charset
|
|
7
|
+
* @returns {Blob} - 转换后的 Blob 对象
|
|
8
|
+
*/
|
|
9
|
+
function objToBlob(obj: object, charset = "utf-8") {
|
|
10
|
+
const jsonStr = JSON.stringify(obj);
|
|
11
|
+
const blob = new Blob([jsonStr], {
|
|
12
|
+
type: `application/json;charset=${charset}`,
|
|
13
|
+
});
|
|
14
|
+
return blob;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
// 定义更清晰的类型
|
|
19
|
+
|
|
20
|
+
interface GetCodeOptProps {
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
env: "test" | "prod";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 获取二维码信息
|
|
27
|
+
* @param id 二维码ID(字符串或数字类型)
|
|
28
|
+
* @param opt 配置项
|
|
29
|
+
* @returns Promise 对象,便于外部处理异步逻辑
|
|
30
|
+
*/
|
|
31
|
+
const getCodeInfo = async (
|
|
32
|
+
id: string | number,
|
|
33
|
+
opt: GetCodeOptProps
|
|
34
|
+
): Promise<unknown> => {
|
|
35
|
+
const { env, baseUrl } = opt;
|
|
36
|
+
try {
|
|
37
|
+
const url = ` ${baseUrl || "https://open-oss.abt.hzabjt.com"}/qr-code/${env}/${String(id)}`;
|
|
38
|
+
const response = await fetch(url);
|
|
39
|
+
const data = await response.json();
|
|
40
|
+
|
|
41
|
+
return data;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 设置二维码信息
|
|
52
|
+
* @param value 二维码信息对象
|
|
53
|
+
* @param opt 配置项
|
|
54
|
+
* @returns Promise 对象,便于外部处理异步逻辑
|
|
55
|
+
*/
|
|
56
|
+
const setCodeInfo = async (
|
|
57
|
+
value: object,
|
|
58
|
+
): Promise<unknown> => {
|
|
59
|
+
try {
|
|
60
|
+
const id = nanoidNumALetters();
|
|
61
|
+
const file = objToBlob(value) as File;
|
|
62
|
+
const ossUploader = new OssUploads();
|
|
63
|
+
const uploadResult = await ossUploader.upload(file, {
|
|
64
|
+
fileName: id,
|
|
65
|
+
params: {
|
|
66
|
+
dir: "/qr-code/test",
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const fileUrl = uploadResult?.fileUrl;
|
|
70
|
+
return {
|
|
71
|
+
fileUrl,
|
|
72
|
+
id
|
|
73
|
+
};
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export { getCodeInfo, setCodeInfo };
|