chenyc
2026-03-22 7885cede659f3255be56f77c1eef2ada7387d6f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 };