|
|
|
|
@ -5,6 +5,12 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const UIModule = (() => {
|
|
|
|
|
// ---- API 地址 ----
|
|
|
|
|
// 从云服务器加载时,API 同源,用空串
|
|
|
|
|
// 本地部署时(localhost),API 指向云服务器
|
|
|
|
|
const API_BASE = (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')
|
|
|
|
|
? 'http://121.41.216.243' : '';
|
|
|
|
|
|
|
|
|
|
// ---- 状态 ----
|
|
|
|
|
let droneConnected = false;
|
|
|
|
|
let missionRunning = false;
|
|
|
|
|
@ -115,7 +121,7 @@ const UIModule = (() => {
|
|
|
|
|
// ===== 从后端获取飞行中任务 =====
|
|
|
|
|
async function fetchActiveTasks() {
|
|
|
|
|
try {
|
|
|
|
|
const resp = await fetch('/api/tasks');
|
|
|
|
|
const resp = await fetch(API_BASE + '/api/tasks');
|
|
|
|
|
if (!resp.ok) return;
|
|
|
|
|
const data = await resp.json();
|
|
|
|
|
if (data.tasks && Array.isArray(data.tasks)) {
|
|
|
|
|
@ -137,7 +143,7 @@ const UIModule = (() => {
|
|
|
|
|
// ===== 从后端获取需求列表 =====
|
|
|
|
|
async function fetchDemands() {
|
|
|
|
|
try {
|
|
|
|
|
const resp = await fetch('/api/demands');
|
|
|
|
|
const resp = await fetch(API_BASE + '/api/demands');
|
|
|
|
|
if (!resp.ok) return;
|
|
|
|
|
const data = await resp.json();
|
|
|
|
|
if (data.demands && Array.isArray(data.demands)) {
|
|
|
|
|
@ -278,7 +284,7 @@ const UIModule = (() => {
|
|
|
|
|
|
|
|
|
|
async function cancelActiveTask(taskId) {
|
|
|
|
|
try {
|
|
|
|
|
var resp = await fetch('/api/tasks/' + encodeURIComponent(taskId) + '/cancel', { method: 'POST' });
|
|
|
|
|
var resp = await fetch(API_BASE + '/api/tasks/' + encodeURIComponent(taskId) + '/cancel', { method: 'POST' });
|
|
|
|
|
var data = await resp.json();
|
|
|
|
|
if (data.ok) {
|
|
|
|
|
addLog('success', '已取消调度: ' + taskId);
|
|
|
|
|
@ -349,7 +355,7 @@ const UIModule = (() => {
|
|
|
|
|
function onReject() {
|
|
|
|
|
if (!selectedTask) return;
|
|
|
|
|
// 同步更新后端状态,防止轮询时恢复
|
|
|
|
|
fetch('/api/demands/' + selectedTask.id + '/reject', { method: 'POST' });
|
|
|
|
|
fetch(API_BASE + '/api/demands/' + selectedTask.id + '/reject', { method: 'POST' });
|
|
|
|
|
addLog('info', '已拒绝需求: ' + selectedTask.id);
|
|
|
|
|
pendingTasks = pendingTasks.filter(t => t.id !== selectedTask.id);
|
|
|
|
|
renderPendingList();
|
|
|
|
|
@ -367,7 +373,7 @@ const UIModule = (() => {
|
|
|
|
|
|
|
|
|
|
// 向后端创建任务
|
|
|
|
|
try {
|
|
|
|
|
var dispatchResp = await fetch('/api/task/dispatch', {
|
|
|
|
|
var dispatchResp = await fetch(API_BASE + '/api/task/dispatch', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
@ -476,7 +482,7 @@ const UIModule = (() => {
|
|
|
|
|
|
|
|
|
|
async function fetchSoldiers() {
|
|
|
|
|
try {
|
|
|
|
|
const resp = await fetch('/api/soldiers');
|
|
|
|
|
const resp = await fetch(API_BASE + '/api/soldiers');
|
|
|
|
|
if (!resp.ok) return;
|
|
|
|
|
const data = await resp.json();
|
|
|
|
|
if (data.soldiers && Array.isArray(data.soldiers)) {
|
|
|
|
|
@ -527,7 +533,7 @@ const UIModule = (() => {
|
|
|
|
|
|
|
|
|
|
async function fetchZones() {
|
|
|
|
|
try {
|
|
|
|
|
const resp = await fetch('/api/zones');
|
|
|
|
|
const resp = await fetch(API_BASE + '/api/zones');
|
|
|
|
|
if (!resp.ok) return;
|
|
|
|
|
const data = await resp.json();
|
|
|
|
|
if (data.zones && Array.isArray(data.zones)) {
|
|
|
|
|
@ -563,7 +569,7 @@ const UIModule = (() => {
|
|
|
|
|
const description = '无人机被摧毁';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const resp = await fetch('/api/danger-zones', {
|
|
|
|
|
const resp = await fetch(API_BASE + '/api/danger-zones', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ lat, lng, radius, description })
|
|
|
|
|
@ -686,7 +692,7 @@ const UIModule = (() => {
|
|
|
|
|
if (isNaN(lat) || isNaN(lng)) { addLog('warning', '请输入有效的经纬度'); return; }
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var resp = await fetch('/api/zones', {
|
|
|
|
|
var resp = await fetch(API_BASE + '/api/zones', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
@ -712,7 +718,7 @@ const UIModule = (() => {
|
|
|
|
|
|
|
|
|
|
async function deleteZone(id) {
|
|
|
|
|
try {
|
|
|
|
|
var resp = await fetch('/api/zones/' + id, { method: 'DELETE' });
|
|
|
|
|
var resp = await fetch(API_BASE + '/api/zones/' + id, { method: 'DELETE' });
|
|
|
|
|
if (resp.ok) {
|
|
|
|
|
addLog('info', '已删除区域 #' + id);
|
|
|
|
|
fetchZones();
|
|
|
|
|
|