Files
crypto_monitor/lib/common/static/options_settings.js
T

115 lines
4.0 KiB
JavaScript

(function () {
"use strict";
const root = document.getElementById("options-settings-root");
if (!root) return;
async function apiJson(url, opts) {
const r = await fetch(url, Object.assign({ credentials: "same-origin" }, opts || {}));
return r.json();
}
function setMsg(id, text, isErr) {
const el = document.getElementById(id);
if (!el) return;
el.textContent = text || "";
el.classList.toggle("opt-error", !!isErr);
}
function wireChipGroup(id) {
const group = document.getElementById(id);
if (!group) return function () {
return null;
};
const chips = group.querySelectorAll(".opt-set-chip");
chips.forEach(function (chip) {
chip.addEventListener("click", function () {
chips.forEach(function (c) {
c.classList.remove("active");
});
chip.classList.add("active");
});
});
return function () {
return group.querySelector(".opt-set-chip.active");
};
}
const activeSwapDir = wireChipGroup("opt-set-swap-dir");
const activeIntDir = wireChipGroup("opt-set-int-dir");
const activeIntCcy = wireChipGroup("opt-set-int-ccy");
const activeCrossDir = wireChipGroup("opt-set-cross-dir");
const activeCrossCcy = wireChipGroup("opt-set-cross-ccy");
const swapBtn = document.getElementById("opt-set-swap-btn");
if (swapBtn) {
swapBtn.addEventListener("click", async function () {
const amount = parseFloat(document.getElementById("opt-set-swap-amount").value);
if (!amount || amount <= 0) {
setMsg("opt-set-swap-msg", "请输入有效数量", true);
return;
}
const dirChip = activeSwapDir();
const d = await apiJson("/api/options/spot/swap", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
direction: (dirChip && dirChip.getAttribute("data-dir")) || "usdt_to_usdc",
amount: amount,
}),
});
setMsg("opt-set-swap-msg", d.ok ? "兑换单已提交" : (d.msg || "失败"), !d.ok);
});
}
const intBtn = document.getElementById("opt-set-int-btn");
if (intBtn) {
intBtn.addEventListener("click", async function () {
const amount = parseFloat(document.getElementById("opt-set-int-amount").value);
if (!amount || amount <= 0) {
setMsg("opt-set-int-msg", "请输入有效数量", true);
return;
}
const dirChip = activeIntDir();
const ccyChip = activeIntCcy();
const d = await apiJson("/api/options/transfer", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
ccy: (ccyChip && ccyChip.getAttribute("data-ccy")) || "USDC",
from: (dirChip && dirChip.getAttribute("data-from")) || "funding",
to: (dirChip && dirChip.getAttribute("data-to")) || "trading",
amount: amount,
}),
});
setMsg("opt-set-int-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
});
}
const crossBtn = document.getElementById("opt-set-cross-btn");
if (crossBtn) {
crossBtn.addEventListener("click", async function () {
const amount = parseFloat(document.getElementById("opt-set-cross-amount").value);
if (!amount || amount <= 0) {
setMsg("opt-set-cross-msg", "请输入有效数量", true);
return;
}
const dirChip = activeCrossDir();
const ccyChip = activeCrossCcy();
const d = await apiJson("/api/options/cross-transfer", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
ccy: (ccyChip && ccyChip.getAttribute("data-ccy")) || "USDT",
amount: amount,
from_account: "funding",
to_account: "funding",
direction: (dirChip && dirChip.getAttribute("data-dir")) || "sub_to_main",
}),
});
setMsg("opt-set-cross-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
if (d.ok && typeof refreshAccountSnapshot === "function") refreshAccountSnapshot();
});
}
})();