修改位置交互,修改代码逻辑
This commit is contained in:
@@ -1,32 +1,21 @@
|
||||
// 用户服务文件
|
||||
|
||||
// 用户服务 - 处理用户相关的数据操作
|
||||
// 用户服务 - 处理用户认证、会话管理、权限验证等
|
||||
import { UserInfo, EmployeeInfo } from '../types';
|
||||
import { Role } from '../utils/roleUtils';
|
||||
import apiService from './apiService';
|
||||
import { isMockMode } from './apiService';
|
||||
|
||||
/**
|
||||
* 用户服务类
|
||||
* 提供用户认证、信息管理、权限验证等功能
|
||||
*/
|
||||
class UserService {
|
||||
// 模拟用户数据
|
||||
private mockUsers: UserInfo[];
|
||||
|
||||
/**
|
||||
* 构造函数,初始化模拟用户数据
|
||||
* 构造函数
|
||||
*/
|
||||
constructor() {
|
||||
this.mockUsers = [
|
||||
{
|
||||
id: 1,
|
||||
role: 'ADMIN'
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
role: 'DELIVERY_PERSON'
|
||||
}
|
||||
];
|
||||
// 不再使用模拟数据
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,14 +23,6 @@ class UserService {
|
||||
* @returns 用户信息
|
||||
*/
|
||||
async getUserInfo(): Promise<UserInfo> {
|
||||
if (isMockMode) {
|
||||
// 模拟数据 - 返回管理员信息
|
||||
console.log('[MOCK] 获取用户信息');
|
||||
return {
|
||||
id: 1,
|
||||
role: 'ADMIN'
|
||||
};
|
||||
}
|
||||
return apiService.getUserInfo();
|
||||
}
|
||||
|
||||
@@ -49,11 +30,6 @@ class UserService {
|
||||
* 用户退出登录
|
||||
*/
|
||||
async logout(): Promise<void> {
|
||||
if (isMockMode) {
|
||||
// 模拟登出
|
||||
console.log('[MOCK] 用户登出');
|
||||
return;
|
||||
}
|
||||
return apiService.logout();
|
||||
}
|
||||
|
||||
@@ -80,7 +56,7 @@ class UserService {
|
||||
* @returns 是否为管理员
|
||||
*/
|
||||
isAdmin(): boolean {
|
||||
return this.getUserRole() === 'ADMIN';
|
||||
return this.getUserRole() === Role.ADMIN;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +64,7 @@ class UserService {
|
||||
* @returns 是否为货运人员
|
||||
*/
|
||||
isDeliveryPerson(): boolean {
|
||||
return this.getUserRole() === 'DELIVERY_PERSON';
|
||||
return this.getUserRole() === Role.DELIVERY_PERSON;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,25 +170,6 @@ class UserService {
|
||||
*/
|
||||
async ServerLogin(code: string): Promise<{ success: boolean; openid?: string; token?: string; userInfo?: UserInfo }> {
|
||||
try {
|
||||
if (isMockMode) {
|
||||
// 模拟登录成功
|
||||
console.log('[MOCK] 微信小程序登录');
|
||||
|
||||
// 使用模拟用户数据
|
||||
const userInfo = this.mockUsers[0];
|
||||
const openid = 'mock-openid-' + Math.random().toString(36).substring(2);
|
||||
const token = 'mock-token-' + Date.now();
|
||||
const session_key = 'mock-session-key-' + Math.random().toString(36).substring(2);
|
||||
|
||||
// 保存到本地存储
|
||||
wx.setStorageSync('userInfo', userInfo);
|
||||
wx.setStorageSync('token', token);
|
||||
wx.setStorageSync('openid', openid);
|
||||
wx.setStorageSync('session_key', session_key);
|
||||
|
||||
return { success: true, openid, token, userInfo };
|
||||
}
|
||||
|
||||
// 真实API模式
|
||||
//TODO: 登录成功的基础数据:服务器下发的公共游客可看的数据
|
||||
const result = await apiService.ServerLogin(code);
|
||||
@@ -268,14 +225,6 @@ class UserService {
|
||||
* @returns 更新后的用户信息
|
||||
*/
|
||||
async updateUserInfo(userInfo: Partial<UserInfo>): Promise<UserInfo> {
|
||||
if (isMockMode) {
|
||||
// 模拟更新成功
|
||||
console.log('[MOCK] 更新用户信息');
|
||||
return {
|
||||
id: 1,
|
||||
role: userInfo.role || 'GUEST'
|
||||
};
|
||||
}
|
||||
return apiService.updateUserInfo(userInfo);
|
||||
}
|
||||
|
||||
@@ -284,14 +233,6 @@ class UserService {
|
||||
* @returns 用户状态信息
|
||||
*/
|
||||
async getUserStatus(): Promise<{ status: 'signed_in' | 'signed_out' | 'registered' | 'unregistered'; lastSignInTime?: string; lastSignOutTime?: string }> {
|
||||
if (isMockMode) {
|
||||
// 模拟数据 - 默认返回已注册状态
|
||||
console.log('[MOCK] 获取用户状态');
|
||||
return {
|
||||
status: 'registered'
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// 调用服务器接口获取用户状态
|
||||
const response = await apiService.getUserStatus();
|
||||
@@ -328,24 +269,26 @@ class UserService {
|
||||
throw new Error('用户认证信息缺失,请重新登录');
|
||||
}
|
||||
|
||||
if (isMockMode) {
|
||||
// 模拟签到成功,返回员工信息
|
||||
console.log('[MOCK] 用户签到成功');
|
||||
const employeeInfo: EmployeeInfo = {
|
||||
id: userInfo.id,
|
||||
name: '测试用户',
|
||||
phone: '13800138000',
|
||||
role: userInfo.role || 'DELIVERY_PERSON'
|
||||
};
|
||||
|
||||
return {
|
||||
success: true,
|
||||
employeeInfo: employeeInfo,
|
||||
message: '签到成功'
|
||||
};
|
||||
return apiService.userSignIn(userInfo.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户签退
|
||||
* @returns 签退结果
|
||||
*/
|
||||
async signOut(): Promise<{ success: boolean; message?: string }> {
|
||||
const userInfo = this.getGlobalUserInfo();
|
||||
if (!userInfo || !userInfo.id) {
|
||||
throw new Error('用户未登录,无法签退');
|
||||
}
|
||||
|
||||
// 检查是否有有效的token(防止后端空指针异常)
|
||||
const app = getApp<any>();
|
||||
if (!app.globalData.token) {
|
||||
throw new Error('用户认证信息缺失,请重新登录');
|
||||
}
|
||||
|
||||
return apiService.userSignIn(userInfo.id);
|
||||
return apiService.userSignOut(userInfo.id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,29 +297,6 @@ class UserService {
|
||||
* @returns 注册结果和员工信息
|
||||
*/
|
||||
async register(registerInfo: { name: string; phone: string }): Promise<{ success: boolean; employeeInfo: EmployeeInfo; message?: string }> {
|
||||
if (isMockMode) {
|
||||
// 模拟注册成功
|
||||
console.log('[MOCK] 用户注册成功');
|
||||
// 确保所有必需字段都有值
|
||||
const employeeInfo: EmployeeInfo = {
|
||||
id: Date.now(),
|
||||
name: registerInfo.name,
|
||||
phone: registerInfo.phone,
|
||||
role: 'DELIVERY_PERSON'
|
||||
};
|
||||
this.mockUsers.push({
|
||||
id: employeeInfo.id,
|
||||
role: employeeInfo.role || 'DELIVERY_PERSON',
|
||||
name: employeeInfo.name,
|
||||
phone: employeeInfo.phone
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
employeeInfo: employeeInfo,
|
||||
message: '注册成功'
|
||||
};
|
||||
}
|
||||
|
||||
return apiService.userRegister(registerInfo);
|
||||
}
|
||||
|
||||
@@ -385,20 +305,6 @@ class UserService {
|
||||
* @returns 权限列表
|
||||
*/
|
||||
async getUserPermissions(): Promise<string[]> {
|
||||
if (isMockMode) {
|
||||
// 模拟权限列表
|
||||
console.log('[MOCK] 获取用户权限列表');
|
||||
return [
|
||||
'order:view',
|
||||
'order:edit',
|
||||
'delivery:view',
|
||||
'delivery:edit',
|
||||
'warehouse:view',
|
||||
'warehouse:edit',
|
||||
'user:view',
|
||||
'user:edit'
|
||||
];
|
||||
}
|
||||
return apiService.getUserPermissions();
|
||||
}
|
||||
|
||||
@@ -407,11 +313,6 @@ class UserService {
|
||||
* @returns 是否在线
|
||||
*/
|
||||
async checkUserOnline(): Promise<boolean> {
|
||||
if (isMockMode) {
|
||||
// 模拟用户在线
|
||||
console.log('[MOCK] 检查用户在线状态');
|
||||
return true;
|
||||
}
|
||||
return apiService.checkUserOnline();
|
||||
}
|
||||
|
||||
@@ -425,25 +326,6 @@ class UserService {
|
||||
expiresAt: number;
|
||||
permissions: string[];
|
||||
}> {
|
||||
if (isMockMode) {
|
||||
// 模拟会话信息
|
||||
console.log('[MOCK] 获取用户会话信息');
|
||||
return {
|
||||
userId: 1,
|
||||
sessionId: 'mock-session-' + Date.now(),
|
||||
expiresAt: Date.now() + 86400000, // 24小时后过期
|
||||
permissions: [
|
||||
'order:view',
|
||||
'order:edit',
|
||||
'delivery:view',
|
||||
'delivery:edit',
|
||||
'warehouse:view',
|
||||
'warehouse:edit',
|
||||
'user:view',
|
||||
'user:edit'
|
||||
]
|
||||
};
|
||||
}
|
||||
return apiService.getSessionInfo();
|
||||
}
|
||||
|
||||
@@ -453,14 +335,6 @@ class UserService {
|
||||
* @returns 新令牌及过期时间
|
||||
*/
|
||||
async refreshToken(oldToken: string): Promise<{ token: string; expiresAt: number }> {
|
||||
if (isMockMode) {
|
||||
// 模拟令牌刷新
|
||||
console.log('[MOCK] 刷新用户令牌');
|
||||
return {
|
||||
token: 'mock-refreshed-token-' + Date.now(),
|
||||
expiresAt: Date.now() + 86400000 // 24小时后过期
|
||||
};
|
||||
}
|
||||
return apiService.refreshToken(oldToken);
|
||||
}
|
||||
|
||||
@@ -470,11 +344,6 @@ class UserService {
|
||||
* @returns 是否拥有该权限
|
||||
*/
|
||||
async verifyPermission(permission: string): Promise<boolean> {
|
||||
if (isMockMode) {
|
||||
// 模拟权限验证(管理员拥有所有权限)
|
||||
console.log('[MOCK] 验证用户权限:', permission);
|
||||
return true;
|
||||
}
|
||||
return apiService.verifyPermission(permission);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user