Create 通用获取SQL数据的函数,支持任意个变量参数.js
增加一个获取自定义SQL的函数,
This commit is contained in:
parent
d757586b4b
commit
7fcccae676
46
通用获取SQL数据的函数,支持任意个变量参数.js
Normal file
46
通用获取SQL数据的函数,支持任意个变量参数.js
Normal file
@ -0,0 +1,46 @@
|
||||
// 通用获取 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¶ms=${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);
|
Loading…
x
Reference in New Issue
Block a user