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

450 lines
13 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)">
<!-- 多选勾选框 -->
<el-checkbox v-if="multiple" :checked="isSelected(item.rawMaterialId)"
@change="(val) => handleCardSelection(val, item)" class="card-checkbox" />
<!-- 卡片内容包含所有原表格字段 -->
<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>
<el-button type="primary" @click="confirmSelection" v-if="multiple">
确认选择
</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: {
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
},
multiple: {
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,
rawMaterialList: [],
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
},
selectedIds: [],
selectedRows: [],
recentlySelectedList: [],
recentLoading: false
2025-07-18 17:22:56 +08:00
};
},
computed: {
buttonText() {
// 如果为选中任何原材料,显示选择原材料
// 如果为选中多个原材料,显示已选 X 项
// 如果为选中单个原材料,显示原材料名称
console.log(this.selectedIds);
if (this.selectedIds.length === 0) {
return '请选择原材料';
}
if (this.multiple) {
return `已选 ${this.selectedIds.length}`;
} else {
return `${this.selectedRows[0]?.rawMaterialName}[${this.selectedRows[0]?.specification || '-'}] (${this.selectedRows[0]?.material || '-'})`;
}
2025-07-21 10:56:50 +08:00
},
displayText() {
if (this.multiple) {
return this.selectedIds.length > 0
? `已选 ${this.selectedIds.length}`
: '未选择';
} else {
return this.selectedRows[0]?.rawMaterialName || '未选择';
}
},
2025-07-18 17:22:56 +08:00
},
watch: {
value: {
immediate: true,
handler(val) {
console.log('watch触发val', val);
this.selectedIds = val ? val.split(',').filter(id => id) : [];
// this.syncSelectedRows();
this.$forceUpdate(); // 新增:强制刷新视图
}
},
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.selectedIds.includes(rawMaterialId);
},
// 卡片选择事件(多选)
handleCardSelection(checked, item) {
if (checked) {
!this.selectedIds.includes(item.rawMaterialId) && this.selectedIds.push(item.rawMaterialId);
} else {
this.selectedIds = this.selectedIds.filter(id => id != item.rawMaterialId);
}
},
// 获取最近选择的数据
async listRecentlySelected() {
try {
this.recentLoading = true;
// 从localstorage中获取缓存的ids
const ids = localStorage.getItem('recentlySelectedRawMaterialIds') || '';
const idsArray = ids.split(',').filter(id => id);
console.log('idsArray', idsArray, idsArray.length === 0);
if (idsArray.length === 0) return;
const list = await Promise.all(idsArray.map(async id => {
const response = await getRawMaterial(id);
return response.data || {};
}));
console.log('recentlySelectedList', list);
this.recentlySelectedList = list || [];
} catch (error) {
console.error('获取最近选择的原材料列表失败', error);
this.$message.error('获取最近选择的原材料列表失败');
} finally {
this.recentLoading = false;
}
},
// 原有方法保持不变(仅修改同步选中状态逻辑)
async getList() {
try {
this.loading = true;
const params = { ...this.queryParams, ...this.filters };
console.log('params', params);
const response = await listRawMaterial(params);
if (response.code === 200) {
this.rawMaterialList = response.rows.map(item => ({
...item,
rawMaterialId: item.rawMaterialId.toString()
})) || [];
this.total = response.total || 0;
}
return this.rawMaterialList;
} 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) {
if (!this.multiple) {
// 存储在最近列表中修改localstorage, 列表中最多5个超过五个则删除最早的一个
const index = this.recentlySelectedList.findIndex(item => item.rawMaterialId === row.rawMaterialId);
if (index > -1) {
this.recentlySelectedList.splice(index, 1);
}
this.recentlySelectedList.unshift(row);
if (this.recentlySelectedList.length > 5) {
this.recentlySelectedList.pop();
}
localStorage.setItem('recentlySelectedRawMaterialIds', this.recentlySelectedList.map(item => item.rawMaterialId).join(','));
this.selectedIds = [row.rawMaterialId];
this.selectedRows = [row];
this.confirmSelection();
}
},
handleSelect(row) {
if (this.multiple) {
const index = this.selectedIds.indexOf(row.rawMaterialId);
index > -1
? (this.selectedIds.splice(index, 1), this.selectedRows.splice(index, 1))
: (this.selectedIds.push(row.rawMaterialId), this.selectedRows.push(row));
} else {
this.selectedIds = [row.rawMaterialId];
this.selectedRows = [row];
this.confirmSelection();
}
2025-08-19 15:39:59 +08:00
},
confirmSelection() {
const emitValue = this.selectedIds.join(',');
this.$emit('input', emitValue);
this.$emit('change', emitValue, this.selectedRows);
this.dialogVisible = false;
},
handleClose() {
this.selectedIds = this.value ? this.value.split(',').filter(id => id) : [];
// this.syncSelectedRows();
this.dialogVisible = false;
2025-08-19 15:39:59 +08:00
}
},
mounted() {
this.listRecentlySelected();
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: 4px;
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-checkbox {
position: absolute;
top: 16px;
right: 16px;
}
/* 卡片内容样式 */
.card-content {
margin-right: 24px;
/* 给多选框留空间 */
}
.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>