三方接口对接
This commit is contained in:
58
src/service/api/api.js
Normal file
58
src/service/api/api.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE;
|
||||
|
||||
const MOCK_DELAY = 300;
|
||||
|
||||
const mock = (data, delay = MOCK_DELAY) =>
|
||||
new Promise((resolve) => setTimeout(() => resolve(data), delay));
|
||||
|
||||
export const fetchApi = async (url, options = {}) => {
|
||||
const fullUrl = url.startsWith("http") ? url : BASE_URL + url;
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const config = {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
...options.headers,
|
||||
},
|
||||
...options,
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch(fullUrl, config);
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
throw new Error(data.message || `请求失败: ${res.status}`);
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(`[API] ${options.method || "GET"} ${fullUrl}`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const get = (url, params, options = {}) => {
|
||||
const query = params
|
||||
? "?" + new URLSearchParams(params).toString()
|
||||
: "";
|
||||
return fetchApi(url + query, { ...options, method: "GET" });
|
||||
};
|
||||
|
||||
export const post = (url, data, options = {}) =>
|
||||
fetchApi(url, {
|
||||
...options,
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
export const put = (url, data, options = {}) =>
|
||||
fetchApi(url, {
|
||||
...options,
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
export const del = (url, options = {}) =>
|
||||
fetchApi(url, { ...options, method: "DELETE" });
|
||||
|
||||
export { mock };
|
||||
25
src/service/api/progress.js
Normal file
25
src/service/api/progress.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { get, post } from "./api.js";
|
||||
|
||||
export const FILTER_NAMES = ["隧道工程", "桥梁工程", "道路工程"];
|
||||
|
||||
export const DEFAULT_PROJECT_ID = "12e3c0eb186243869d94e214363ba083";
|
||||
|
||||
export const progressApi = {
|
||||
getTree(parentId) {
|
||||
const params = { projectId: DEFAULT_PROJECT_ID };
|
||||
if (parentId) params.parentId = parentId;
|
||||
return get("/api/bim/wbs", params).then((res) => {
|
||||
const raw = res?.data?.data || [];
|
||||
if (!parentId) {
|
||||
return raw.filter((item) => FILTER_NAMES.includes(item.name));
|
||||
}
|
||||
return raw;
|
||||
});
|
||||
},
|
||||
|
||||
getPjProgress({ projectId, positionIds, period }) {
|
||||
return post("/api/bim/getPjDayListByPositionId", { projectId, positionIds, period }).then((res) => {
|
||||
return res?.data?.data || [];
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user