添加管理员逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { WarehouseInfo, UserInfo, DeliveryPerson, Order, EmployeeInfo } from '../types';
|
||||
import locationTrackingService from './locationTrackingService';
|
||||
|
||||
|
||||
/**
|
||||
@@ -7,7 +8,7 @@ import { WarehouseInfo, UserInfo, DeliveryPerson, Order, EmployeeInfo } from '..
|
||||
*/
|
||||
|
||||
// API基础URL配置
|
||||
const IS_LOCAL_DEV = true; // true: 本地开发环境, false: 生产环境
|
||||
const IS_LOCAL_DEV = false; // true: 本地开发环境, false: 生产环境
|
||||
const API_BASE_URL = IS_LOCAL_DEV ? 'http://localhost:8080' : 'https://www.doubleyin.cn';
|
||||
|
||||
console.log(`当前API地址: ${API_BASE_URL} (${IS_LOCAL_DEV ? '本地开发环境' : '生产环境'})`);
|
||||
@@ -23,16 +24,7 @@ console.log(`当前API地址: ${API_BASE_URL} (${IS_LOCAL_DEV ? '本地开发环
|
||||
* 封装了所有与后端API交互的方法,并按照功能模块进行组织
|
||||
*/
|
||||
class ApiService {
|
||||
/**
|
||||
* 位置更新回调函数集合
|
||||
*/
|
||||
private locationUpdateCallbacks: Set<(location: any) => void> | null;
|
||||
|
||||
/**
|
||||
* 在线用户列表回调函数集合
|
||||
*/
|
||||
private onlineUserListCallbacks: Set<(userList: any) => void> | null;
|
||||
|
||||
|
||||
/**
|
||||
* 位置更新WebSocket连接
|
||||
*/
|
||||
@@ -42,8 +34,6 @@ class ApiService {
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
this.locationUpdateCallbacks = null;
|
||||
this.onlineUserListCallbacks = null;
|
||||
this.locationWebSocket = null;
|
||||
}
|
||||
|
||||
@@ -148,28 +138,27 @@ async ServerLogin(code: string) {
|
||||
|
||||
// 构建请求数据,必须包含位置数据
|
||||
const requestData: any = {
|
||||
userId,
|
||||
latitude: initialLocation.latitude,
|
||||
longitude: initialLocation.longitude,
|
||||
timestamp: initialLocation.timestamp
|
||||
};
|
||||
|
||||
// 服务器返回的是用户对象,需要转换为前端期望的格式
|
||||
// 服务器现在返回统一的DTO格式,直接使用
|
||||
const response = await this.request<any>('/user/signin', {
|
||||
method: 'POST',
|
||||
data: requestData,
|
||||
});
|
||||
|
||||
// 转换响应格式
|
||||
// 服务器返回格式:{ success: boolean, userInfo: UserInfoResponse }
|
||||
return {
|
||||
success: true,
|
||||
success: response.success,
|
||||
employeeInfo: {
|
||||
id: response.id,
|
||||
name: response.name,
|
||||
phone: response.phone,
|
||||
role: response.role || 'DELIVERY_PERSON'
|
||||
id: response.userInfo.id,
|
||||
name: response.userInfo.name,
|
||||
phone: response.userInfo.phone,
|
||||
role: response.userInfo.role || 'DELIVERY_PERSON'
|
||||
},
|
||||
message: '签到成功'
|
||||
message: response.message
|
||||
};
|
||||
}
|
||||
|
||||
@@ -181,22 +170,21 @@ async ServerLogin(code: string) {
|
||||
async userRegister(userInfo: { name: string; phone: string }): Promise<{ success: boolean; employeeInfo: EmployeeInfo; message?: string }> {
|
||||
console.log('API userRegister调用,参数userInfo:', userInfo);
|
||||
|
||||
// 服务器返回的是用户对象,需要转换为前端期望的格式
|
||||
// 服务器现在返回统一的DTO格式:{ success: boolean, userInfo: UserInfoResponse }
|
||||
const response = await this.request<any>('/user/register', {
|
||||
method: 'POST',
|
||||
data: userInfo,
|
||||
});
|
||||
|
||||
// 转换响应格式
|
||||
return {
|
||||
success: true,
|
||||
success: response.success,
|
||||
employeeInfo: {
|
||||
id: response.id,
|
||||
name: response.name || userInfo.name, // 如果服务器返回null,使用用户输入的值
|
||||
phone: response.phone || userInfo.phone, // 如果服务器返回null,使用用户输入的值
|
||||
role: response.role || 'DELIVERY_PERSON'
|
||||
id: response.userInfo.id,
|
||||
name: response.userInfo.name || userInfo.name, // 如果服务器返回null,使用用户输入的值
|
||||
phone: response.userInfo.phone || userInfo.phone, // 如果服务器返回null,使用用户输入的值
|
||||
role: response.userInfo.role || 'DELIVERY_PERSON'
|
||||
},
|
||||
message: '注册成功'
|
||||
message: response.message
|
||||
};
|
||||
}
|
||||
|
||||
@@ -217,10 +205,16 @@ async ServerLogin(code: string) {
|
||||
async userSignOut(userId: number): Promise<{ success: boolean; message?: string }> {
|
||||
console.log('API userSignOut调用,参数userId:', userId);
|
||||
|
||||
return await this.request('/user/signout', {
|
||||
// 服务器现在返回统一的DTO格式:{ success: boolean, message: string }
|
||||
const response = await this.request<any>('/user/signout', {
|
||||
method: 'POST',
|
||||
data: { userId },
|
||||
});
|
||||
|
||||
return {
|
||||
success: response.success,
|
||||
message: response.message
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -571,27 +565,6 @@ async ServerLogin(code: string) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 注册在线用户列表回调函数
|
||||
* @param callback 在线用户列表回调函数
|
||||
* @returns 取消订阅函数
|
||||
*/
|
||||
onOnlineUserList(callback: (userList: any) => void): () => void {
|
||||
// 添加回调到集合
|
||||
if (!this.onlineUserListCallbacks) {
|
||||
this.onlineUserListCallbacks = new Set();
|
||||
}
|
||||
this.onlineUserListCallbacks.add(callback);
|
||||
|
||||
// 返回取消订阅函数
|
||||
return () => {
|
||||
if (this.onlineUserListCallbacks) {
|
||||
this.onlineUserListCallbacks.delete(callback);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// ===== 私有属性和方法 =====
|
||||
|
||||
@@ -710,100 +683,9 @@ async ServerLogin(code: string) {
|
||||
console.warn('收到无效的WebSocket消息:', message);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`📡 收到WebSocket消息类型: ${message.type}`, message);
|
||||
|
||||
switch (message.type) {
|
||||
case 'onlineUserList':
|
||||
// 处理在线用户列表消息(服务器发送当前在线用户列表)
|
||||
console.log('👥 处理在线用户列表,用户数量:', message.users ? message.users.length : 0);
|
||||
if (message.users && Array.isArray(message.users)) {
|
||||
this.triggerOnlineUserListCallbacks({
|
||||
type: 'onlineUserList',
|
||||
users: message.users.map((user: any) => ({
|
||||
userId: user.userId,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
userStatus: user.userStatus,
|
||||
lastUpdateTime: user.lastUpdateTime,
|
||||
latitude: user.locationData?.latitude || user.latitude,
|
||||
longitude: user.locationData?.longitude || user.longitude,
|
||||
timestamp: user.locationData?.timestamp || user.timestamp
|
||||
}))
|
||||
});
|
||||
}else {
|
||||
console.warn('❌ onlineUserList消息格式错误,users字段不存在或不是数组');
|
||||
}
|
||||
break;
|
||||
case 'userLocationList':
|
||||
// 处理用户位置列表消息(服务器每30秒发送所有在线用户的位置列表)
|
||||
console.log('👥 处理用户位置列表,用户数量:', message.users ? message.users.length : 0);
|
||||
|
||||
// 确保用户列表存在且是数组
|
||||
if (message.users && Array.isArray(message.users)) {
|
||||
// 转换用户数据格式,确保与位置追踪服务兼容
|
||||
const formattedUsers = message.users.map((user: any) => {
|
||||
// 支持多种数据格式:locationData字段或直接字段
|
||||
const locationData = user.locationData || user;
|
||||
return {
|
||||
userId: user.userId || locationData.userId,
|
||||
name: user.name || user.userName || `用户${user.userId || locationData.userId}`,
|
||||
role: user.role || 'employee',
|
||||
userStatus: user.userStatus !== false, // 转换为布尔值
|
||||
lastUpdateTime: user.lastUpdateTime || locationData.timestamp || Date.now(),
|
||||
latitude: locationData.latitude,
|
||||
longitude: locationData.longitude,
|
||||
timestamp: locationData.timestamp || user.timestamp || Date.now()
|
||||
};
|
||||
});
|
||||
|
||||
console.log('📊 转换后的用户位置数据:', formattedUsers);
|
||||
|
||||
this.triggerOnlineUserListCallbacks({
|
||||
type: 'userLocationList',
|
||||
users: formattedUsers
|
||||
});
|
||||
} else {
|
||||
console.warn('❌ userLocationList消息格式错误,users字段不存在或不是数组');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.warn('收到未知类型的WebSocket消息:', message);
|
||||
break;
|
||||
}
|
||||
locationTrackingService.handleWebSocketMessage(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 触发在线用户列表回调
|
||||
* @param userList 在线用户列表信息
|
||||
*/
|
||||
private triggerOnlineUserListCallbacks(userList: any): void {
|
||||
console.log('🔔 [apiService] 开始触发在线用户列表回调,消息类型:', userList.type);
|
||||
console.log('📊 [apiService] 回调数据内容:', JSON.stringify(userList, null, 2));
|
||||
|
||||
if (this.onlineUserListCallbacks) {
|
||||
console.log(`📞 [apiService] 准备执行 ${this.onlineUserListCallbacks.size} 个回调函数`);
|
||||
let index = 0;
|
||||
this.onlineUserListCallbacks.forEach((callback) => {
|
||||
try {
|
||||
console.log(`🔄 [apiService] 执行第 ${index + 1} 个回调函数`);
|
||||
// 传递正确的数据格式:只传递users数组,而不是整个消息对象
|
||||
callback(userList.users);
|
||||
console.log(`✅ [apiService] 第 ${index + 1} 个回调函数执行成功`);
|
||||
index++;
|
||||
} catch (error) {
|
||||
console.error(`❌ [apiService] 第 ${index + 1} 个回调函数执行失败:`, error);
|
||||
index++;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.warn('⚠️ [apiService] 没有注册的在线用户列表回调函数');
|
||||
}
|
||||
console.log('🏁 [apiService] 在线用户列表回调触发完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送WebSocket消息
|
||||
* @param message 要发送的消息对象
|
||||
@@ -844,7 +726,7 @@ async ServerLogin(code: string) {
|
||||
* 取消订阅位置更新(服务器自动处理,无需发送消息)
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
async unsubscribeFromLocationUpdates(userId: number): Promise<void> {
|
||||
async unsubscribeFromLocationUpdates(_userId: number): Promise<void> {
|
||||
// 服务器会自动处理取消订阅逻辑,无需发送取消订阅消息
|
||||
console.log('位置取消订阅已初始化,服务器将自动处理取消订阅逻辑');
|
||||
}
|
||||
|
Reference in New Issue
Block a user