605 lines
13 KiB
Vue
605 lines
13 KiB
Vue
<template>
|
||
<view class="detail-container">
|
||
<!-- 商品明细展示区 -->
|
||
<view class="detail-display" v-if="selectedDetail">
|
||
<view class="detail-header">
|
||
<view class="search-box">
|
||
<view class="search-box-inner">
|
||
<u-search v-model="searchKeyword" placeholder="搜索运单号、货主、商品条码、车牌" bg-color="#f5f5f5" shape="round"
|
||
:show-action="false" @search="handleSearch" @clear="handleSearch"></u-search>
|
||
<view class="scan-camera-btn" @click="handleManualScan">
|
||
<u-icon name="camera" size="34rpx" color="#2979ff"></u-icon>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="detail-actions">
|
||
<u-button type="error" size="mini" @click="handleDeleteDetail(selectedDetail.id)">
|
||
<u-icon name="trash"></u-icon> 删除
|
||
</u-button>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 商品详细信息展示 -->
|
||
<scroll-view scroll-y class="detail-content">
|
||
<product-info-editor ref="productEditor" :detail-data="selectedDetail" @update="handleDetailUpdate"
|
||
@change="$emit('change')" @selectDetailChange="selectDetailChange" @SpaceInput="SpaceInput" @SpaceSearch="scan" @tryGetStock="handleProductBarcodeSearch" @print="printBarcode"></product-info-editor>
|
||
</scroll-view>
|
||
</view>
|
||
<!-- 空状态 -->
|
||
<view class="empty-detail" v-else>
|
||
<u-empty mode="list" text="请选择商品明细"></u-empty>
|
||
</view>
|
||
|
||
<!-- 商品列表分页显示区 -->
|
||
<view class="list-container">
|
||
<view class="list-header">
|
||
<text class="total">共 {{ totalDetails }} 项</text>
|
||
<view class="list-actions">
|
||
<view class="page-info">{{ currentPage }} / {{ totalPages }}</view>
|
||
<u-button type="primary" size="mini" @click="handleAddDetail">
|
||
<u-icon name="plus" size="14"></u-icon> 新增商品
|
||
</u-button>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 商品网格列表 -->
|
||
<swiper class="quick-swiper" :current="currentPage - 1" @change="handleSwiperChange" :duration="300">
|
||
<swiper-item v-for="(pageItems, pageIndex) in paginatedDetails" :key="pageIndex">
|
||
<view class="grid-page">
|
||
<view class="grid-item" v-for="(item, index) in pageItems" :key="item.id"
|
||
:class="{ 'active': selectedDetail && selectedDetail.id === item.id }"
|
||
@click="handleSelectDetail(item)">
|
||
<view class="item-index">
|
||
{{ (pageIndex * itemsPerPage) + index + 1 }}
|
||
</view>
|
||
<view class="item-content">
|
||
<text class="item-name">{{ truncateText(item.product_name,5) }}</text>
|
||
<text class="item-quantity">{{ item.actual_quantity }}</text>
|
||
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</swiper-item>
|
||
</swiper>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import ProductInfoEditor from './ProductInfoEditor.vue';
|
||
export default {
|
||
name: 'DetailContainer',
|
||
components: {
|
||
ProductInfoEditor
|
||
},
|
||
props: {
|
||
selectedDetail: Object,
|
||
details: {
|
||
type: Array,
|
||
default: () => []
|
||
}
|
||
},
|
||
filters: {
|
||
// 截取文本过滤器
|
||
truncateFilter(text, length = 20) {
|
||
if (!text) return '';
|
||
if (text.length <= length) return text;
|
||
return text.substring(0, length) + '...';
|
||
}
|
||
},
|
||
data() {
|
||
return {
|
||
searchKeyword: '',
|
||
currentPage: 1,
|
||
itemsPerPage: 4,
|
||
isSpaceInput:false,
|
||
};
|
||
},
|
||
filters: {
|
||
detailStatusFilter(status) {
|
||
const map = {
|
||
'pending': '待收货',
|
||
'receiving': '收货中',
|
||
'completed': '已完成',
|
||
'cancelled': '已取消'
|
||
};
|
||
return map[status] || status;
|
||
}
|
||
},
|
||
computed: {
|
||
// 总项目数
|
||
totalDetails() {
|
||
return this.details.length;
|
||
},
|
||
// 总页数
|
||
totalPages() {
|
||
return Math.ceil(this.details.length / this.itemsPerPage);
|
||
},
|
||
// 分页数据:每页8个,分成多个页面
|
||
paginatedDetails() {
|
||
const pages = [];
|
||
const total = this.details.length;
|
||
|
||
for (let i = 0; i < total; i += this.itemsPerPage) {
|
||
pages.push(this.details.slice(i, i + this.itemsPerPage));
|
||
}
|
||
|
||
// 如果最后一项为空数组,移除它
|
||
if (pages.length > 0 && pages[pages.length - 1].length === 0) {
|
||
pages.pop();
|
||
}
|
||
|
||
return pages;
|
||
}
|
||
},
|
||
watch: {
|
||
details: {
|
||
handler(newVal) {
|
||
// 如果数据变化且之前有选中项,尝试保持选中
|
||
if (newVal.length > 0 && this.selectedDetail) {
|
||
const found = newVal.find(item => item.id === this.selectedDetail.id);
|
||
if (!found && newVal[0]) {
|
||
this.handleSelectDetail(newVal[0]);
|
||
}
|
||
} else if (newVal.length > 0 && !this.selectedDetail) {
|
||
this.handleSelectDetail(newVal[0]);
|
||
}
|
||
},
|
||
immediate: true
|
||
},
|
||
selectedDetail: {
|
||
handler(newVal) {
|
||
console.log("selectedDetail", newVal);
|
||
},
|
||
immediate: true
|
||
}
|
||
},
|
||
mounted() {
|
||
this.$store.dispatch('broadcast/setCallbacks', {
|
||
onResult: this.scan
|
||
});
|
||
this.$store.dispatch('broadcast/start');
|
||
},
|
||
destroyed() {
|
||
console.log("组件销毁");
|
||
|
||
this.$store.dispatch('broadcast/stop');
|
||
},
|
||
methods: {
|
||
speakMessage(message, options = {}) {
|
||
const text = String(message || '').trim();
|
||
if (!text) {
|
||
return;
|
||
}
|
||
|
||
const tools = this.$tools;
|
||
if (!tools || typeof tools.speakText !== 'function') {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const task = tools.speakText(text, options);
|
||
if (task && typeof task.catch === 'function') {
|
||
task.catch(error => {
|
||
console.warn('[ReceivingDetail-DetailContainer] 语音播报失败:', error);
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.warn('[ReceivingDetail-DetailContainer] 语音播报异常:', error);
|
||
}
|
||
},
|
||
// === 事件处理方法 ===
|
||
truncateText(text, length = 20) {
|
||
if (!text) return '';
|
||
if (text.length <= length) return text;
|
||
return text.substring(0, length) + '...';
|
||
},
|
||
selectDetailChange(data) {
|
||
console.log("下级数据更新",data);
|
||
this.$emit('selectDetailChange', data)
|
||
},
|
||
// 选择商品明细
|
||
handleSelectDetail(detail) {
|
||
this.$emit('select', detail);
|
||
},
|
||
|
||
// 搜索商品
|
||
handleSearch() {
|
||
this.$emit('tryGetStock', this.searchKeyword);
|
||
},
|
||
handleDetailUpdate(data) {
|
||
console.log(data);
|
||
},
|
||
// 清空搜索
|
||
handleClearSearch() {
|
||
this.searchKeyword = '';
|
||
this.$emit('search', '');
|
||
},
|
||
|
||
// 新增商品
|
||
handleAddDetail() {
|
||
this.$emit('add');
|
||
},
|
||
|
||
// 编辑商品
|
||
handleEditDetail(detail) {
|
||
this.$emit('edit', detail);
|
||
},
|
||
|
||
// 删除商品确认
|
||
handleDeleteDetail(id) {
|
||
this.$emit('delete', id);
|
||
},
|
||
|
||
// 预览图片
|
||
handlePreviewImage(images, index) {
|
||
if (!images || images.length === 0) return;
|
||
this.$emit('preview', {
|
||
images,
|
||
index
|
||
});
|
||
},
|
||
|
||
// Swiper切换
|
||
handleSwiperChange(e) {
|
||
this.currentPage = e.detail.current + 1;
|
||
},
|
||
|
||
// === 工具方法 ===
|
||
|
||
// 收货状态类型获取
|
||
getDetailStatusType(status) {
|
||
const map = {
|
||
'pending': 'warning',
|
||
'receiving': 'primary',
|
||
'completed': 'success',
|
||
'cancelled': 'error'
|
||
};
|
||
return map[status] || 'default';
|
||
},
|
||
|
||
// 匹配状态类型获取
|
||
getMatchedStatusType(status) {
|
||
const map = {
|
||
'matched': 'success',
|
||
'unmatched': 'warning',
|
||
'manual': 'info'
|
||
};
|
||
return map[status] || 'default';
|
||
},
|
||
printBarcode(data){
|
||
this.$emit('print',data);
|
||
},
|
||
handleProductBarcodeSearch(keyword) {
|
||
const barcode = String(keyword || '').trim();
|
||
if (!barcode) {
|
||
return;
|
||
}
|
||
|
||
this.$emit('tryGetStock', barcode);
|
||
},
|
||
normalizeUniScanResult(scanResult) {
|
||
if (!scanResult) {
|
||
return '';
|
||
}
|
||
|
||
if (typeof scanResult === 'string') {
|
||
return scanResult.trim();
|
||
}
|
||
|
||
if (typeof scanResult.result === 'string') {
|
||
return scanResult.result.trim();
|
||
}
|
||
|
||
if (Array.isArray(scanResult.result)) {
|
||
const firstResult = scanResult.result.find(item => item);
|
||
if (typeof firstResult === 'string') {
|
||
return firstResult.trim();
|
||
}
|
||
if (firstResult && typeof firstResult.data === 'string') {
|
||
return firstResult.data.trim();
|
||
}
|
||
}
|
||
|
||
if (typeof scanResult.data === 'string') {
|
||
return scanResult.data.trim();
|
||
}
|
||
|
||
return '';
|
||
},
|
||
handleManualScan() {
|
||
uni.scanCode({
|
||
onlyFromCamera: false,
|
||
scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'],
|
||
success: (res) => {
|
||
const rawCode = this.normalizeUniScanResult(res);
|
||
if (!rawCode) {
|
||
uni.showToast({
|
||
title: '未识别到有效内容',
|
||
icon: 'none'
|
||
});
|
||
return;
|
||
}
|
||
|
||
this.scan({
|
||
data: rawCode,
|
||
source: 'uni.scanCode'
|
||
});
|
||
},
|
||
fail: (error) => {
|
||
const errMsg = String(error?.errMsg || '').toLowerCase();
|
||
if (errMsg.includes('cancel')) {
|
||
return;
|
||
}
|
||
|
||
uni.showToast({
|
||
title: '扫码失败,请重试',
|
||
icon: 'none'
|
||
});
|
||
}
|
||
});
|
||
},
|
||
scan(data) {
|
||
if(!data.data){
|
||
return;
|
||
}
|
||
console.log("scan",data.data);
|
||
if(this.isSpaceInput){
|
||
this.$emit('searchSpace', data.data);
|
||
}else{
|
||
this.speakMessage('商品条码扫描成功');
|
||
this.$emit('tryGetStock', data.data);
|
||
}
|
||
},
|
||
SpaceInput(data){
|
||
console.log("扫描状态改变",data);
|
||
this.isSpaceInput = data;
|
||
}
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.detail-container {
|
||
height: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
// === 详情展示区样式 ===
|
||
.detail-display {
|
||
flex: 1;
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
margin-bottom: 20rpx;
|
||
overflow: hidden;
|
||
display: flex;
|
||
flex-direction: column;
|
||
|
||
.detail-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 20rpx;
|
||
border-bottom: 1rpx solid #f0f0f0;
|
||
|
||
.search-box {
|
||
flex: 1;
|
||
margin-right: 20rpx;
|
||
|
||
.search-box-inner {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 16rpx;
|
||
|
||
::v-deep .u-search {
|
||
flex: 1;
|
||
}
|
||
|
||
.scan-camera-btn {
|
||
width: 76rpx;
|
||
height: 76rpx;
|
||
border-radius: 38rpx;
|
||
background: #eef4ff;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
flex-shrink: 0;
|
||
box-shadow: 0 4rpx 12rpx rgba(41, 121, 255, 0.12);
|
||
|
||
&:active {
|
||
opacity: 0.85;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.detail-actions {
|
||
display: flex;
|
||
gap: 10rpx;
|
||
}
|
||
}
|
||
|
||
.detail-content {
|
||
flex: 1;
|
||
height: 50rpx;
|
||
|
||
.section {
|
||
margin-bottom: 30rpx;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
.section-title {
|
||
font-size: 32rpx;
|
||
font-weight: bold;
|
||
color: #333;
|
||
margin-bottom: 20rpx;
|
||
padding-bottom: 10rpx;
|
||
border-bottom: 2rpx solid #f0f0f0;
|
||
}
|
||
}
|
||
|
||
.detail-item {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-bottom: 15rpx;
|
||
|
||
.label {
|
||
width: 160rpx;
|
||
color: #666;
|
||
font-size: 28rpx;
|
||
}
|
||
|
||
.value {
|
||
flex: 1;
|
||
color: #333;
|
||
font-size: 28rpx;
|
||
}
|
||
}
|
||
|
||
.product-images {
|
||
margin-top: 30rpx;
|
||
|
||
.image-scroll {
|
||
white-space: nowrap;
|
||
margin-top: 10rpx;
|
||
|
||
.image-list {
|
||
display: inline-flex;
|
||
gap: 20rpx;
|
||
|
||
.product-image {
|
||
width: 150rpx;
|
||
height: 150rpx;
|
||
border-radius: 8rpx;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// === 空状态样式 ===
|
||
.empty-detail {
|
||
flex: 1;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
margin-bottom: 20rpx;
|
||
}
|
||
|
||
// === 列表容器样式 ===
|
||
.list-container {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 20rpx;
|
||
display: flex;
|
||
flex-direction: column;
|
||
height: 260rpx;
|
||
|
||
.list-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
height: 40rpx;
|
||
line-height: 40rpx;
|
||
margin-bottom: 20rpx;
|
||
|
||
.total {
|
||
color: #666;
|
||
font-size: 28rpx;
|
||
}
|
||
|
||
.list-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 20rpx;
|
||
|
||
.page-info {
|
||
width: 100rpx;
|
||
font-size: 28rpx;
|
||
color: #666;
|
||
text-align: center;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// === Swiper网格样式 ===
|
||
.quick-swiper {
|
||
flex: 1;
|
||
height: 150rpx;
|
||
|
||
.grid-page {
|
||
display: grid;
|
||
grid-template-columns: repeat(4, 1fr);
|
||
grid-template-rows: repeat(1, 1fr);
|
||
gap: 20rpx;
|
||
height: 100%;
|
||
|
||
.grid-item {
|
||
border: 2rpx solid #f0f0f0;
|
||
border-radius: 12rpx;
|
||
padding: 20rpx;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
transition: all 0.3s;
|
||
position: relative;
|
||
box-sizing: border-box;
|
||
|
||
&.active {
|
||
border-color: #2979ff;
|
||
background-color: rgba(41, 121, 255, 0.1);
|
||
transform: scale(0.98);
|
||
}
|
||
|
||
// 空白项样式
|
||
&.empty-item {
|
||
border: 2rpx dashed #f0f0f0;
|
||
background-color: transparent;
|
||
}
|
||
|
||
.item-index {
|
||
position: absolute;
|
||
top: 5rpx;
|
||
right: 10rpx;
|
||
font-size: 24rpx;
|
||
color: #999;
|
||
}
|
||
|
||
.item-content {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
width: 100%;
|
||
|
||
.item-name {
|
||
font-size: 26rpx;
|
||
color: #333;
|
||
margin-bottom: 10rpx;
|
||
text-align: center;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
display: -webkit-box;
|
||
-webkit-line-clamp: 2;
|
||
-webkit-box-orient: vertical;
|
||
line-height: 1.4;
|
||
}
|
||
|
||
.item-quantity {
|
||
font-size: 24rpx;
|
||
color: #666;
|
||
margin-bottom: 10rpx;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style> |