Files
WXProgram/miniprogram/services/employeeService.ts

118 lines
3.1 KiB
TypeScript
Raw Normal View History

// 员工管理服务 - 处理员工相关的数据操作
import { EmployeeInfo } from '../types';
import apiService from './apiService';
/**
*
*
*/
class EmployeeService {
/**
*
*/
constructor() {
// 不再使用模拟数据
}
/**
*
* @returns
*/
async getEmployees(): Promise<EmployeeInfo[]> {
try {
return await apiService.getEmployees();
} catch (error) {
console.error('获取员工列表失败:', error);
// API调用失败时返回空数组
return [];
}
}
/**
*
* @param employeeInfo
* @returns
*/
async addEmployee(employeeInfo: { name: string; phone: string; role: string }): Promise<EmployeeInfo> {
try {
return await apiService.addEmployee(employeeInfo);
} catch (error) {
console.error('添加员工失败:', error);
throw new Error('添加员工失败,请稍后重试');
}
}
/**
*
* @param employeeId ID
* @returns
*/
async deleteEmployee(employeeId: number): Promise<{ success: boolean; message?: string }> {
try {
return await apiService.deleteEmployee(employeeId);
} catch (error) {
console.error('删除员工失败:', error);
return {
success: false,
message: '删除员工失败,请稍后重试'
};
}
}
/**
*
* @param employeeId ID
* @param employeeInfo
* @returns
*/
async updateEmployee(employeeId: number, employeeInfo: { name?: string; phone?: string; role?: string }): Promise<EmployeeInfo> {
try {
return await apiService.updateEmployee(employeeId, employeeInfo);
} catch (error) {
console.error('更新员工信息失败:', error);
throw new Error('更新员工信息失败,请稍后重试');
}
}
/**
*
* @param phone
* @returns null
*/
async findEmployeeByPhone(phone: string): Promise<EmployeeInfo | null> {
const employees = await this.getEmployees();
return employees.find(emp => emp.phone === phone) || null;
}
/**
*
* @param name
* @param phone
* @returns
*/
async validateEmployee(name: string, phone: string): Promise<{ success: boolean; message?: string; employee?: EmployeeInfo }> {
const employees = await this.getEmployees();
const employee = employees.find(emp => emp.name === name && emp.phone === phone);
if (employee) {
return {
success: true,
message: '员工信息验证成功',
employee
};
} else {
return {
success: false,
message: '员工信息不存在,请联系管理员添加'
};
}
}
}
/**
*
* 使
*/
export default new EmployeeService();