import { normalizeResponseData, requestJson } from "./request" const OPEN_API_BASE = import.meta.env.VITE_OPEN_API_BASE || "/openApi" function joinBase(base, path) { if (/^https?:\/\//.test(path)) return path return `${base}${path.startsWith("/") ? path : `/${path}`}` } function unwrapResponse(response) { const failed = response?.success === false || (response?.code !== undefined && ![0, 200, "0", "200"].includes(response.code)) if (failed) { const error = new Error(response?.msg || response?.message || "接口请求失败") error.code = response?.code throw error } return normalizeResponseData(response) } function getV3(path, params, options = {}) { return requestJson(joinBase(OPEN_API_BASE, path), { params, timeout: options.timeout || 20000, }).then(unwrapResponse) } function normalizeRows(data) { if (Array.isArray(data)) return data if (Array.isArray(data?.rows)) return data.rows if (Array.isArray(data?.list)) return data.list if (Array.isArray(data?.data)) return data.data if (data?.data) return normalizeRows(data.data) if (data?.result) return normalizeRows(data.result) return [] } export function getMuhuList(query = "", page = 1, pageSize = 80) { return getV3("/screen2/pasture-management/pastures", { keyword: query, page, pageSize }).then(normalizeRows) } export function getPastureGrasslandsById(id) { if (id === undefined || id === null || id === "") return Promise.resolve([]) return getV3(`/screen2/pasture-management/pastures/${encodeURIComponent(id)}/grasslands`).then(normalizeRows) } export function getPastureClickInfoByWkt(wkt) { if (!wkt) return Promise.resolve(null) return getV3("/screen2/pasture-management/grassland/click", { wkt }).then((data) => data || null) } export function getPastureDetailsById(id) { return getV3(`/screen2/pasture-management/pastures/${encodeURIComponent(id)}`).then((data) => data || {}) } export function getPasturesDetailsById(id) { return getPastureDetailsById(id) } export function getPastureList() { return getV3("/screen2/pasture-management/pastures", { pageSize: 300 }).then(normalizeRows) } export async function getPastureProfile() { const [dashboard, list] = await Promise.all([ getV3("/screen2/pasture-management/dashboard"), getPastureList(), ]) return { core: dashboard?.core || [], distribution: dashboard?.distribution || [], age: dashboard?.demographics || [], online: dashboard?.online || [], grassland: dashboard?.grassland || [], list, } }