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 };
|