用户在线状态处理
This commit is contained in:
@@ -7,7 +7,7 @@ import { WarehouseInfo, UserInfo, DeliveryPerson, Order, EmployeeInfo } from '..
|
||||
*/
|
||||
|
||||
// API基础URL配置
|
||||
const IS_LOCAL_DEV = false; // true: 本地开发环境, false: 生产环境
|
||||
const IS_LOCAL_DEV = true; // 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 ? '本地开发环境' : '生产环境'})`);
|
||||
@@ -140,15 +140,24 @@ async ServerLogin(code: string) {
|
||||
/**
|
||||
* 签到接口
|
||||
* @param userId 用户ID
|
||||
* @param initialLocation 初始位置数据(必须)
|
||||
* @returns 签到结果和员工信息
|
||||
*/
|
||||
async userSignIn(userId: number): Promise<{ success: boolean; employeeInfo: EmployeeInfo; message?: string }> {
|
||||
console.log('API userSignIn调用,参数userId:', userId);
|
||||
async userSignIn(userId: number, initialLocation: { latitude: number; longitude: number; timestamp: number }): Promise<{ success: boolean; employeeInfo: EmployeeInfo; message?: string }> {
|
||||
console.log('API userSignIn调用,参数userId:', userId, 'initialLocation:', initialLocation);
|
||||
|
||||
// 构建请求数据,必须包含位置数据
|
||||
const requestData: any = {
|
||||
userId,
|
||||
latitude: initialLocation.latitude,
|
||||
longitude: initialLocation.longitude,
|
||||
timestamp: initialLocation.timestamp
|
||||
};
|
||||
|
||||
// 服务器返回的是用户对象,需要转换为前端期望的格式
|
||||
const response = await this.request<any>('/user/signin', {
|
||||
method: 'POST',
|
||||
data: { userId },
|
||||
data: requestData,
|
||||
});
|
||||
|
||||
// 转换响应格式
|
||||
@@ -562,29 +571,7 @@ async ServerLogin(code: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册位置更新回调函数
|
||||
* @param callback 位置更新回调函数
|
||||
* @returns 取消订阅函数
|
||||
*/
|
||||
onLocationUpdate(callback: (location: any) => void): () => void {
|
||||
// 添加回调到集合
|
||||
if (!this.locationUpdateCallbacks) {
|
||||
this.locationUpdateCallbacks = new Set();
|
||||
}
|
||||
this.locationUpdateCallbacks.add(callback);
|
||||
|
||||
// 返回取消订阅函数
|
||||
return () => {
|
||||
if (this.locationUpdateCallbacks) {
|
||||
this.locationUpdateCallbacks.delete(callback);
|
||||
// 如果没有回调了,可以关闭WebSocket连接
|
||||
if (this.locationUpdateCallbacks.size === 0 && this.locationWebSocket) {
|
||||
this.closeLocationWebSocket();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注册在线用户列表回调函数
|
||||
@@ -625,8 +612,18 @@ async ServerLogin(code: string) {
|
||||
const serverUrl = API_BASE_URL.replace('http', 'ws');
|
||||
const wsUrl = `${serverUrl}/ws/location`;
|
||||
|
||||
// 获取当前用户ID(从全局应用数据中获取)
|
||||
const app = getApp<any>();
|
||||
const userInfo = app.globalData.userInfo;
|
||||
const userId = userInfo?.id;
|
||||
|
||||
// 构建带用户ID参数的WebSocket URL
|
||||
const finalWsUrl = userId ? `${wsUrl}?userId=${userId}` : wsUrl;
|
||||
|
||||
console.log(`WebSocket连接URL: ${finalWsUrl}`);
|
||||
|
||||
this.locationWebSocket = wx.connectSocket({
|
||||
url: wsUrl,
|
||||
url: finalWsUrl,
|
||||
header: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
@@ -714,18 +711,61 @@ async ServerLogin(code: string) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`📡 收到WebSocket消息类型: ${message.type}`, message);
|
||||
|
||||
switch (message.type) {
|
||||
case 'updateLocation':
|
||||
// 处理位置更新消息
|
||||
this.triggerLocationUpdateCallbacks(message);
|
||||
break;
|
||||
case 'onlineUserList':
|
||||
// 处理在线用户列表消息
|
||||
this.triggerOnlineUserListCallbacks(message);
|
||||
// 处理在线用户列表消息(服务器发送当前在线用户列表)
|
||||
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 'subscribed':
|
||||
// 处理订阅成功响应
|
||||
console.log('WebSocket订阅成功:', message);
|
||||
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);
|
||||
@@ -733,41 +773,40 @@ async ServerLogin(code: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发位置更新回调
|
||||
* @param locationUpdate 位置更新信息
|
||||
*/
|
||||
private triggerLocationUpdateCallbacks(locationUpdate: any): void {
|
||||
if (this.locationUpdateCallbacks) {
|
||||
this.locationUpdateCallbacks.forEach(callback => {
|
||||
try {
|
||||
callback(locationUpdate);
|
||||
} catch (error) {
|
||||
console.error('位置更新回调执行失败:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 触发在线用户列表回调
|
||||
* @param userList 在线用户列表信息
|
||||
*/
|
||||
private triggerOnlineUserListCallbacks(userList: any): void {
|
||||
console.log('🔔 [apiService] 开始触发在线用户列表回调,消息类型:', userList.type);
|
||||
console.log('📊 [apiService] 回调数据内容:', JSON.stringify(userList, null, 2));
|
||||
|
||||
if (this.onlineUserListCallbacks) {
|
||||
this.onlineUserListCallbacks.forEach(callback => {
|
||||
console.log(`📞 [apiService] 准备执行 ${this.onlineUserListCallbacks.size} 个回调函数`);
|
||||
let index = 0;
|
||||
this.onlineUserListCallbacks.forEach((callback) => {
|
||||
try {
|
||||
callback(userList);
|
||||
console.log(`🔄 [apiService] 执行第 ${index + 1} 个回调函数`);
|
||||
// 传递正确的数据格式:只传递users数组,而不是整个消息对象
|
||||
callback(userList.users);
|
||||
console.log(`✅ [apiService] 第 ${index + 1} 个回调函数执行成功`);
|
||||
index++;
|
||||
} catch (error) {
|
||||
console.error('在线用户列表回调执行失败:', error);
|
||||
console.error(`❌ [apiService] 第 ${index + 1} 个回调函数执行失败:`, error);
|
||||
index++;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.warn('⚠️ [apiService] 没有注册的在线用户列表回调函数');
|
||||
}
|
||||
console.log('🏁 [apiService] 在线用户列表回调触发完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送WebSocket消息
|
||||
* @param message 要发送的消息
|
||||
* @param message 要发送的消息对象
|
||||
*/
|
||||
private sendWebSocketMessage(message: any): void {
|
||||
if (!this.locationWebSocket) {
|
||||
|
Reference in New Issue
Block a user