ZhangQiangEcologyKit/通用获取SQL数据的函数,支持任意个变量参数.js
liozvqe 7fcccae676 Create 通用获取SQL数据的函数,支持任意个变量参数.js
增加一个获取自定义SQL的函数,
2025-05-10 17:52:27 +08:00

46 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 通用获取 SQL 数据的函数,支持任意个变量参数
async function fetchResponseData(sqlId, ...values) {
try {
const vars = values.map(value => ({
value: value,
UUID: "",
notCleanExcerpts: ""
}));
// 生成vars: [
//{ value: name, UUID: "", notCleanExcerpts: "" },
//{ value: check_id, UUID: "", notCleanExcerpts: "" }
//]
const params = {
SQLID: sqlId,
vars: vars,
maxRunNum: ""
};
const url = `/api/esb/oa/execute?eventkey=ExecuteMySQL&params=${encodeURIComponent(JSON.stringify(params))}`;
const result = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
}
});
const data = await result.json();
if (data.msg === "执行成功" && Array.isArray(data.data?.res)) {
return data.data.res.map(record => {
const obj = JSON.parse(record);
return Object.fromEntries(
Object.entries(obj).map(([key, val]) => [key.toLowerCase(), val])
);
});
} else {
console.error("返回数据失败msg 不是 '执行成功' 或缺少 res 字段", data);
return [];
}
} catch (error) {
console.error("获取数据失败:", error);
return [];
}
}
//引用函数可传入不同的参数前面数字是自定义SQL语句后面是对应的参数名称
let attendanceData = await fetchResponseData(15, sqr, sjidStr);//获取考勤数据
let rawAttendanceLocation = await fetchResponseData(16, sqr);