Files
klp-oa/klp-ui/src/components/KLPService/RawMaterialSelect/index.vue

400 lines
12 KiB
Vue
Raw Normal View History

2025-07-18 17:22:56 +08:00
<template>
<div class="raw-material-selector">
<!-- 触发器按钮 -->
<div class="el-input" v-if="!readonly" type="text" @click="dialogVisible = true">
<div class="el-input__inner">
<el-icon class="el-icon-search"></el-icon>
{{ buttonText }}
</div>
</div>
<span v-else class="readonly-text">{{ displayText }}</span>
<!-- 选择对话框 -->
<el-dialog title="选择原材料" :visible.sync="dialogVisible" width="1200px" :close-on-click-modal="false"
@close="handleClose" append-to-body>
<!-- 搜索区域 -->
<el-form :inline="true" :model="queryParams" class="search-form" size="small">
<el-form-item label="编号">
<el-input v-model="queryParams.rawMaterialCode" placeholder="请输入原材料编号" clearable
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="名称">
<MemoInput storageKey="productName" v-model="queryParams.rawMaterialName" placeholder="请输入原材料名称" clearable
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="规格">
<MemoInput storageKey="coilSpec" v-model="queryParams.specification" placeholder="请输入规格"
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="材质">
<MemoInput storageKey="material" v-model="queryParams.material" placeholder="请输入材质"
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="厂家">
<MemoInput storageKey="manufacturer" v-model="queryParams.manufacturer" placeholder="请输入厂家"
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="表面处理">
<MemoInput storageKey="surfaceTreatmentDesc" v-model="queryParams.surfaceTreatmentDesc" placeholder="请输入表面处理"
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item label="镀层质量">
<MemoInput storageKey="zincLayer" v-model="queryParams.zincLayer" placeholder="请输入镀层质量"
@keyup.enter.native="handleQuery" />
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
</el-form-item>
2025-08-19 15:39:59 +08:00
</el-form>
<h3>最近选择</h3>
<el-table v-loading="recentLoading" title="最近选择" :data="recentlySelectedList" @row-click="handleRowClick">
<el-table-column label="编号" prop="rawMaterialCode" />
<el-table-column label="名称" prop="rawMaterialName" />
<el-table-column label="规格" prop="specification" />
<el-table-column label="材质" prop="material" />
<el-table-column label="厂家" prop="manufacturer" />
<el-table-column label="表面处理" prop="surfaceTreatmentDesc" />
<el-table-column label="镀层质量" prop="zincLayer" />
</el-table>
<!-- 卡片布局 -->
<div v-loading="loading" class="card-container">
<div v-for="(item, index) in rawMaterialList" :key="index + item.rawMaterialId" class="material-card"
:class="{ 'selected-card': isSelected(item.rawMaterialId) }" @click="handleRowClick(item)">
<!-- 卡片内容包含所有原表格字段 -->
<div class="card-content">
<div class="card-title">
<span class="name">{{ item.rawMaterialName }}</span>
<span class="code">[{{ item.rawMaterialCode }}]</span>
</div>
<div class="card-info">
<div class="info-item">规格<span>{{ item.specification || '-' }}</span></div>
<div class="info-item">材质<span>{{ item.material || '-' }}</span></div>
<div class="info-item">厂家<span>{{ item.manufacturer || '-' }}</span></div>
<div class="info-item">表面处理<span>{{ item.surfaceTreatmentDesc || '-' }}</span></div>
<div class="info-item">镀层质量<span>{{ item.zincLayer || '-' }}</span></div>
</div>
</div>
</div>
<!-- 空数据提示 -->
<div class="empty-tip" v-if="rawMaterialList.length === 0 && !loading">
暂无匹配的原材料数据
</div>
</div>
<!-- 分页 -->
<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
@pagination="getList" />
2025-08-19 15:39:59 +08:00
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
</div>
</el-dialog>
</div>
2025-07-18 17:22:56 +08:00
</template>
<script>
import { listRawMaterial, getRawMaterial } from '@/api/wms/rawMaterial';
import MemoInput from '@/components/MemoInput/index.vue'
2025-07-21 11:55:04 +08:00
2025-07-18 17:22:56 +08:00
export default {
name: 'RawMaterialSelector',
components: {
MemoInput
},
2025-07-18 17:22:56 +08:00
props: {
// 绑定值原材料ID 单选字符串格式
value: {
2025-07-18 17:22:56 +08:00
type: String,
default: ''
2025-08-19 15:39:59 +08:00
},
// 是否只读
readonly: {
2025-08-19 15:39:59 +08:00
type: Boolean,
default: false
},
// 过滤条件
filters: {
type: Object,
default: () => ({})
}
},
2025-07-18 17:22:56 +08:00
data() {
return {
dialogVisible: false,
2025-08-19 15:39:59 +08:00
loading: false,
recentLoading: false,
rawMaterialList: [],
recentlySelectedList: [],
total: 0,
queryParams: {
pageNum: 1,
pageSize: 10,
rawMaterialCode: undefined,
rawMaterialName: undefined,
specification: undefined,
material: undefined,
manufacturer: undefined,
surfaceTreatmentDesc: undefined,
zincLayer: undefined
2025-08-19 15:39:59 +08:00
},
// ✅ 单选核心变量选中的ID和单条数据对象
selectedId: '',
selectedRow: {}
2025-07-18 17:22:56 +08:00
};
},
computed: {
// 选择按钮展示文本
buttonText() {
if (!this.selectedId) {
return '请选择原材料';
}
return `${this.selectedRow.rawMaterialName}[${this.selectedRow.specification || '-'}] (${this.selectedRow.material || '-'})`;
2025-07-21 10:56:50 +08:00
},
// 只读状态展示文本
displayText() {
return this.selectedRow.rawMaterialName || '未选择';
}
2025-07-18 17:22:56 +08:00
},
watch: {
// 监听父组件传值 实现值回显 ✅修复原回显BUG
value: {
immediate: true,
async handler(val) {
this.selectedId = val || '';
// 有选中ID则获取详情赋值
if (this.selectedId) {
const res = await getRawMaterial(this.selectedId);
if (res.code === 200) this.selectedRow = res.data;
} else {
this.selectedRow = {};
}
}
},
// 弹窗打开时加载数据
dialogVisible(val) {
if (val) {
this.getList();
}
2025-08-23 17:22:06 +08:00
}
2025-07-30 10:53:06 +08:00
},
2025-07-18 17:22:56 +08:00
methods: {
// 判断卡片是否选中
isSelected(rawMaterialId) {
return this.selectedId === rawMaterialId;
},
// 获取最近选择的原材料数据
async listRecentlySelected() {
try {
this.recentLoading = true;
const ids = localStorage.getItem('recentlySelectedRawMaterialIds') || '';
const idsArray = ids.split(',').filter(id => id);
if (idsArray.length === 0) return;
const list = await Promise.all(idsArray.map(async id => {
const response = await getRawMaterial(id);
return response.data || {};
}));
this.recentlySelectedList = list || [];
} catch (error) {
console.error('获取最近选择的原材料列表失败', error);
this.$message.error('获取最近选择的原材料列表失败');
} finally {
this.recentLoading = false;
}
},
// 获取原材料列表 ✅修复原数据重复BUG
async getList() {
try {
this.loading = true;
const params = { ...this.queryParams, ...this.filters };
const response = await listRawMaterial(params);
if (response.code === 200) {
this.rawMaterialList = response.rows || [];
this.total = response.total || 0;
// 选中的项不在列表中则追加进去,保证选中高亮
if (this.selectedId && !this.rawMaterialList.some(item => item.rawMaterialId === this.selectedId)) {
const res = await getRawMaterial(this.selectedId);
res.code === 200 && this.rawMaterialList.push(res.data);
}
}
} catch (error) {
console.error('获取原材料列表失败', error);
this.$message.error('获取原材料列表失败');
} finally {
this.loading = false;
}
},
// 搜索
handleQuery() {
this.queryParams.pageNum = 1;
this.getList();
},
// 重置搜索条件
resetQuery() {
this.queryParams = {
pageNum: 1,
pageSize: 10,
rawMaterialCode: undefined,
rawMaterialName: undefined,
specification: undefined,
material: undefined,
manufacturer: undefined,
surfaceTreatmentDesc: undefined,
zincLayer: undefined
2025-08-19 15:39:59 +08:00
};
this.getList();
2025-08-19 15:39:59 +08:00
},
// 点击行/卡片选中 核心单选事件
handleRowClick(row) {
// 缓存最近选择数据最多5条
const index = this.recentlySelectedList.findIndex(item => item.rawMaterialId === row.rawMaterialId);
if (index > -1) this.recentlySelectedList.splice(index, 1);
this.recentlySelectedList.unshift(row);
this.recentlySelectedList.length > 5 && this.recentlySelectedList.pop();
localStorage.setItem('recentlySelectedRawMaterialIds', this.recentlySelectedList.map(item => item.rawMaterialId).join(','));
// 赋值选中状态
this.selectedId = row.rawMaterialId;
this.selectedRow = row;
// 向父组件传值
this.$emit('input', this.selectedId);
this.$emit('change', this.selectedId, this.selectedRow);
// 关闭弹窗
this.dialogVisible = false;
},
// 弹窗关闭时恢复选中状态,防止误操作
handleClose() {
this.selectedId = this.value || '';
this.dialogVisible = false;
2025-08-19 15:39:59 +08:00
}
},
mounted() {
this.listRecentlySelected();
this.getList();
2025-07-18 17:22:56 +08:00
}
};
</script>
2025-07-21 10:56:50 +08:00
<style scoped lang="scss">
.raw-material-selector {
display: inline-block;
}
.search-form {
margin-bottom: 20px;
flex-wrap: wrap;
}
.readonly-text {
color: #606266;
line-height: 1;
padding: 8px 0;
display: inline-block;
2025-07-21 10:56:50 +08:00
}
2025-08-19 15:39:59 +08:00
::v-deep .el-dialog__body {
padding: 20px;
overflow-x: auto;
2025-07-21 10:56:50 +08:00
}
2025-08-19 15:39:59 +08:00
::v-deep .el-form-item {
margin-bottom: 15px;
margin-right: 15px;
2025-07-21 10:56:50 +08:00
}
/* 卡片容器样式 */
.card-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
gap: 20px;
margin-bottom: 20px;
min-height: 400px;
padding: 10px;
}
/* 卡片样式 */
.material-card {
border: 1px solid #e6e6e6;
border-radius: 0px;
padding: 12px 16px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
background: #fff;
&:hover {
border-color: #409eff;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
}
}
/* 选中卡片样式 */
.selected-card {
border-color: #409eff;
background-color: #f0f7ff;
}
/* 卡片内容样式 */
.card-content {
width: 100%;
}
.card-title {
display: flex;
align-items: center;
margin-bottom: 12px;
font-weight: 500;
}
.name {
font-size: 16px;
color: #1989fa;
margin-right: 8px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.code {
font-size: 14px;
color: #909399;
}
.card-info {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
font-size: 14px;
color: #606266;
}
.info-item {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.info-item span {
color: #303133;
}
/* 空数据提示 */
.empty-tip {
grid-column: 1 / -1;
display: flex;
align-items: center;
justify-content: center;
height: 400px;
color: #909399;
font-size: 16px;
}
</style>