更新 js/ky.js
This commit is contained in:
parent
3ea9131a34
commit
8f295582ec
15
js/ky.js
15
js/ky.js
@ -97,24 +97,27 @@ const TARGET_URL_KEY = "/api/window/windows";
|
|||||||
};
|
};
|
||||||
|
|
||||||
const existingIndex = dataList.findIndex(item => item.device_id === deviceId);
|
const existingIndex = dataList.findIndex(item => item.device_id === deviceId);
|
||||||
|
let successMsg = "";
|
||||||
|
|
||||||
if (existingIndex !== -1) {
|
if (existingIndex !== -1) {
|
||||||
// 更新
|
// 更新
|
||||||
$.log(`检测到设备 ${deviceId} 已存在,更新 token`);
|
$.log(`检测到设备 ${deviceId} 已存在,更新 token`);
|
||||||
dataList[existingIndex] = newData;
|
dataList[existingIndex] = newData;
|
||||||
|
successMsg = `${deviceId} 更新成功`;
|
||||||
} else {
|
} else {
|
||||||
// 新增
|
// 新增
|
||||||
$.log(`检测到新设备 ${deviceId},添加数据`);
|
$.log(`检测到新设备 ${deviceId},添加数据`);
|
||||||
dataList.push(newData);
|
dataList.push(newData);
|
||||||
|
successMsg = `${deviceId} 添加成功`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newEnvValue = JSON.stringify(dataList);
|
const newEnvValue = JSON.stringify(dataList);
|
||||||
|
|
||||||
// 5. 上传/更新变量
|
// 5. 上传/更新变量
|
||||||
if (targetEnvId) {
|
if (targetEnvId) {
|
||||||
await qlUpdateEnv(token, targetEnvId, "KYAPP", newEnvValue);
|
await qlUpdateEnv(token, targetEnvId, "KYAPP", newEnvValue, successMsg);
|
||||||
} else {
|
} else {
|
||||||
await qlAddEnv(token, [{ name: "KYAPP", value: newEnvValue }]);
|
await qlAddEnv(token, [{ name: "KYAPP", value: newEnvValue }], successMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -188,7 +191,7 @@ async function qlGetEnvs(token) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 青龙 API: 更新变量
|
// 青龙 API: 更新变量
|
||||||
async function qlUpdateEnv(token, id, name, value) {
|
async function qlUpdateEnv(token, id, name, value, msg) {
|
||||||
const options = {
|
const options = {
|
||||||
url: `${QL_URL}/open/envs`,
|
url: `${QL_URL}/open/envs`,
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@ -210,7 +213,7 @@ async function qlUpdateEnv(token, id, name, value) {
|
|||||||
if (err) throw new Error(JSON.stringify(err));
|
if (err) throw new Error(JSON.stringify(err));
|
||||||
const body = JSON.parse(data);
|
const body = JSON.parse(data);
|
||||||
if (body.code === 200) {
|
if (body.code === 200) {
|
||||||
$.msg($.name, "更新成功", `设备 ${name} 数据已更新`);
|
$.msg($.name, "操作成功", msg || `设备 ${name} 数据已更新`);
|
||||||
} else {
|
} else {
|
||||||
$.msg($.name, "更新失败", body.message);
|
$.msg($.name, "更新失败", body.message);
|
||||||
}
|
}
|
||||||
@ -224,7 +227,7 @@ async function qlUpdateEnv(token, id, name, value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 青龙 API: 新增变量
|
// 青龙 API: 新增变量
|
||||||
async function qlAddEnv(token, envsArray) {
|
async function qlAddEnv(token, envsArray, msg) {
|
||||||
const options = {
|
const options = {
|
||||||
url: `${QL_URL}/open/envs`,
|
url: `${QL_URL}/open/envs`,
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@ -242,7 +245,7 @@ async function qlAddEnv(token, envsArray) {
|
|||||||
if (err) throw new Error(JSON.stringify(err));
|
if (err) throw new Error(JSON.stringify(err));
|
||||||
const body = JSON.parse(data);
|
const body = JSON.parse(data);
|
||||||
if (body.code === 200) {
|
if (body.code === 200) {
|
||||||
$.msg($.name, "上传成功", "KYAPP 变量已创建");
|
$.msg($.name, "操作成功", msg || "KYAPP 变量已创建");
|
||||||
} else {
|
} else {
|
||||||
$.msg($.name, "上传失败", body.message);
|
$.msg($.name, "上传失败", body.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user