This commit is contained in:
wangziqi
2026-01-08 13:23:09 +08:00
parent 177cfd9b9d
commit 35098f3028
57 changed files with 4725 additions and 55 deletions

View File

@@ -275,17 +275,21 @@ async function loadCustomerData() {
if (!user) return;
try {
// 获取所有用户找到自己的customer_id
const usersRes = await http.get(API.USERS);
if (usersRes.code === 200 && usersRes.data) {
const customers = usersRes.data.filter(u => u.role === 'customer');
const currentUser = customers.find(c => c.userId === user.userId);
// 获取用户信息、客户信息、车辆信息
const [usersRes, customersRes] = await Promise.all([
http.get(API.USERS),
http.get(API.CUSTOMERS)
]);
if (currentUser) {
// 加载车辆、工单、预约数据
loadCustomerVehicles(currentUser.userId);
loadCustomerOrders(currentUser.userId);
loadCustomerAppointments(currentUser.userId);
if (usersRes.code === 200 && customersRes.code === 200) {
// 找到当前用户对应的客户记录
const currentCustomer = customersRes.data.find(c => c.userId === user.userId);
if (currentCustomer) {
// 加载车辆、工单、预约数据使用customer_id而不是user_id
loadCustomerVehicles(currentCustomer.customerId);
loadCustomerOrders(currentCustomer.customerId);
loadCustomerAppointments(currentCustomer.customerId);
}
}
} catch (error) {
@@ -294,13 +298,13 @@ async function loadCustomerData() {
}
// 加载客户车辆
async function loadCustomerVehicles(userId) {
async function loadCustomerVehicles(customerId) {
try {
const response = await http.get(API.VEHICLES);
// 使用专门的API端点获取客户的车辆而不是获取所有车辆再过滤
const response = await http.get(API.VEHICLE_CUSTOMER(customerId));
if (response.code === 200 && response.data) {
const myVehicles = response.data.filter(v => v.customerId === userId);
displayVehicles(myVehicles);
populateVehicleSelect(myVehicles);
displayVehicles(response.data);
populateVehicleSelect(response.data);
}
} catch (error) {
console.error('加载车辆失败:', error);
@@ -352,10 +356,10 @@ function populateVehicleSelect(vehicles) {
}
// 加载客户工单
async function loadCustomerOrders(userId) {
async function loadCustomerOrders(customerId) {
try {
const [ordersRes, vehiclesRes] = await Promise.all([
http.get(API.ORDER_CUSTOMER(userId)),
http.get(API.ORDER_CUSTOMER(customerId)),
http.get(API.VEHICLES)
]);
@@ -394,10 +398,10 @@ function displayOrders(orders, vehicles) {
}
// 加载客户预约
async function loadCustomerAppointments(userId) {
async function loadCustomerAppointments(customerId) {
try {
const [appointmentsRes, vehiclesRes] = await Promise.all([
http.get(API.APPOINTMENT_CUSTOMER(userId)),
http.get(API.APPOINTMENT_CUSTOMER(customerId)),
http.get(API.VEHICLES)
]);
@@ -445,10 +449,16 @@ async function cancelAppointment(id) {
const response = await http.put(API.APPOINTMENT_CANCEL(id), {});
if (response.code === 200) {
Utils.showToast('预约已取消', 'success');
// 重新加载预约列表
// 重新加载预约列表 - 需要获取customer_id
const user = Utils.getCurrentUser();
if (user) {
loadCustomerAppointments(user.userId);
const customersRes = await http.get(API.CUSTOMERS);
if (customersRes.code === 200) {
const currentCustomer = customersRes.data.find(c => c.userId === user.userId);
if (currentCustomer) {
loadCustomerAppointments(currentCustomer.customerId);
}
}
}
} else {
Utils.showToast(response.message || '取消失败', 'error');
@@ -490,8 +500,23 @@ async function submitAppointment() {
Utils.loading(true);
try {
// 获取customer_id
const customersRes = await http.get(API.CUSTOMERS);
if (customersRes.code !== 200) {
Utils.showToast('获取客户信息失败', 'error');
Utils.loading(false);
return;
}
const currentCustomer = customersRes.data.find(c => c.userId === user.userId);
if (!currentCustomer) {
Utils.showToast('未找到客户信息', 'error');
Utils.loading(false);
return;
}
const response = await http.post(API.APPOINTMENTS, {
customerId: user.userId,
customerId: currentCustomer.customerId,
vehicleId: parseInt(vehicleId),
serviceType: serviceType,
appointmentTime: appointmentTime,
@@ -504,7 +529,7 @@ async function submitAppointment() {
// 重置表单
document.getElementById('appointmentForm').reset();
// 重新加载预约列表
loadCustomerAppointments(user.userId);
loadCustomerAppointments(currentCustomer.customerId);
} else {
Utils.showToast(response.message || '预约失败', 'error');
}