修改位置交互,修改代码逻辑
This commit is contained in:
@@ -16,7 +16,7 @@ console.log(`当前API地址: ${API_BASE_URL} (${IS_LOCAL_DEV ? '本地开发环
|
||||
* 是否启用模拟模式
|
||||
* 该配置控制是否使用模拟数据进行开发测试
|
||||
*/
|
||||
export const isMockMode = false;
|
||||
|
||||
|
||||
/**
|
||||
* API服务类
|
||||
@@ -28,6 +28,11 @@ class ApiService {
|
||||
*/
|
||||
private locationUpdateCallbacks: Set<(location: any) => void> | null;
|
||||
|
||||
/**
|
||||
* 在线用户列表回调函数集合
|
||||
*/
|
||||
private onlineUserListCallbacks: Set<(userList: any) => void> | null;
|
||||
|
||||
/**
|
||||
* 位置更新WebSocket连接
|
||||
*/
|
||||
@@ -38,6 +43,7 @@ class ApiService {
|
||||
*/
|
||||
constructor() {
|
||||
this.locationUpdateCallbacks = null;
|
||||
this.onlineUserListCallbacks = null;
|
||||
this.locationWebSocket = null;
|
||||
}
|
||||
|
||||
@@ -194,6 +200,20 @@ async ServerLogin(code: string) {
|
||||
return await this.request('/user/logout', { method: 'POST' });
|
||||
}
|
||||
|
||||
/**
|
||||
* 签退接口
|
||||
* @param userId 用户ID
|
||||
* @returns 签退结果
|
||||
*/
|
||||
async userSignOut(userId: number): Promise<{ success: boolean; message?: string }> {
|
||||
console.log('API userSignOut调用,参数userId:', userId);
|
||||
|
||||
return await this.request('/user/signout', {
|
||||
method: 'POST',
|
||||
data: { userId },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户签到状态
|
||||
* @returns 用户状态信息
|
||||
@@ -311,6 +331,52 @@ async ServerLogin(code: string) {
|
||||
});
|
||||
}
|
||||
|
||||
// ====== 员工管理接口 ======
|
||||
|
||||
/**
|
||||
* 获取所有员工列表
|
||||
* @returns 员工信息数组
|
||||
*/
|
||||
async getEmployees(): Promise<EmployeeInfo[]> {
|
||||
return await this.request<EmployeeInfo[]>('/employees');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新员工
|
||||
* @param employeeInfo 员工信息
|
||||
* @returns 添加结果
|
||||
*/
|
||||
async addEmployee(employeeInfo: { name: string; phone: string; role: string }): Promise<EmployeeInfo> {
|
||||
return await this.request<EmployeeInfo>('/employees', {
|
||||
method: 'POST',
|
||||
data: employeeInfo,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除员工
|
||||
* @param employeeId 员工ID
|
||||
* @returns 删除结果
|
||||
*/
|
||||
async deleteEmployee(employeeId: number): Promise<{ success: boolean; message?: string }> {
|
||||
return await this.request<{ success: boolean; message?: string }>(`/employees/${employeeId}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新员工信息
|
||||
* @param employeeId 员工ID
|
||||
* @param employeeInfo 员工信息
|
||||
* @returns 更新结果
|
||||
*/
|
||||
async updateEmployee(employeeId: number, employeeInfo: { name?: string; phone?: string; role?: string }): Promise<EmployeeInfo> {
|
||||
return await this.request<EmployeeInfo>(`/employees/${employeeId}`, {
|
||||
method: 'PUT',
|
||||
data: employeeInfo,
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== 仓库相关接口 ====================
|
||||
|
||||
/**
|
||||
@@ -464,15 +530,6 @@ async ServerLogin(code: string) {
|
||||
subscriptionId: string;
|
||||
expiresAt: number;
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
// 模拟数据 - 返回模拟的订阅信息
|
||||
console.log('[MOCK] 订阅实时位置更新,货运人员ID列表:', deliveryPersonIds);
|
||||
return {
|
||||
subscriptionId: `mock-subscription-${Date.now()}`,
|
||||
expiresAt: Date.now() + 3600000 // 1小时后过期
|
||||
};
|
||||
}
|
||||
|
||||
// 真实环境中调用API
|
||||
try {
|
||||
return await this.request<{
|
||||
@@ -493,12 +550,6 @@ async ServerLogin(code: string) {
|
||||
* @param subscriptionId 订阅ID
|
||||
*/
|
||||
async unsubscribeFromRealTimeLocations(subscriptionId: string): Promise<void> {
|
||||
if (isMockMode) {
|
||||
// 模拟数据 - 记录取消订阅操作
|
||||
console.log('[MOCK] 取消订阅实时位置更新,订阅ID:', subscriptionId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 真实环境中调用API
|
||||
try {
|
||||
await this.request<void>('/locations/unsubscribe', {
|
||||
@@ -529,12 +580,31 @@ async ServerLogin(code: string) {
|
||||
this.locationUpdateCallbacks.delete(callback);
|
||||
// 如果没有回调了,可以关闭WebSocket连接
|
||||
if (this.locationUpdateCallbacks.size === 0 && this.locationWebSocket) {
|
||||
this.locationWebSocket.close();
|
||||
this.locationWebSocket = null;
|
||||
this.closeLocationWebSocket();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册在线用户列表回调函数
|
||||
* @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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// ===== 私有属性和方法 =====
|
||||
|
||||
@@ -578,9 +648,21 @@ async ServerLogin(code: string) {
|
||||
});
|
||||
|
||||
// WebSocket连接关闭事件
|
||||
wx.onSocketClose(() => {
|
||||
console.log('WebSocket连接已关闭');
|
||||
wx.onSocketClose((res) => {
|
||||
console.log('WebSocket连接已关闭', res);
|
||||
this.locationWebSocket = null;
|
||||
|
||||
// 如果是非主动关闭,尝试重连
|
||||
if (res.code !== 1000) { // 1000表示正常关闭
|
||||
console.log('WebSocket连接异常关闭,将在3秒后尝试重连');
|
||||
setTimeout(() => {
|
||||
if (!this.locationWebSocket) {
|
||||
this.initLocationWebSocket().catch(err => {
|
||||
console.error('WebSocket重连失败:', err);
|
||||
});
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
});
|
||||
|
||||
// WebSocket错误事件
|
||||
@@ -597,14 +679,28 @@ async ServerLogin(code: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查WebSocket连接是否已建立
|
||||
*/
|
||||
public isWebSocketConnected(): boolean {
|
||||
return this.locationWebSocket !== null && this.locationWebSocket !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭位置更新WebSocket连接
|
||||
*/
|
||||
public async closeLocationWebSocket(): Promise<void> {
|
||||
if (this.locationWebSocket && typeof wx !== 'undefined' && typeof wx.closeSocket === 'function') {
|
||||
wx.closeSocket();
|
||||
this.locationWebSocket = null;
|
||||
console.log('WebSocket连接已关闭');
|
||||
try {
|
||||
// 先检查WebSocket状态,避免在连接过程中关闭
|
||||
wx.closeSocket();
|
||||
this.locationWebSocket = null;
|
||||
console.log('WebSocket连接已关闭');
|
||||
} catch (error) {
|
||||
console.warn('关闭WebSocket连接时出现异常:', error);
|
||||
// 即使关闭失败,也重置连接状态
|
||||
this.locationWebSocket = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,13 +717,53 @@ async ServerLogin(code: string) {
|
||||
switch (message.type) {
|
||||
case 'updateLocation':
|
||||
// 处理位置更新消息
|
||||
this.onLocationUpdate(message);
|
||||
this.triggerLocationUpdateCallbacks(message);
|
||||
break;
|
||||
case 'onlineUserList':
|
||||
// 处理在线用户列表消息
|
||||
this.triggerOnlineUserListCallbacks(message);
|
||||
break;
|
||||
case 'subscribed':
|
||||
// 处理订阅成功响应
|
||||
console.log('WebSocket订阅成功:', message);
|
||||
break;
|
||||
default:
|
||||
console.warn('收到未知类型的WebSocket消息:', message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发位置更新回调
|
||||
* @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 {
|
||||
if (this.onlineUserListCallbacks) {
|
||||
this.onlineUserListCallbacks.forEach(callback => {
|
||||
try {
|
||||
callback(userList);
|
||||
} catch (error) {
|
||||
console.error('在线用户列表回调执行失败:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送WebSocket消息
|
||||
@@ -652,62 +788,35 @@ async ServerLogin(code: string) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅位置更新
|
||||
* @param deliveryPersonId 配送员ID
|
||||
* 订阅位置更新(服务器自动处理,无需发送消息)
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
async subscribeToLocationUpdates(deliveryPersonId: number): Promise<void> {
|
||||
if (isMockMode) {
|
||||
console.log('[MOCK] 订阅位置更新');
|
||||
return;
|
||||
}
|
||||
|
||||
async subscribeToLocationUpdates(): Promise<void> {
|
||||
// 确保WebSocket连接已建立
|
||||
if (!this.locationWebSocket) {
|
||||
this.initLocationWebSocket();
|
||||
}
|
||||
|
||||
// 发送订阅消息
|
||||
const subscribeMessage = {
|
||||
type: 'subscribe',
|
||||
deliveryPersonId: deliveryPersonId,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
this.sendWebSocketMessage(subscribeMessage);
|
||||
// 服务器会自动处理订阅逻辑,无需发送订阅消息
|
||||
console.log('位置订阅已初始化,服务器将自动处理订阅逻辑');
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅位置更新
|
||||
* @param deliveryPersonId 配送员ID
|
||||
* 取消订阅位置更新(服务器自动处理,无需发送消息)
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
async unsubscribeFromLocationUpdates(deliveryPersonId: number): Promise<void> {
|
||||
if (isMockMode) {
|
||||
console.log('[MOCK] 取消订阅位置更新');
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送取消订阅消息
|
||||
const unsubscribeMessage = {
|
||||
type: 'unsubscribe',
|
||||
deliveryPersonId: deliveryPersonId,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
this.sendWebSocketMessage(unsubscribeMessage);
|
||||
async unsubscribeFromLocationUpdates(userId: number): Promise<void> {
|
||||
// 服务器会自动处理取消订阅逻辑,无需发送取消订阅消息
|
||||
console.log('位置取消订阅已初始化,服务器将自动处理取消订阅逻辑');
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送位置更新
|
||||
* @param deliveryPersonId 配送员ID
|
||||
* @param userId 用户ID
|
||||
* @param latitude 纬度
|
||||
* @param longitude 经度
|
||||
*/
|
||||
async sendLocationUpdate(deliveryPersonId: number, latitude: number, longitude: number): Promise<void> {
|
||||
if (isMockMode) {
|
||||
console.log('[MOCK] 发送位置更新');
|
||||
return;
|
||||
}
|
||||
|
||||
async sendLocationUpdate(userId: number, latitude: number, longitude: number): Promise<void> {
|
||||
// 确保WebSocket连接已建立
|
||||
if (!this.locationWebSocket) {
|
||||
this.initLocationWebSocket();
|
||||
@@ -716,7 +825,7 @@ async ServerLogin(code: string) {
|
||||
// 发送位置更新消息
|
||||
const locationMessage = {
|
||||
type: 'updateLocation',
|
||||
deliveryPersonId: deliveryPersonId,
|
||||
userId: userId,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
timestamp: Date.now()
|
||||
@@ -725,6 +834,8 @@ async ServerLogin(code: string) {
|
||||
this.sendWebSocketMessage(locationMessage);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ==================== 订单接口 ====================
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user