2026-03-02 12:29:23 +00:00
|
|
|
|
// API服务层,用于与后端进行交互
|
|
|
|
|
|
|
2026-03-03 02:23:39 +00:00
|
|
|
|
const API_BASE_URL = '/api';
|
2026-03-02 12:29:23 +00:00
|
|
|
|
|
|
|
|
|
|
// 通用请求函数
|
|
|
|
|
|
async function request(url, options = {}) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}${url}`, {
|
|
|
|
|
|
...options,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...options.headers
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-03-03 02:23:39 +00:00
|
|
|
|
|
2026-03-02 12:29:23 +00:00
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
|
}
|
2026-03-03 02:23:39 +00:00
|
|
|
|
|
2026-03-02 12:29:23 +00:00
|
|
|
|
return await response.json();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('API request error:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 区域管理API
|
|
|
|
|
|
export const regionApi = {
|
|
|
|
|
|
getAll: () => request('/regions'),
|
|
|
|
|
|
getById: (id) => request(`/regions/${id}`),
|
|
|
|
|
|
create: (data) => request('/regions', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/regions/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/regions/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 构建查询字符串
|
|
|
|
|
|
function buildQueryString(params) {
|
|
|
|
|
|
const query = Object.entries(params)
|
|
|
|
|
|
.filter(([key, value]) => value !== undefined && value !== null && value !== '')
|
|
|
|
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
|
|
|
|
.join('&');
|
|
|
|
|
|
return query ? `?${query}` : '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 公寓管理API
|
|
|
|
|
|
export const apartmentApi = {
|
|
|
|
|
|
getAll: (params = {}) => request(`/apartments${buildQueryString(params)}`),
|
|
|
|
|
|
getById: (id) => request(`/apartments/${id}`),
|
|
|
|
|
|
create: (data) => request('/apartments', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/apartments/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/apartments/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 房间管理API
|
|
|
|
|
|
export const roomApi = {
|
|
|
|
|
|
getAll: (params = {}) => request(`/rooms${buildQueryString(params)}`),
|
|
|
|
|
|
getById: (id) => request(`/rooms/${id}`),
|
|
|
|
|
|
create: (data) => request('/rooms', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/rooms/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/rooms/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 租客管理API
|
|
|
|
|
|
export const tenantApi = {
|
|
|
|
|
|
getAll: () => request('/tenants'),
|
|
|
|
|
|
getById: (id) => request(`/tenants/${id}`),
|
|
|
|
|
|
create: (data) => request('/tenants', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/tenants/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/tenants/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 合同管理API
|
|
|
|
|
|
export const contractApi = {
|
|
|
|
|
|
getAll: () => request('/contracts'),
|
|
|
|
|
|
getById: (id) => request(`/contracts/${id}`),
|
|
|
|
|
|
create: (data) => request('/contracts', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/contracts/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/contracts/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 租房管理API
|
|
|
|
|
|
export const rentalApi = {
|
|
|
|
|
|
getAll: (params = {}) => request(`/rentals${buildQueryString(params)}`),
|
|
|
|
|
|
getById: (id) => request(`/rentals/${id}`),
|
|
|
|
|
|
create: (data) => request('/rentals', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/rentals/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/rentals/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 统计分析API
|
|
|
|
|
|
export const statisticsApi = {
|
|
|
|
|
|
getRentData: () => request('/statistics/rent'),
|
|
|
|
|
|
getRoomStatus: () => request('/statistics/room-status'),
|
|
|
|
|
|
getRegionHouseStats: () => request('/statistics/region-house'),
|
|
|
|
|
|
getRegionApartmentHouseStats: () => request('/statistics/region-apartment-house')
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 水费管理API
|
|
|
|
|
|
export const waterBillApi = {
|
|
|
|
|
|
getAll: (params = {}) => request(`/water-bills${buildQueryString(params)}`),
|
|
|
|
|
|
getById: (id) => request(`/water-bills/${id}`),
|
|
|
|
|
|
create: (data) => request('/water-bills', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/water-bills/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/water-bills/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 电费管理API
|
|
|
|
|
|
export const electricityBillApi = {
|
|
|
|
|
|
getAll: (params = {}) => request(`/electricity-bills${buildQueryString(params)}`),
|
|
|
|
|
|
getById: (id) => request(`/electricity-bills/${id}`),
|
|
|
|
|
|
create: (data) => request('/electricity-bills', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
update: (id, data) => request(`/electricity-bills/${id}`, {
|
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
|
}),
|
|
|
|
|
|
delete: (id) => request(`/electricity-bills/${id}`, {
|
|
|
|
|
|
method: 'DELETE'
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
region: regionApi,
|
|
|
|
|
|
apartment: apartmentApi,
|
|
|
|
|
|
room: roomApi,
|
|
|
|
|
|
tenant: tenantApi,
|
|
|
|
|
|
contract: contractApi,
|
|
|
|
|
|
rental: rentalApi,
|
|
|
|
|
|
statistics: statisticsApi,
|
|
|
|
|
|
waterBill: waterBillApi,
|
|
|
|
|
|
electricityBill: electricityBillApi
|
2026-03-03 02:23:39 +00:00
|
|
|
|
};
|