// 仓库管理页面 - 处理仓库的增删改查和库存管理 import { showToast } from '../../utils/helpers'; import { WarehouseInfo } from '../../types'; import warehouseService from '../../services/warehouseService'; Page({ data: { // 仓库列表 warehouses: [] as WarehouseInfo[], filteredWarehouses: [] as WarehouseInfo[], // 页面状态 currentTab: 'list', // list: 列表页, add: 添加页 loading: false, // 添加仓库表单数据 addForm: { name: '', address: '', contact: '', phone: '', description: '', capacity: 500, longitude: 102.833722, latitude: 24.880095 }, // 错误信息 errorMessage: '', successMessage: '', // 搜索关键词 searchKeyword: '', // 用户信息 userInfo: null as any }, onLoad() { this.loadWarehouses(); }, onShow() { // 页面显示时刷新数据 this.loadWarehouses(); }, // 加载仓库列表 async loadWarehouses() { this.setData({ loading: true, errorMessage: '' }); try { const warehouses = await warehouseService.getWarehouses(); this.setData({ warehouses, filteredWarehouses: warehouses, 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 }); }, // 搜索仓库 onSearchInput(e: any) { const keyword = e.detail.value.trim(); this.setData({ searchKeyword: keyword }); this.filterWarehouses(keyword); }, // 过滤仓库列表 filterWarehouses(keyword: string) { const { warehouses } = this.data; if (!keyword) { this.setData({ filteredWarehouses: warehouses }); return; } const filtered = warehouses.filter(warehouse => warehouse.name.includes(keyword) || warehouse.address.includes(keyword) || warehouse.contact?.includes(keyword) || warehouse.phone?.includes(keyword) ); this.setData({ filteredWarehouses: filtered }); }, // 表单输入处理 onNameInput(e: any) { this.setData({ 'addForm.name': e.detail.value }); }, onAddressInput(e: any) { this.setData({ 'addForm.address': e.detail.value }); }, onContactInput(e: any) { this.setData({ 'addForm.contact': e.detail.value }); }, onPhoneInput(e: any) { this.setData({ 'addForm.phone': e.detail.value }); }, onDescriptionInput(e: any) { this.setData({ 'addForm.description': e.detail.value }); }, onCapacityInput(e: any) { this.setData({ 'addForm.capacity': parseInt(e.detail.value) || 500 }); }, // 验证表单 validateForm(): boolean { const { name, address, contact, phone } = this.data.addForm; if (!name.trim()) { this.setData({ errorMessage: '请输入仓库名称' }); return false; } if (!address.trim()) { this.setData({ errorMessage: '请输入仓库地址' }); return false; } if (!contact?.trim()) { this.setData({ errorMessage: '请输入联系人' }); return false; } if (!phone?.trim()) { this.setData({ errorMessage: '请输入联系电话' }); return false; } return true; }, // 提交添加仓库表单 async submitAddForm() { this.setData({ errorMessage: '', successMessage: '' }); if (!this.validateForm()) { return; } this.setData({ loading: true }); try { await warehouseService.createWarehouse(this.data.addForm); this.setData({ loading: false, successMessage: '仓库添加成功', currentTab: 'list' }); // 重置表单 this.resetForm(); // 重新加载仓库列表 this.loadWarehouses(); showToast('仓库添加成功'); } catch (error) { console.error('添加仓库失败:', error); this.setData({ loading: false, errorMessage: '添加仓库失败,请重试' }); } }, // 重置表单 resetForm() { this.setData({ addForm: { name: '', address: '', contact: '', phone: '', description: '', capacity: 500, longitude: 102.833722, latitude: 24.880095 } }); }, // 删除仓库 async deleteWarehouse(e: any) { const warehouseId = e.currentTarget.dataset.id; const warehouseName = e.currentTarget.dataset.name; wx.showModal({ title: '确认删除', content: `确定要删除仓库"${warehouseName}"吗?此操作不可恢复。`, confirmText: '删除', confirmColor: '#ff4757', cancelText: '取消', success: async (res) => { if (res.confirm) { try { await warehouseService.deleteWarehouse(warehouseId); showToast('仓库删除成功'); // 重新加载仓库列表 this.loadWarehouses(); } catch (error) { console.error('删除仓库失败:', error); showToast('删除仓库失败'); } } } }); }, // 获取仓库状态文本 getWarehouseStatusText(status: 'open' | 'closed' | 'maintenance' | undefined): string { const statusMap: Record = { 'open': '营业中', 'closed': '已关闭', 'maintenance': '维护中' }; return statusMap[status || 'open'] || '未知状态'; }, // 获取仓库库存状态 getWarehouseInventoryStatus(currentStock: number, capacity: number): string { const percentage = (currentStock / capacity) * 100; if (percentage >= 90) return '库存充足'; if (percentage >= 60) return '库存正常'; if (percentage >= 30) return '库存偏低'; return '库存不足'; }, // 获取库存状态对应的颜色 getInventoryStatusColor(currentStock: number, capacity: number): string { const percentage = (currentStock / capacity) * 100; if (percentage >= 90) return '#52c41a'; // 绿色 if (percentage >= 60) return '#1890ff'; // 蓝色 if (percentage >= 30) return '#faad14'; // 橙色 return '#f5222d'; // 红色 }, // 下拉刷新 onPullDownRefresh() { this.loadWarehouses().finally(() => { wx.stopPullDownRefresh(); }); }, // 清除错误信息 clearError() { this.setData({ errorMessage: '' }); }, // 清除成功信息 clearSuccess() { this.setData({ successMessage: '' }); }, /** * 返回上一页 */ goBack() { wx.navigateBack(); } });