From 7885cede659f3255be56f77c1eef2ada7387d6f1 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期日, 22 三月 2026 16:23:21 +0800
Subject: [PATCH] 初始化项目
---
src/api.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 50 insertions(+), 0 deletions(-)
diff --git a/src/api.js b/src/api.js
new file mode 100644
index 0000000..eb3a7c5
--- /dev/null
+++ b/src/api.js
@@ -0,0 +1,50 @@
+const config = require("./config");
+
+async function postForm(path, formData) {
+ const baseURL = (config.aliyun && config.aliyun.baseURL) || "https://things.icoldchain.cn/";
+ const base = baseURL.endsWith("/") ? baseURL : `${baseURL}/`;
+ const url = new URL(path, base).toString();
+
+ const body = new URLSearchParams();
+ for (const [key, value] of Object.entries(formData || {})) {
+ body.append(key, value == null ? "" : String(value));
+ }
+
+ const response = await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded"
+ },
+ body: body.toString()
+ });
+
+ const text = await response.text();
+ let data = text;
+ try {
+ data = text ? JSON.parse(text) : null;
+ } catch (_) {}
+
+ // 兼容旧调用方:返回结构保持 { data, status, headers }
+ return {
+ data,
+ status: response.status,
+ headers: Object.fromEntries(response.headers.entries())
+ };
+}
+
+// 兼容旧项目:按相对路径 + 设备号去请求三元组
+function getAliyunDeviceSecret(path, deviceName) {
+ return postForm(path, {
+ isAutoRegister: 1,
+ deviceName
+ });
+}
+
+function getDevices(path, userClient, productCode) {
+ return postForm(path, {
+ client_code: userClient,
+ product_code: productCode
+ });
+}
+
+module.exports = { getAliyunDeviceSecret, getDevices };
\ No newline at end of file
--
Gitblit v1.8.0