地址路径修改

This commit is contained in:
2025-10-26 13:15:04 +08:00
parent be2323074b
commit 271b88232c
77 changed files with 13254 additions and 228 deletions

View File

@@ -2,6 +2,8 @@
import { EmployeeInfo } from '../../types';
import employeeService from '../../services/employeeService';
import { Role, getRoleOptions } from '../../utils/roleUtils';
import { avatarCache } from '../../utils/avatarCache';
import { API_BASE_URL } from '../../services/apiService';
Page({
data: {
@@ -10,7 +12,7 @@ Page({
filteredEmployees: [] as EmployeeInfo[],
// 页面状态
currentTab: 'list', // list: 列表页, add: 添加页
currentTab: 'list', // list: 列表页, add: 添加页, edit: 编辑页
loading: false,
// 添加员工表单数据
@@ -20,6 +22,15 @@ Page({
role: Role.DELIVERY_PERSON
},
// 编辑员工表单数据
editForm: {
id: 0,
name: '',
phone: '',
role: Role.DELIVERY_PERSON,
avatarUrl: ''
},
// 错误信息
errorMessage: '',
successMessage: '',
@@ -31,7 +42,10 @@ Page({
roleOptions: getRoleOptions(),
// 用户信息
userInfo: null as any
userInfo: null as any,
// 临时头像路径(用于头像上传)
tempAvatarPath: ''
},
onLoad() {
@@ -40,7 +54,10 @@ Page({
},
onShow() {
this.loadEmployees();
// 只在页面显示时检查是否需要刷新数据,避免频繁请求
if (this.data.employees.length === 0) {
this.loadEmployees();
}
},
/**
@@ -63,6 +80,45 @@ Page({
}
},
/**
* 获取完整的头像URL
* @param avatarPath 头像相对路径
* @returns 完整的头像URL
*/
getFullAvatarUrl(avatarPath: string | undefined): string {
if (!avatarPath) {
return '/images/user-avatar.png';
}
// 如果已经是完整URL直接返回
if (avatarPath.startsWith('http')) {
return avatarPath;
}
// 将相对路径转换为完整URL
return `${API_BASE_URL}${avatarPath}`;
},
/**
* 获取本地缓存的头像路径
*/
async getLocalAvatarPath(avatarPath: string | undefined): Promise<string> {
if (!avatarPath) {
return '/images/user-avatar.png';
}
try {
// 使用头像缓存获取本地文件路径
const fullAvatarUrl = this.getFullAvatarUrl(avatarPath);
const localAvatarPath = await avatarCache.getAvatarPath(fullAvatarUrl);
return localAvatarPath;
} catch (error) {
console.error('获取头像缓存失败:', error);
// 如果缓存失败回退到远程URL
return this.getFullAvatarUrl(avatarPath);
}
},
/**
* 加载员工列表
*/
@@ -75,11 +131,24 @@ Page({
try {
const employees = await employeeService.getEmployees();
// 为每个员工获取本地缓存的头像路径
const employeesWithAvatars = await Promise.all(
employees.map(async (employee) => {
const localAvatarPath = await this.getLocalAvatarPath(employee.avatarPath);
return {
...employee,
fullAvatarUrl: localAvatarPath
};
})
);
// 获取过滤后的员工列表
const filteredEmployees = this.getFilteredEmployees(employees);
const filteredEmployees = this.getFilteredEmployees(employeesWithAvatars);
this.setData({
employees,
employees: employeesWithAvatars,
filteredEmployees,
loading: false
});
@@ -103,7 +172,8 @@ Page({
successMessage: ''
});
if (tab === 'list') {
// 只在切换到列表页且没有数据时才加载,避免频繁请求
if (tab === 'list' && this.data.employees.length === 0) {
this.loadEmployees();
}
},
@@ -215,6 +285,220 @@ Page({
}
},
/**
* 编辑员工
*/
editEmployee(e: any) {
const employeeId = e.currentTarget.dataset.id;
const employee = this.data.employees.find(emp => emp.id === employeeId);
if (employee) {
this.setData({
editForm: {
id: employee.id || 0,
name: employee.name || '',
phone: employee.phone || '',
role: employee.role || Role.DELIVERY_PERSON,
avatarUrl: ''
},
currentTab: 'edit'
});
}
},
/**
* 提交编辑员工表单
*/
async submitEditForm() {
if (!this.validateEditForm()) {
return;
}
this.setData({
loading: true,
errorMessage: '',
successMessage: ''
});
try {
// 调用更新员工的API方法
await employeeService.updateEmployee(this.data.editForm.id, {
name: this.data.editForm.name,
phone: this.data.editForm.phone,
role: this.data.editForm.role
});
// 如果有新头像,上传头像
if (this.data.tempAvatarPath) {
try {
const result = await employeeService.uploadAvatar(this.data.editForm.id, this.data.tempAvatarPath);
if (result.success && result.avatarUrl) {
// 更新头像URL
this.setData({
'editForm.avatarUrl': result.avatarUrl
});
}
} catch (avatarError) {
console.error('上传头像失败:', avatarError);
wx.showToast({
title: '头像上传失败',
icon: 'error'
});
}
}
this.setData({
loading: false,
successMessage: '员工更新成功',
currentTab: 'list',
tempAvatarPath: ''
});
// 重新加载员工列表
this.loadEmployees();
} catch (error) {
console.error('更新员工失败:', error);
this.setData({
loading: false,
errorMessage: '更新员工失败,请重试'
});
}
},
/**
* 选择头像
*/
chooseAvatar() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album'],
maxDuration: 30,
camera: 'back',
success: (res) => {
if (res.tempFiles && res.tempFiles.length > 0) {
const tempFilePath = res.tempFiles[0].tempFilePath;
this.setData({
'editForm.avatarUrl': tempFilePath,
tempAvatarPath: tempFilePath
});
}
},
fail: (err) => {
console.error('选择头像失败:', err);
wx.showToast({
title: '选择头像失败',
icon: 'error'
});
}
});
},
/**
* 拍照
*/
takePhoto() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
if (res.tempFiles && res.tempFiles.length > 0) {
const tempFilePath = res.tempFiles[0].tempFilePath;
this.setData({
'editForm.avatarUrl': tempFilePath,
tempAvatarPath: tempFilePath
});
}
},
fail: (err) => {
console.error('拍照失败:', err);
wx.showToast({
title: '拍照失败',
icon: 'error'
});
}
});
},
/**
* 验证编辑表单
*/
validateEditForm(): boolean {
const { name, phone, role } = this.data.editForm;
if (!name.trim()) {
this.setData({
errorMessage: '请输入员工姓名'
});
return false;
}
if (!phone.trim()) {
this.setData({
errorMessage: '请输入员工工号'
});
return false;
}
// 简单的工号格式验证
const phoneRegex = /^\d+$/;
if (!phoneRegex.test(phone)) {
this.setData({
errorMessage: '工号只能包含数字'
});
return false;
}
if (!role) {
this.setData({
errorMessage: '请选择员工角色'
});
return false;
}
return true;
},
/**
* 取消编辑
*/
cancelEdit() {
this.setData({
currentTab: 'list',
errorMessage: '',
successMessage: ''
});
},
/**
* 编辑表单输入处理
*/
onEditFormInput(e: any) {
const { field } = e.currentTarget.dataset;
const value = e.detail.value;
this.setData({
[`editForm.${field}`]: value,
errorMessage: '',
successMessage: ''
});
},
/**
* 处理编辑角色选择
*/
onEditRoleChange(e: any) {
const index = e.detail.value;
const selectedRole = this.data.roleOptions[index].value;
this.setData({
'editForm.role': selectedRole
});
},
/**
* 删除员工
*/
@@ -304,19 +588,8 @@ Page({
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;
});
// 显示所有员工,包括当前用户
let filteredEmployees = employees;
// 更严格的搜索关键词检查
if (!searchKeyword || typeof searchKeyword !== 'string' || !searchKeyword.trim()) {