注册修改为绑定

This commit is contained in:
2025-10-21 21:51:51 +08:00
parent 5ee4e077fb
commit be2323074b
28 changed files with 729 additions and 211 deletions

View File

@@ -95,8 +95,10 @@ class ApiService {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data);
} else {
console.error(`API Error: HTTP ${res.statusCode}: ${res.errMsg}`);
reject(new Error(`HTTP ${res.statusCode}: ${res.errMsg}`));
// 优先使用服务器返回的具体错误信息,如果没有则使用默认错误信息
const errorMessage = res.data || res.errMsg || '请求失败';
console.error(`API Error: HTTP ${res.statusCode}: ${errorMessage}`);
reject(new Error(`HTTP ${res.statusCode}: ${errorMessage}`));
}
},
fail: (err: any) => {
@@ -163,29 +165,58 @@ async ServerLogin(code: string) {
}
/**
* 注册接口
* @param userInfo 注册用户信息
* @returns 注册结果和员工信息
* 绑定接口
* @param userInfo 绑定用户信息
* @returns 绑定结果和员工信息
*/
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 }
// 服务器返回直接的用户信息格式:{id, name, phone, role, openid}
const response = await this.request<any>('/user/register', {
method: 'POST',
data: userInfo,
});
return {
success: response.success,
employeeInfo: {
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: response.message
};
// 检查响应格式,兼容不同的返回格式
if (response.id) {
// 直接返回用户信息格式
return {
success: true,
employeeInfo: {
id: response.id,
name: response.name || userInfo.name,
phone: response.phone || userInfo.phone,
role: response.role || 'DELIVERY_PERSON'
},
message: '绑定成功'
};
} else if (response.userInfo && response.userInfo.id) {
// 包装格式:{success, userInfo, message}
return {
success: response.success !== false,
employeeInfo: {
id: response.userInfo.id,
name: response.userInfo.name || userInfo.name,
phone: response.userInfo.phone || userInfo.phone,
role: response.userInfo.role || 'DELIVERY_PERSON'
},
message: response.message
};
} else {
// 未知格式,返回错误
console.error('未知的绑定响应格式:', response);
return {
success: false,
employeeInfo: {
id: 0,
name: userInfo.name,
phone: userInfo.phone,
role: 'DELIVERY_PERSON'
},
message: '绑定响应格式错误'
};
}
}
@@ -197,6 +228,25 @@ async ServerLogin(code: string) {
return await this.request('/user/logout', { method: 'POST' });
}
/**
* 解绑微信接口
* 清除当前用户的openid绑定允许重新注册其他账号
* @returns 解绑结果
*/
async unbindWechat(): Promise<{ success: boolean; message?: string }> {
console.log('API unbindWechat调用');
const response = await this.request<any>('/user/unbind', {
method: 'POST',
data: {}
});
return {
success: response.success,
message: response.message
};
}
/**
* 签退接口
* @param userId 用户ID

View File

@@ -77,8 +77,8 @@ class EmployeeService {
}
/**
* 根据手机号查找员工
* @param phone 手机
* 根据号查找员工
* @param phone
* @returns 员工信息或null
*/
async findEmployeeByPhone(phone: string): Promise<EmployeeInfo | null> {
@@ -87,9 +87,9 @@ class EmployeeService {
}
/**
* 验证员工信息(用于注册时检查)
* @param name 姓名
* @param phone 手机
* 验证员工信息(用于绑定时检查)
* @param name 姓名
* @param phone
* @returns 验证结果
*/
async validateEmployee(name: string, phone: string): Promise<{ success: boolean; message?: string; employee?: EmployeeInfo }> {

View File

@@ -423,17 +423,40 @@ class LocationTrackingService {
const formattedUsers = message.users.map((user: any) => {
// 支持多种数据格式locationData字段或直接字段
const locationData = user.locationData || user;
return {
// 验证必需字段是否存在
if (!user.userId && !locationData.userId) {
console.error('❌ 用户数据缺少userId字段:', user);
return null;
}
if (locationData.latitude === undefined || locationData.longitude === undefined) {
console.error('❌ 用户数据缺少位置信息:', user);
return null;
}
// 对于位置更新消息允许缺少name和role字段从本地缓存中获取
const existingUser = this.onlineUsers.get(user.userId || locationData.userId);
const formattedUser = {
userId: user.userId || locationData.userId,
name: user.name || user.userName || `用户${user.userId || locationData.userId}`,
role: user.role || 'employee',
name: user.name || user.userName || (existingUser ? existingUser.name : `用户${user.userId || locationData.userId}`),
role: user.role || (existingUser ? existingUser.role : 'DRIVER'),
userStatus: user.userStatus !== false, // 转换为布尔值
lastUpdateTime: user.lastUpdateTime || locationData.timestamp || Date.now(),
latitude: locationData.latitude,
longitude: locationData.longitude,
timestamp: locationData.timestamp || user.timestamp || Date.now()
};
});
// 验证必需字段
if (!formattedUser.userId) {
console.error('❌ 用户数据缺少userId字段:', formattedUser);
return null;
}
return formattedUser;
}).filter(user => user !== null); // 过滤掉无效数据
console.log('📊 转换后的用户位置数据:', formattedUsers);
@@ -500,12 +523,15 @@ class LocationTrackingService {
const longitude = user.longitude;
const timestamp = user.timestamp || user.lastUpdateTime || Date.now();
// 对于位置更新消息允许缺少name和role字段从本地缓存中获取
const existingUser = this.onlineUsers.get(user.userId);
// 更新或添加用户信息
this.onlineUsers.set(user.userId, {
userId: user.userId,
name: user.name || user.userName || `用户${user.userId}`,
name: user.name || user.userName || (existingUser ? existingUser.name : `用户${user.userId}`),
avatarUrl: '/images/user-avatar.png',
role: user.role || 'employee',
role: user.role || (existingUser ? existingUser.role : 'DRIVER'),
lastLocation: {
userId: user.userId,
longitude: longitude,

View File

@@ -64,26 +64,12 @@ class MapService {
clearTimeout(timeout);
console.error('高德地图SDK定位失败:', err);
// 如果高德SDK失败,尝试使用微信原生API
console.log('尝试使用微信原生getLocation API...');
wx.getLocation({
type: 'gcj02',
success: (wxRes) => {
console.log('微信原生API定位成功:', wxRes);
resolve({
longitude: wxRes.longitude,
latitude: wxRes.latitude
});
},
fail: (wxErr) => {
console.error('微信原生API定位失败:', wxErr);
// 所有方法都失败,使用模拟位置
console.log('所有定位方法失败,使用模拟位置');
resolve({
longitude: 102.7123,
latitude: 25.0409
});
}
// 注释:高德SDK失败后,直接使用模拟位置,不调用微信原生API
// 原因:微信小程序可能没有位置权限,避免权限错误
console.log('高德地图SDK定位失败使用模拟位置');
resolve({
longitude: 102.7123,
latitude: 25.0409
});
}
}, (locationString: string) => {

View File

@@ -113,7 +113,7 @@ class UserService {
}
/**
* 执行静默登录流程:微信登录->个人服务器登录->进入基础界面->签到/注册
* 执行静默登录流程:微信登录->个人服务器登录->进入基础界面->签到/绑定
* @returns 登录结果
*/
async wxLogin(): Promise<{
@@ -293,14 +293,59 @@ class UserService {
}
/**
* 用户注册
* @param registerInfo 注册信息
* @returns 注册结果和员工信息
* 用户绑定
* @param registerInfo 绑定信息
* @returns 绑定结果和员工信息
*/
async register(registerInfo: { name: string; phone: string }): Promise<{ success: boolean; employeeInfo: EmployeeInfo; message?: string }> {
return apiService.userRegister(registerInfo);
}
/**
* 解绑微信
* 清除当前用户的openid绑定允许重新注册其他账号
* @returns 解绑结果
*/
async unbindWechat(): Promise<{ success: boolean; message?: string }> {
try {
console.log('开始解绑微信流程');
// 调用API解绑
const result = await apiService.unbindWechat();
if (result.success) {
console.log('微信解绑成功');
// 清除本地存储的登录信息
wx.removeStorageSync('userInfo');
wx.removeStorageSync('token');
wx.removeStorageSync('openid');
wx.removeStorageSync('session_key');
// 清除全局数据
const app = getApp<any>();
app.globalData.userInfo = null;
app.globalData.token = null;
app.globalData.openid = null;
app.globalData.isLoggedIn = false;
console.log('本地登录信息已清除');
// 解绑成功后跳转到主界面
setTimeout(() => {
wx.reLaunch({
url: '/pages/index/index'
});
}, 500);
}
return result;
} catch (error) {
console.error('解绑微信失败:', error);
return { success: false, message: '解绑微信失败,请重试' };
}
}
/**
* 获取用户权限列表
* @returns 权限列表