// api.js - 跨端统一 API 调用层 const isWebView2 = () => { return window.isWebView2 === true; }; const getBaseUrl = () => { if (isWebView2()) { return "app://api/"; } return "https://your-production-api.com/api/"; }; async function callApi(endpoint, options = {}) { const url = getBaseUrl() + endpoint; const fetchOptions = { method: options.method || "GET", headers: { "Content-Type": "application/json", ...(options.headers || {}) }, ...(options.body && { body: JSON.stringify(options.body) }) }; const token = localStorage.getItem("authToken"); if (token) { fetchOptions.headers.Authorization = `Bearer ${token}`; } try { const response = await fetch(url, fetchOptions); const data = await response.json(); console.log(data) if (!response.ok) { throw new Error(data.error || `HTTP ${response.status}`); } return data; } catch (err) { console.error(`API call failed: ${endpoint}`, err); throw err; } } window.api = { getUser: () => callApi("getUser"), processData: (input) => callApi("processData", { method: "POST", body: { input } }) };