362 lines
7.8 KiB
TypeScript
362 lines
7.8 KiB
TypeScript
|
// 员工管理页面 - 集成到管理员界面
|
|||
|
import { EmployeeInfo } from '../../types';
|
|||
|
import employeeService from '../../services/employeeService';
|
|||
|
import { Role, getRoleOptions } from '../../utils/roleUtils';
|
|||
|
|
|||
|
Page({
|
|||
|
data: {
|
|||
|
// 员工列表
|
|||
|
employees: [] as EmployeeInfo[],
|
|||
|
filteredEmployees: [] as EmployeeInfo[],
|
|||
|
|
|||
|
// 页面状态
|
|||
|
currentTab: 'list', // list: 列表页, add: 添加页
|
|||
|
loading: false,
|
|||
|
|
|||
|
// 添加员工表单数据
|
|||
|
addForm: {
|
|||
|
name: '',
|
|||
|
phone: '',
|
|||
|
role: Role.DELIVERY_PERSON
|
|||
|
},
|
|||
|
|
|||
|
// 错误信息
|
|||
|
errorMessage: '',
|
|||
|
successMessage: '',
|
|||
|
|
|||
|
// 搜索关键词
|
|||
|
searchKeyword: '',
|
|||
|
|
|||
|
// 角色选项
|
|||
|
roleOptions: getRoleOptions(),
|
|||
|
|
|||
|
// 用户信息
|
|||
|
userInfo: null as any
|
|||
|
},
|
|||
|
|
|||
|
onLoad() {
|
|||
|
this.getUserInfo();
|
|||
|
this.loadEmployees();
|
|||
|
},
|
|||
|
|
|||
|
onShow() {
|
|||
|
this.loadEmployees();
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 获取用户信息
|
|||
|
*/
|
|||
|
getUserInfo() {
|
|||
|
const app = getApp<any>();
|
|||
|
if (app.globalData.userInfo) {
|
|||
|
const userInfo = app.globalData.userInfo;
|
|||
|
this.setData({ userInfo });
|
|||
|
|
|||
|
// 验证是否为管理员
|
|||
|
if (userInfo.role !== 'ADMIN') {
|
|||
|
wx.showToast({
|
|||
|
title: '无权限访问',
|
|||
|
icon: 'none'
|
|||
|
});
|
|||
|
wx.navigateBack();
|
|||
|
}
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 加载员工列表
|
|||
|
*/
|
|||
|
async loadEmployees() {
|
|||
|
this.setData({
|
|||
|
loading: true,
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
|
|||
|
try {
|
|||
|
const employees = await employeeService.getEmployees();
|
|||
|
// 获取过滤后的员工列表
|
|||
|
const filteredEmployees = this.getFilteredEmployees(employees);
|
|||
|
|
|||
|
this.setData({
|
|||
|
employees,
|
|||
|
filteredEmployees,
|
|||
|
loading: false
|
|||
|
});
|
|||
|
} catch (error) {
|
|||
|
console.error('加载员工列表失败:', error);
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
errorMessage: '加载员工列表失败,请稍后重试'
|
|||
|
});
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 切换页面标签
|
|||
|
*/
|
|||
|
switchTab(e: any) {
|
|||
|
const tab = e.currentTarget.dataset.tab;
|
|||
|
this.setData({
|
|||
|
currentTab: tab,
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
|
|||
|
if (tab === 'list') {
|
|||
|
this.loadEmployees();
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 处理添加员工表单输入
|
|||
|
*/
|
|||
|
onFormInput(e: any) {
|
|||
|
const { field } = e.currentTarget.dataset;
|
|||
|
const value = e.detail.value;
|
|||
|
|
|||
|
this.setData({
|
|||
|
[`addForm.${field}`]: value,
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 处理角色选择
|
|||
|
*/
|
|||
|
onRoleChange(e: any) {
|
|||
|
const index = e.detail.value;
|
|||
|
const selectedRole = this.data.roleOptions[index].value;
|
|||
|
this.setData({
|
|||
|
'addForm.role': selectedRole
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 验证表单数据
|
|||
|
*/
|
|||
|
validateForm(): boolean {
|
|||
|
const { name, phone, role } = this.data.addForm;
|
|||
|
|
|||
|
if (!name.trim()) {
|
|||
|
this.setData({
|
|||
|
errorMessage: '请输入员工姓名'
|
|||
|
});
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (!phone.trim()) {
|
|||
|
this.setData({
|
|||
|
errorMessage: '请输入手机号'
|
|||
|
});
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// 简单的手机号格式验证
|
|||
|
const phoneRegex = /^1[3-9]\d{9}$/;
|
|||
|
if (!phoneRegex.test(phone)) {
|
|||
|
this.setData({
|
|||
|
errorMessage: '请输入正确的手机号格式'
|
|||
|
});
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (!role) {
|
|||
|
this.setData({
|
|||
|
errorMessage: '请选择员工角色'
|
|||
|
});
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 提交添加员工表单
|
|||
|
*/
|
|||
|
async submitAddForm() {
|
|||
|
if (!this.validateForm()) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
this.setData({
|
|||
|
loading: true,
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
|
|||
|
try {
|
|||
|
await employeeService.addEmployee(this.data.addForm);
|
|||
|
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
successMessage: '员工添加成功',
|
|||
|
addForm: {
|
|||
|
name: '',
|
|||
|
phone: '',
|
|||
|
role: Role.DELIVERY_PERSON
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
// 添加成功后自动切换到列表页
|
|||
|
setTimeout(() => {
|
|||
|
this.setData({
|
|||
|
currentTab: 'list'
|
|||
|
});
|
|||
|
this.loadEmployees();
|
|||
|
}, 1500);
|
|||
|
} catch (error) {
|
|||
|
console.error('添加员工失败:', error);
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
errorMessage: (error as Error).message || '添加员工失败,请稍后重试'
|
|||
|
});
|
|||
|
}
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 删除员工
|
|||
|
*/
|
|||
|
async deleteEmployee(e: any) {
|
|||
|
const employeeId = e.currentTarget.dataset.id;
|
|||
|
const employee = this.data.employees.find(emp => emp.id === employeeId);
|
|||
|
|
|||
|
if (!employee) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
wx.showModal({
|
|||
|
title: '确认删除',
|
|||
|
content: `确定要删除员工 ${employee.name} (${employee.phone}) 吗?此操作不可恢复。`,
|
|||
|
confirmText: '删除',
|
|||
|
confirmColor: '#ff4d4f',
|
|||
|
cancelText: '取消',
|
|||
|
success: async (res) => {
|
|||
|
if (res.confirm) {
|
|||
|
this.setData({
|
|||
|
loading: true,
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
|
|||
|
try {
|
|||
|
const result = await employeeService.deleteEmployee(employeeId);
|
|||
|
|
|||
|
if (result.success) {
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
successMessage: result.message || '员工删除成功'
|
|||
|
});
|
|||
|
|
|||
|
// 重新加载员工列表
|
|||
|
this.loadEmployees();
|
|||
|
} else {
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
errorMessage: result.message || '删除员工失败'
|
|||
|
});
|
|||
|
}
|
|||
|
} catch (error) {
|
|||
|
console.error('删除员工失败:', error);
|
|||
|
this.setData({
|
|||
|
loading: false,
|
|||
|
errorMessage: '删除员工失败,请稍后重试'
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 搜索员工
|
|||
|
*/
|
|||
|
onSearchInput(e: any) {
|
|||
|
const keyword = e.detail.value;
|
|||
|
this.setData({
|
|||
|
searchKeyword: keyword
|
|||
|
});
|
|||
|
|
|||
|
// 更新过滤后的员工列表
|
|||
|
this.updateFilteredEmployees();
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 更新过滤后的员工列表
|
|||
|
*/
|
|||
|
updateFilteredEmployees() {
|
|||
|
const { employees } = this.data;
|
|||
|
const filteredEmployees = this.getFilteredEmployees(employees);
|
|||
|
this.setData({
|
|||
|
filteredEmployees
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 获取过滤后的员工列表
|
|||
|
*/
|
|||
|
getFilteredEmployees(employees?: EmployeeInfo[]): EmployeeInfo[] {
|
|||
|
const { searchKeyword } = this.data;
|
|||
|
|
|||
|
// 如果employees为空,返回空数组
|
|||
|
if (!employees || !Array.isArray(employees)) {
|
|||
|
return [];
|
|||
|
}
|
|||
|
|
|||
|
// 获取当前登录用户信息
|
|||
|
const app = getApp();
|
|||
|
const currentUser = app.globalData.userInfo;
|
|||
|
|
|||
|
// 过滤掉当前登录用户
|
|||
|
let filteredEmployees = employees.filter(emp => {
|
|||
|
// 如果没有当前用户信息,显示所有员工
|
|||
|
if (!currentUser || !currentUser.id) {
|
|||
|
return true;
|
|||
|
}
|
|||
|
// 过滤掉当前用户
|
|||
|
return emp.id !== currentUser.id;
|
|||
|
});
|
|||
|
|
|||
|
// 更严格的搜索关键词检查
|
|||
|
if (!searchKeyword || typeof searchKeyword !== 'string' || !searchKeyword.trim()) {
|
|||
|
return filteredEmployees;
|
|||
|
}
|
|||
|
|
|||
|
const keyword = searchKeyword.toLowerCase();
|
|||
|
return filteredEmployees.filter(emp =>
|
|||
|
emp.name.toLowerCase().includes(keyword) ||
|
|||
|
emp.phone.includes(keyword) ||
|
|||
|
(emp.role || '').toLowerCase().includes(keyword)
|
|||
|
);
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 获取角色显示文本
|
|||
|
*/
|
|||
|
getRoleText(role: string): string {
|
|||
|
const roleMap: Record<string, string> = {
|
|||
|
'ADMIN': '管理员',
|
|||
|
'DELIVERY_PERSON': '配送员',
|
|||
|
'EMPLOYEE': '员工'
|
|||
|
};
|
|||
|
return roleMap[role] || role;
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 清空消息
|
|||
|
*/
|
|||
|
clearMessages() {
|
|||
|
this.setData({
|
|||
|
errorMessage: '',
|
|||
|
successMessage: ''
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
/**
|
|||
|
* 返回上一页
|
|||
|
*/
|
|||
|
goBack() {
|
|||
|
wx.navigateBack();
|
|||
|
}
|
|||
|
});
|