<template>
|
<div class="app-container">
|
<div class="search_form" style="margin-bottom: 10px">
|
<div>
|
<span class="search_title">仓库名称:</span>
|
<el-input
|
v-model="searchForm.warehouseName"
|
placeholder="请输入仓库名称"
|
clearable
|
style="width: 200px"
|
@keyup.enter="handleQuery"
|
/>
|
<span class="search_title ml10">仓库位置:</span>
|
<el-input
|
v-model="searchForm.location"
|
placeholder="请输入仓库位置"
|
clearable
|
style="width: 200px"
|
@keyup.enter="handleQuery"
|
/>
|
<span class="search_title ml10">负责人:</span>
|
<el-input
|
v-model="searchForm.managerName"
|
placeholder="请输入负责人"
|
clearable
|
style="width: 200px"
|
@keyup.enter="handleQuery"
|
/>
|
<el-button type="primary" @click="handleQuery" style="margin-left: 10px"
|
>搜索</el-button
|
>
|
<el-button @click="handleReset">重置</el-button>
|
</div>
|
<div>
|
<el-button type="primary" @click="handleAdd">新增</el-button>
|
<el-button type="danger" plain @click="handleDelete">删除</el-button>
|
</div>
|
</div>
|
<div class="table_list">
|
<el-table
|
:data="tableData"
|
border
|
v-loading="tableLoading"
|
@selection-change="handleSelectionChange"
|
style="width: 100%"
|
height="calc(100vh - 18.5em)"
|
>
|
<el-table-column align="center" type="selection" width="55" />
|
<el-table-column align="center" label="序号" type="index" width="60" />
|
<el-table-column
|
label="仓库名称"
|
prop="warehouseName"
|
min-width="150"
|
show-overflow-tooltip
|
/>
|
<el-table-column
|
label="仓库位置"
|
prop="location"
|
min-width="200"
|
show-overflow-tooltip
|
/>
|
<el-table-column
|
label="负责人"
|
prop="managerName"
|
min-width="120"
|
show-overflow-tooltip
|
/>
|
<el-table-column
|
label="联系电话"
|
prop="contactPhone"
|
min-width="150"
|
show-overflow-tooltip
|
/>
|
<el-table-column label="状态" prop="status" width="100" align="center">
|
<template #default="scope">
|
<el-tag :type="scope.row.status ? 'success' : 'info'" size="small">
|
{{ scope.row.status ? '启用' : '停用' }}
|
</el-tag>
|
</template>
|
</el-table-column>
|
<el-table-column
|
label="创建时间"
|
prop="createTime"
|
min-width="180"
|
show-overflow-tooltip
|
/>
|
<el-table-column label="操作" width="150" align="center" fixed="right">
|
<template #default="scope">
|
<el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
|
<el-button link type="danger" size="small" @click="handleRowDelete(scope.row)">删除</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
<pagination
|
v-show="total > 0"
|
:total="total"
|
layout="total, sizes, prev, pager, next, jumper"
|
:page="page.current"
|
:limit="page.size"
|
@pagination="paginationChange"
|
/>
|
</div>
|
|
<!-- 新增/编辑对话框 -->
|
<el-dialog
|
v-model="dialogVisible"
|
:title="dialogTitle"
|
width="600"
|
@close="closeDialog"
|
>
|
<el-form
|
ref="formRef"
|
:model="formState"
|
label-width="100px"
|
:rules="formRules"
|
>
|
<el-form-item label="仓库名称" prop="warehouseName">
|
<el-input
|
v-model="formState.warehouseName"
|
placeholder="请输入仓库名称"
|
maxlength="50"
|
show-word-limit
|
/>
|
</el-form-item>
|
<el-form-item label="仓库位置" prop="location">
|
<el-input
|
v-model="formState.location"
|
placeholder="请输入仓库位置"
|
maxlength="200"
|
show-word-limit
|
/>
|
</el-form-item>
|
<el-form-item label="负责人" prop="managerId">
|
<el-select
|
v-model="formState.managerId"
|
placeholder="请选择负责人"
|
clearable
|
filterable
|
style="width: 100%"
|
@change="handleManagerChange"
|
>
|
<el-option
|
v-for="user in userList"
|
:key="user.userId"
|
:label="user.nickName"
|
:value="user.userId"
|
/>
|
</el-select>
|
</el-form-item>
|
<el-form-item label="联系电话" prop="contactPhone">
|
<el-input
|
v-model="formState.contactPhone"
|
placeholder="请输入联系电话"
|
maxlength="20"
|
show-word-limit
|
/>
|
</el-form-item>
|
<el-form-item label="状态" prop="status">
|
<el-radio-group v-model="formState.status">
|
<el-radio :value="true">启用</el-radio>
|
<el-radio :value="false">停用</el-radio>
|
</el-radio-group>
|
</el-form-item>
|
</el-form>
|
<template #footer>
|
<div class="dialog-footer">
|
<el-button type="primary" @click="handleSubmit">确认</el-button>
|
<el-button @click="closeDialog">取消</el-button>
|
</div>
|
</template>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script setup>
|
import pagination from "@/components/PIMTable/Pagination.vue";
|
import { ref, reactive, computed, getCurrentInstance, onMounted } from "vue";
|
import { ElMessageBox, ElMessage } from "element-plus";
|
import {
|
getWarehousePage,
|
addWarehouse,
|
updateWarehouse,
|
delWarehouse,
|
updateWarehouseStatus,
|
} from "@/api/inventoryManagement/warehouse.js";
|
import { listUser } from "@/api/system/user.js";
|
|
const { proxy } = getCurrentInstance();
|
const tableData = ref([]);
|
const selectedRows = ref([]);
|
const tableLoading = ref(false);
|
|
const page = reactive({
|
current: 1,
|
size: 10,
|
});
|
const total = ref(0);
|
|
// 搜索表单
|
const searchForm = reactive({
|
warehouseName: "",
|
location: "",
|
managerName: "",
|
});
|
|
// 对话框相关
|
const dialogVisible = ref(false);
|
const dialogType = ref("add"); // 'add' 或 'edit'
|
const dialogTitle = computed(() =>
|
dialogType.value === "add" ? "新增仓库" : "编辑仓库"
|
);
|
const isEdit = computed(() => dialogType.value === "edit");
|
const formRef = ref();
|
|
// 表单数据
|
const formState = reactive({
|
id: undefined,
|
warehouseName: "",
|
location: "",
|
managerId: undefined,
|
managerName: "",
|
contactPhone: "",
|
status: true,
|
});
|
|
// 用户列表
|
const userList = ref([]);
|
|
// 表单验证规则
|
const formRules = {
|
warehouseName: [
|
{ required: true, message: "请输入仓库名称", trigger: "blur" },
|
{ min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
|
],
|
location: [
|
{ required: true, message: "请输入仓库位置", trigger: "blur" },
|
{ min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
|
],
|
managerId: [
|
{ required: true, message: "请选择负责人", trigger: "change" },
|
],
|
contactPhone: [
|
{ required: true, message: "请输入联系电话", trigger: "blur" },
|
{ pattern: /^1[3-9]\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: "请输入正确的电话号码", trigger: "blur" },
|
],
|
status: [{ required: true, message: "请选择状态", trigger: "change" }],
|
};
|
|
// 获取用户列表
|
const getUserList = () => {
|
listUser({ pageNum: 1, pageSize: 1000 }).then((res) => {
|
userList.value = res.rows || [];
|
});
|
};
|
|
// 负责人选择变化
|
const handleManagerChange = (userId) => {
|
const user = userList.value.find((item) => item.userId === userId);
|
if (user) {
|
formState.managerName = user.nickName;
|
} else {
|
formState.managerName = "";
|
}
|
};
|
|
// 查询列表
|
const handleQuery = () => {
|
page.current = 1;
|
getList();
|
};
|
|
// 重置搜索
|
const handleReset = () => {
|
searchForm.warehouseName = "";
|
searchForm.location = "";
|
searchForm.managerName = "";
|
handleQuery();
|
};
|
|
const paginationChange = (obj) => {
|
page.current = obj.page;
|
page.size = obj.limit;
|
getList();
|
};
|
|
const getList = () => {
|
tableLoading.value = true;
|
getWarehousePage({
|
...searchForm,
|
current: page.current,
|
size: page.size,
|
})
|
.then((res) => {
|
tableLoading.value = false;
|
tableData.value = res.data.records || [];
|
total.value = res.data.total || 0;
|
})
|
.catch(() => {
|
tableLoading.value = false;
|
});
|
};
|
|
// 新增
|
const handleAdd = () => {
|
dialogType.value = "add";
|
resetForm();
|
dialogVisible.value = true;
|
};
|
|
// 编辑
|
const handleEdit = (row) => {
|
dialogType.value = "edit";
|
resetForm();
|
// 根据managerName查找对应的managerId
|
const user = userList.value.find((item) => item.nickName === row.managerName);
|
// 填充表单数据
|
Object.assign(formState, {
|
id: row.id,
|
warehouseName: row.warehouseName,
|
location: row.location,
|
managerId: user ? user.userId : row.managerId,
|
managerName: row.managerName,
|
contactPhone: row.contactPhone,
|
status: row.status,
|
});
|
dialogVisible.value = true;
|
};
|
|
// 重置表单
|
const resetForm = () => {
|
formState.id = undefined;
|
formState.warehouseName = "";
|
formState.location = "";
|
formState.managerId = undefined;
|
formState.managerName = "";
|
formState.contactPhone = "";
|
formState.status = true;
|
proxy.$refs["formRef"]?.resetFields();
|
};
|
|
// 关闭对话框
|
const closeDialog = () => {
|
dialogVisible.value = false;
|
resetForm();
|
};
|
|
// 提交表单
|
const handleSubmit = () => {
|
proxy.$refs["formRef"].validate((valid) => {
|
if (valid) {
|
if (dialogType.value === "add") {
|
submitAdd();
|
} else {
|
submitEdit();
|
}
|
}
|
});
|
};
|
|
// 提交新增
|
const submitAdd = () => {
|
const params = { ...formState };
|
delete params.id;
|
addWarehouse(params)
|
.then(() => {
|
ElMessage.success("新增成功");
|
closeDialog();
|
getList();
|
})
|
.catch(() => {
|
ElMessage.error("新增失败");
|
});
|
};
|
|
// 提交编辑
|
const submitEdit = () => {
|
const params = { ...formState };
|
updateWarehouse(params)
|
.then(() => {
|
ElMessage.success("编辑成功");
|
closeDialog();
|
getList();
|
})
|
.catch(() => {
|
ElMessage.error("编辑失败");
|
});
|
};
|
|
// 状态变更
|
const handleStatusChange = (row) => {
|
const statusText = row.status ? "启用" : "停用";
|
ElMessageBox.confirm(
|
`确认要${statusText}该仓库吗?`,
|
"提示",
|
{
|
confirmButtonText: "确认",
|
cancelButtonText: "取消",
|
type: "warning",
|
}
|
)
|
.then(() => {
|
updateWarehouseStatus(row.id, row.status)
|
.then(() => {
|
ElMessage.success(`${statusText}成功`);
|
getList();
|
})
|
.catch(() => {
|
// 恢复原状态
|
row.status = !row.status;
|
ElMessage.error(`${statusText}失败`);
|
});
|
})
|
.catch(() => {
|
// 恢复原状态
|
row.status = !row.status;
|
ElMessage.info("已取消");
|
});
|
};
|
|
// 表格选择数据
|
const handleSelectionChange = (selection) => {
|
selectedRows.value = selection;
|
};
|
|
// 批量删除
|
const handleDelete = () => {
|
if (selectedRows.value.length === 0) {
|
proxy.$modal.msgWarning("请选择要删除的数据");
|
return;
|
}
|
const ids = selectedRows.value.map((item) => item.id);
|
ElMessageBox.confirm("选中的内容将被删除,是否确认删除?", "提示", {
|
confirmButtonText: "确认",
|
cancelButtonText: "取消",
|
type: "warning",
|
})
|
.then(() => {
|
delWarehouse(ids).then(() => {
|
proxy.$modal.msgSuccess("删除成功");
|
getList();
|
});
|
})
|
.catch(() => {
|
proxy.$modal.msg("已取消");
|
});
|
};
|
|
// 单行删除
|
const handleRowDelete = (row) => {
|
ElMessageBox.confirm("确认删除该仓库吗?", "提示", {
|
confirmButtonText: "确认",
|
cancelButtonText: "取消",
|
type: "warning",
|
})
|
.then(() => {
|
delWarehouse([row.id]).then(() => {
|
proxy.$modal.msgSuccess("删除成功");
|
getList();
|
});
|
})
|
.catch(() => {
|
proxy.$modal.msg("已取消");
|
});
|
};
|
|
// 初始化加载数据
|
onMounted(() => {
|
getList();
|
getUserList();
|
});
|
</script>
|
|
<style scoped>
|
.search_form {
|
display: flex;
|
justify-content: space-between;
|
align-items: center;
|
flex-wrap: wrap;
|
gap: 10px;
|
}
|
.search_title {
|
font-size: 14px;
|
color: #606266;
|
}
|
.ml10 {
|
margin-left: 10px;
|
}
|
.table_list {
|
margin-top: 10px;
|
}
|
</style>
|