refactor: 对齐后端样例工程规范

This commit is contained in:
2026-07-26 18:24:13 +08:00
parent 4382641e19
commit 4f2bb7e88b
66 changed files with 1869 additions and 660 deletions

View File

@@ -1 +1,3 @@
VITE_API_BASE_URL=http://localhost:8080/api/v1
VITE_API_BASE_URL=http://localhost:12426/Platform/v1
# 受保护 API 使用 BSM JWT本地联调时由认证服务签发不要提交真实令牌。
VITE_DEV_JWT=

View File

@@ -1,5 +1,6 @@
// 平台总后台 API 客户端,仅封装首期 M0 + M1 接口。
const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080/api/v1';
const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:12426/Platform/v1';
const developmentJWT = import.meta.env.VITE_DEV_JWT?.trim();
export type OrgGasStation = {
identity: string;
@@ -48,23 +49,39 @@ export type SafEvent = {
export type DashboardOverview = Record<string, number>;
type ListReply<T> = { total: number; list: T[] };
type RawOrgDeliveryPoint = { identity: string; delivery_code: string; name: string; gas_station_name?: string; service_area: string; status: string };
type RawOrgServicePerson = { identity: string; name: string; phone_masked?: string; roles: string; work_status: string; credential_status: string };
type RawIdnAccount = { identity: string; phone_masked: string; account_type: string; status: string; service_area: string };
type RawSafEvent = { identity: string; event_code: string; level: number; title: string; status: string; created_at: string };
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const response = await fetch(`${apiBaseURL}${path}`, {
headers: { 'Content-Type': 'application/json', ...(init?.headers ?? {}) },
headers: {
'Content-Type': 'application/json',
...(developmentJWT ? { Authorization: `Bearer ${developmentJWT}` } : {}),
...(init?.headers ?? {}),
},
...init,
});
const payload = (await response.json()) as { data?: T; error?: string };
if (!response.ok || payload.error) throw new Error(payload.error || '请求失败');
return payload.data as T;
const payload = (await response.json()) as { code?: number; message?: string; details?: T };
if (!response.ok || payload.code !== 0) throw new Error(payload.message || '请求失败');
return payload.details as T;
}
export const platformAPI = {
getDashboard: () => request<DashboardOverview>('/dashboard/overview'),
listOrgGasStation: () => request<OrgGasStation[]>('/org/gas-station'),
getDashboard: async () => {
const item = await request<{ gas_station_count: number; delivery_point_count: number; service_person_count: number; user_count: number; pending_safety_count: number }>('/dashboard/overview');
return { gasStationCount: item.gas_station_count, deliveryPointCount: item.delivery_point_count, servicePersonCount: item.service_person_count, userCount: item.user_count, pendingSafetyCount: item.pending_safety_count };
},
listOrgGasStation: async () => {
const reply = await request<ListReply<{ identity: string; station_code: string; name: string; principal: string; service_area: string; status: string; created_at: string }>>('/organization/org_gas_station');
return reply.list.map((item) => ({ identity: item.identity, stationCode: item.station_code, name: item.name, principal: item.principal, serviceArea: item.service_area, status: item.status, createdAt: item.created_at }));
},
createOrgGasStation: (body: Pick<OrgGasStation, 'stationCode' | 'name' | 'principal' | 'serviceArea'>) =>
request<OrgGasStation>('/org/gas-station', { method: 'POST', body: JSON.stringify(body) }),
listOrgDeliveryPoint: () => request<OrgDeliveryPoint[]>('/org/delivery-point'),
listOrgServicePerson: () => request<OrgServicePerson[]>('/org/service-person'),
listIdnAccount: () => request<IdnAccount[]>('/idn/account'),
listSafEvent: () => request<SafEvent[]>('/saf/event'),
request<OrgGasStation>('/organization/org_gas_station', { method: 'POST', body: JSON.stringify({ station_code: body.stationCode, name: body.name, principal: body.principal, service_area: body.serviceArea }) }),
listOrgDeliveryPoint: async () => (await request<ListReply<RawOrgDeliveryPoint>>('/organization/org_delivery_point')).list.map((item) => ({ identity: item.identity, deliveryCode: item.delivery_code, name: item.name, gasStationName: item.gas_station_name ?? '未关联', serviceArea: item.service_area, status: item.status })),
listOrgServicePerson: async () => (await request<ListReply<RawOrgServicePerson>>('/organization/org_service_person')).list.map((item) => ({ identity: item.identity, name: item.name, phoneMasked: item.phone_masked ?? '***', roles: item.roles, workStatus: item.work_status, credentialStatus: item.credential_status })),
listIdnAccount: async () => (await request<ListReply<RawIdnAccount>>('/identity/idn_account')).list.map((item) => ({ identity: item.identity, phoneMasked: item.phone_masked, accountType: item.account_type, status: item.status, serviceArea: item.service_area })),
listSafEvent: async () => (await request<ListReply<RawSafEvent>>('/safety/saf_event')).list.map((item) => ({ identity: item.identity, eventCode: item.event_code, level: item.level, title: item.title, status: item.status, createdAt: item.created_at })),
};