481 lines
14 KiB
JavaScript
481 lines
14 KiB
JavaScript
|
|
"use strict";
|
||
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
|
};
|
||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
|
// 仓库管理页面 - 处理仓库的增删改查和库存管理
|
||
|
|
const helpers_1 = require("../../utils/helpers");
|
||
|
|
const warehouseService_1 = __importDefault(require("../../services/warehouseService"));
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
// 仓库列表
|
||
|
|
warehouses: [],
|
||
|
|
filteredWarehouses: [],
|
||
|
|
// 页面状态
|
||
|
|
currentTab: 'list', // list: 列表页, add: 添加页, edit: 编辑页
|
||
|
|
loading: false,
|
||
|
|
// 添加仓库表单数据
|
||
|
|
addForm: {
|
||
|
|
name: '',
|
||
|
|
address: '',
|
||
|
|
contact: '',
|
||
|
|
phone: '',
|
||
|
|
description: '',
|
||
|
|
capacity: 500,
|
||
|
|
longitude: 102.833722,
|
||
|
|
latitude: 24.880095
|
||
|
|
},
|
||
|
|
// 编辑仓库表单数据
|
||
|
|
editForm: {
|
||
|
|
id: 0,
|
||
|
|
name: '',
|
||
|
|
address: '',
|
||
|
|
contact: '',
|
||
|
|
phone: '',
|
||
|
|
description: '',
|
||
|
|
capacity: 500,
|
||
|
|
longitude: 102.833722,
|
||
|
|
latitude: 24.880095,
|
||
|
|
status: 'open'
|
||
|
|
},
|
||
|
|
// 错误信息
|
||
|
|
errorMessage: '',
|
||
|
|
successMessage: '',
|
||
|
|
// 搜索关键词
|
||
|
|
searchKeyword: '',
|
||
|
|
// 用户信息
|
||
|
|
userInfo: null
|
||
|
|
},
|
||
|
|
onLoad() {
|
||
|
|
this.loadWarehouses();
|
||
|
|
},
|
||
|
|
onShow() {
|
||
|
|
// 只在页面显示时检查是否需要刷新数据,避免频繁请求
|
||
|
|
if (this.data.warehouses.length === 0) {
|
||
|
|
this.loadWarehouses();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 加载仓库列表
|
||
|
|
async loadWarehouses() {
|
||
|
|
this.setData({
|
||
|
|
loading: true,
|
||
|
|
errorMessage: ''
|
||
|
|
});
|
||
|
|
try {
|
||
|
|
const warehouses = await warehouseService_1.default.getWarehouses();
|
||
|
|
this.setData({
|
||
|
|
warehouses,
|
||
|
|
filteredWarehouses: warehouses,
|
||
|
|
loading: false
|
||
|
|
});
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
console.error('加载仓库列表失败:', error);
|
||
|
|
this.setData({
|
||
|
|
loading: false,
|
||
|
|
errorMessage: '加载仓库列表失败,请重试'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 切换标签页
|
||
|
|
switchTab(e) {
|
||
|
|
const tab = e.currentTarget.dataset.tab;
|
||
|
|
this.setData({
|
||
|
|
currentTab: tab
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 搜索仓库
|
||
|
|
onSearchInput(e) {
|
||
|
|
const keyword = e.detail.value.trim();
|
||
|
|
this.setData({
|
||
|
|
searchKeyword: keyword
|
||
|
|
});
|
||
|
|
this.filterWarehouses(keyword);
|
||
|
|
},
|
||
|
|
// 过滤仓库列表
|
||
|
|
filterWarehouses(keyword) {
|
||
|
|
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) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.name': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onAddressInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.address': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onContactInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.contact': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onPhoneInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.phone': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onDescriptionInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.description': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onCapacityInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'addForm.capacity': parseInt(e.detail.value) || 500
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 经度输入处理
|
||
|
|
onLongitudeInput(e) {
|
||
|
|
const value = parseFloat(e.detail.value) || 0;
|
||
|
|
this.setData({
|
||
|
|
'addForm.longitude': value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 纬度输入处理
|
||
|
|
onLatitudeInput(e) {
|
||
|
|
const value = parseFloat(e.detail.value) || 0;
|
||
|
|
this.setData({
|
||
|
|
'addForm.latitude': value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 地图选点功能
|
||
|
|
pickLocationFromMap() {
|
||
|
|
console.log('开始地图选点...');
|
||
|
|
wx.chooseLocation({
|
||
|
|
success: (res) => {
|
||
|
|
console.log('地图选点成功:', res);
|
||
|
|
if (res.longitude && res.latitude) {
|
||
|
|
console.log('更新表单数据 - 经度:', res.longitude, '纬度:', res.latitude);
|
||
|
|
this.setData({
|
||
|
|
'addForm.longitude': res.longitude,
|
||
|
|
'addForm.latitude': res.latitude
|
||
|
|
});
|
||
|
|
console.log('表单数据更新后:', this.data.addForm);
|
||
|
|
wx.showToast({
|
||
|
|
title: '位置已选择',
|
||
|
|
icon: 'success'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail: (error) => {
|
||
|
|
console.error('地图选点失败:', error);
|
||
|
|
wx.showToast({
|
||
|
|
title: '选点失败,请重试',
|
||
|
|
icon: 'none'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 验证表单
|
||
|
|
validateForm() {
|
||
|
|
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_1.default.createWarehouse(this.data.addForm);
|
||
|
|
this.setData({
|
||
|
|
loading: false,
|
||
|
|
successMessage: '仓库添加成功',
|
||
|
|
currentTab: 'list'
|
||
|
|
});
|
||
|
|
// 重置表单
|
||
|
|
this.resetForm();
|
||
|
|
// 重新加载仓库列表
|
||
|
|
this.loadWarehouses();
|
||
|
|
(0, helpers_1.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
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 取消编辑
|
||
|
|
cancelEdit() {
|
||
|
|
this.setData({
|
||
|
|
currentTab: 'list',
|
||
|
|
errorMessage: '',
|
||
|
|
successMessage: ''
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 编辑表单输入处理
|
||
|
|
onEditNameInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.name': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditAddressInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.address': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditContactInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.contact': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditPhoneInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.phone': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditDescriptionInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.description': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditCapacityInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.capacity': parseInt(e.detail.value) || 500
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditLongitudeInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.longitude': parseFloat(e.detail.value) || 102.833722
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditLatitudeInput(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.latitude': parseFloat(e.detail.value) || 24.880095
|
||
|
|
});
|
||
|
|
},
|
||
|
|
onEditStatusChange(e) {
|
||
|
|
this.setData({
|
||
|
|
'editForm.status': e.detail.value
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 编辑仓库
|
||
|
|
editWarehouse(e) {
|
||
|
|
const warehouseId = e.currentTarget.dataset.id;
|
||
|
|
const warehouse = this.data.warehouses.find(w => w.id === warehouseId);
|
||
|
|
if (warehouse) {
|
||
|
|
this.setData({
|
||
|
|
editForm: {
|
||
|
|
id: warehouse.id || 0,
|
||
|
|
name: warehouse.name || '',
|
||
|
|
address: warehouse.address || '',
|
||
|
|
contact: warehouse.contact || '',
|
||
|
|
phone: warehouse.phone || '',
|
||
|
|
description: warehouse.description || '',
|
||
|
|
capacity: warehouse.capacity || 500,
|
||
|
|
longitude: warehouse.longitude || 102.833722,
|
||
|
|
latitude: warehouse.latitude || 24.880095,
|
||
|
|
status: warehouse.status || 'open'
|
||
|
|
},
|
||
|
|
currentTab: 'edit'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 提交编辑仓库表单
|
||
|
|
async submitEditForm() {
|
||
|
|
this.setData({
|
||
|
|
errorMessage: '',
|
||
|
|
successMessage: ''
|
||
|
|
});
|
||
|
|
if (!this.validateEditForm()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.setData({
|
||
|
|
loading: true
|
||
|
|
});
|
||
|
|
try {
|
||
|
|
// 使用updateWarehouse更新仓库信息
|
||
|
|
await warehouseService_1.default.updateWarehouse(this.data.editForm.id, this.data.editForm);
|
||
|
|
this.setData({
|
||
|
|
loading: false,
|
||
|
|
successMessage: '仓库更新成功',
|
||
|
|
currentTab: 'list'
|
||
|
|
});
|
||
|
|
// 重新加载仓库列表
|
||
|
|
this.loadWarehouses();
|
||
|
|
(0, helpers_1.showToast)('仓库更新成功');
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
console.error('更新仓库失败:', error);
|
||
|
|
this.setData({
|
||
|
|
loading: false,
|
||
|
|
errorMessage: '更新仓库失败,请重试'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// 验证编辑表单
|
||
|
|
validateEditForm() {
|
||
|
|
const { name, address, contact, phone } = this.data.editForm;
|
||
|
|
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 deleteWarehouse(e) {
|
||
|
|
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_1.default.deleteWarehouse(warehouseId);
|
||
|
|
(0, helpers_1.showToast)('仓库删除成功');
|
||
|
|
// 重新加载仓库列表
|
||
|
|
this.loadWarehouses();
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
console.error('删除仓库失败:', error);
|
||
|
|
(0, helpers_1.showToast)('删除仓库失败');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// 获取仓库状态文本
|
||
|
|
getWarehouseStatusText(status) {
|
||
|
|
const statusMap = {
|
||
|
|
'open': '营业中',
|
||
|
|
'closed': '已关闭',
|
||
|
|
'maintenance': '维护中'
|
||
|
|
};
|
||
|
|
return statusMap[status || 'open'] || '未知状态';
|
||
|
|
},
|
||
|
|
// 获取仓库库存状态
|
||
|
|
getWarehouseInventoryStatus(currentStock, capacity) {
|
||
|
|
const percentage = (currentStock / capacity) * 100;
|
||
|
|
if (percentage >= 90)
|
||
|
|
return '库存充足';
|
||
|
|
if (percentage >= 60)
|
||
|
|
return '库存正常';
|
||
|
|
if (percentage >= 30)
|
||
|
|
return '库存偏低';
|
||
|
|
return '库存不足';
|
||
|
|
},
|
||
|
|
// 获取库存状态对应的颜色
|
||
|
|
getInventoryStatusColor(currentStock, capacity) {
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
});
|