From 7fcccae67637b01879759e0a60f033ad8e5e5243 Mon Sep 17 00:00:00 2001 From: liozvqe <108354593+liozvqe@users.noreply.github.com> Date: Sat, 10 May 2025 17:52:27 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E9=80=9A=E7=94=A8=E8=8E=B7=E5=8F=96SQ?= =?UTF-8?q?L=E6=95=B0=E6=8D=AE=E7=9A=84=E5=87=BD=E6=95=B0,=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=BB=E6=84=8F=E4=B8=AA=E5=8F=98=E9=87=8F=E5=8F=82?= =?UTF-8?q?=E6=95=B0.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加一个获取自定义SQL的函数, --- 通用获取SQL数据的函数,支持任意个变量参数.js | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 通用获取SQL数据的函数,支持任意个变量参数.js diff --git a/通用获取SQL数据的函数,支持任意个变量参数.js b/通用获取SQL数据的函数,支持任意个变量参数.js new file mode 100644 index 0000000..5a1f2fc --- /dev/null +++ b/通用获取SQL数据的函数,支持任意个变量参数.js @@ -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); \ No newline at end of file