WMS_PDA/pages/work/order/bindPkgMat.vue
2026-08-02 20:41:52 +08:00

515 lines
14 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="package-binding-page">
<view class="normar_container">
<page-nav title="绑定包材">
<template #left>
<view
class="nav-manual-input"
:class="{ 'nav-manual-input--inactive': !showScanInput }"
@click="toggleScanInput"
>
<text class="nav-manual-input-text">{{ showScanInput ? '关闭手动输入' : '手动输入' }}</text>
</view>
</template>
<template #right>
<view class="nav-reset" @click="handleReset">
<text class="nav-reset-text">重置</text>
</view>
</template>
</page-nav>
<!-- 运单扫码输入区 -->
<view class="scan-section" v-show="showScanInput">
<view class="scan-input-wrap">
<u-input
v-model="scanInput"
placeholder="扫描/输入快递单号"
border="surround"
:customStyle="{ flex: 1 }"
:focus="scanFocused"
@confirm="handleScanConfirm"
></u-input>
<view class="scan-camera-btn" @click="handleCameraScan">
<u-icon name="camera" size="22" color="#2979ff"></u-icon>
</view>
</view>
</view>
<!-- 订单信息展示 -->
<view class="info-list" :class="{ 'info-list--no-scan': !showScanInput }" v-show="showOrderInfo">
<view class="info-row info-row--header">
<text class="info-label">快递号</text>
<text class="info-value">{{ orderInfo.waybill_code || '--' }}</text>
</view>
<view class="info-row">
<text class="info-label">包材</text>
<view class="info-value-input">
<u-input
v-model="packageInputCode"
placeholder="扫描/输入包材条码"
type="textarea"
:autoHeight="true"
border="surround"
:customStyle="{ flex: 1, minHeight: '72rpx', padding: '8rpx 0' }"
:focus="packageFocused"
@confirm="handlePackageCodeSearch"
></u-input>
<view class="scan-camera-btn-sm" @click="handlePackageCameraScan">
<u-icon name="camera" size="22" color="#2979ff"></u-icon>
</view>
</view>
</view>
</view>
<!-- 绑定状态提示(自动绑定,无需手动操作) -->
<view class="submit-section" v-show="showOrderInfo">
<view class="auto-bind-tip">
<text class="auto-bind-tip-text">扫描包材条码后将自动完成绑定</text>
</view>
</view>
</view>
</view>
</template>
<script>
import voice from '@/core/utils/voice';
export default {
data() {
return {
scanInput: '',
scanFocused: false,
showScanInput: false,
orderInfo: {
order_id: 0,
waybill_code: ''
},
suggestedPkg: null,
packageInputCode: '',
packageFocused: false,
scannedPackageMaterial: null,
showOrderInfo: false
};
},
onLoad() {
this.$store.dispatch('broadcast/setCallbacks', {
onResult: this.onScanData
});
this.$store.dispatch('broadcast/start');
},
onShow() {
this.$store.dispatch('broadcast/resume');
},
onHide() {
this.$store.dispatch('broadcast/pause');
},
onUnload() {
this.$store.dispatch('broadcast/stop');
},
methods: {
async onScanData(data) {
const scanText = data && data.data ? data.data.trim() : '';
console.log('[包材绑定] 广播扫描到数据:', scanText);
if (!scanText) return;
if (this.showOrderInfo) {
// 先尝试匹配包材,匹配不上则作为新快递单号处理
uni.showLoading({ title: '绑定包材中...', mask: true });
const matched = await this.tryMatchPackageMaterial(scanText);
if (matched) {
this.packageInputCode = scanText;
await this.$nextTick();
this.scannedPackageMaterial = matched;
this.packageInputCode = matched.material_name || matched.material_code || scanText;
await this.$nextTick();
this.$forceUpdate();
// 连续加载,进入自动绑定
await this.autoBindPackage(matched, scanText);
} else {
uni.hideLoading();
// 清空旧包材信息,作为新快递单查询
this.packageInputCode = '';
this.scannedPackageMaterial = null;
this.suggestedPkg = null;
this.scanInput = scanText;
this.fetchOrderInfo(scanText);
}
} else {
this.scanInput = scanText;
this.fetchOrderInfo(scanText);
}
},
toggleScanInput() {
this.showScanInput = !this.showScanInput;
},
handleScanConfirm(value) {
const code = value || this.scanInput;
if (!code) return;
this.fetchOrderInfo(code);
},
handleCameraScan() {
uni.scanCode({
success: (res) => {
this.scanInput = res.result;
this.handleScanConfirm(res.result);
}
});
},
handlePackageCameraScan() {
uni.scanCode({
success: (res) => {
this.packageInputCode = res.result;
this.handlePackageCodeSearch(res.result);
}
});
},
speakVoice(text) {
voice.speak(text, { minInterval: 800 });
},
async fetchOrderInfo(code) {
uni.showLoading({ title: '查询中...' });
try {
// 1. 调用 index.toOrder 用运单号获取订单ID
const toOrderRes = await this.$http('common.toOrder', { waybill_code: code }, '', false);
if (toOrderRes && toOrderRes.flag !== 'failure') {
const orderId = Number(toOrderRes.order_id);
if (!orderId || orderId <= 0) {
uni.showToast({ title: '未找到订单', icon: 'none' });
this.speakVoice('无记录');
return;
}
this.orderInfo = {
order_id: orderId,
waybill_code: code
};
this.showOrderInfo = true;
uni.showToast({ title: '查询成功', icon: 'success' });
// 3. 后台静默加载已绑定的包材信息(不阻塞界面)
// 传入扫描的运单号,用于匹配主单/子单对应的包材
this.loadBoundPackageMaterial(orderId, code);
} else {
uni.showToast({ title: (toOrderRes && toOrderRes.msg) || '查询失败', icon: 'none' });
this.speakVoice('无记录');
}
} catch (e) {
uni.showToast({ title: '网络异常', icon: 'none' });
this.speakVoice('无记录');
} finally {
uni.hideLoading();
}
},
/**
* 后台静默加载已绑定的包材信息(不阻塞界面)
* @param {number} orderId 订单ID
* @param {string} waybillCode 当前扫描的运单号(可能为主单或子单)
*/
async loadBoundPackageMaterial(orderId, waybillCode) {
try {
const detailRes = await this.$http('order.detail', { order_id: orderId, tableType: 'dispatch' }, '', false);
if (detailRes && detailRes.flag !== 'failure') {
const detailData = detailRes.data || {};
const expresses = detailData.expresses || [];
const targetCode = String(waybillCode || '').trim();
// 仅按当前扫描的运单号(主单/子单)精确匹配对应的快递记录,未命中则显示空值
const matchedExpress = expresses.find(e => {
const codeOf = String(e.waybill_code || e.waybill_no || e.waybillCode || '').trim();
return codeOf === targetCode;
});
if (matchedExpress && matchedExpress.package_material_id) {
this.suggestedPkg = {
id: matchedExpress.package_material_id,
name: matchedExpress.package_material_name_snapshot
};
this.packageInputCode = matchedExpress.package_material_name_snapshot || '';
} else {
// 未命中对应运单:清空包材显示
this.suggestedPkg = null;
this.packageInputCode = '';
}
}
} catch (e) {
// 静默失败,不影响主流程
console.error('[包材绑定] 加载已绑包材失败', e);
}
},
/**
* 仅搜索匹配包材(无 UI 副作用),返回匹配的包材对象或 null
*/
async tryMatchPackageMaterial(code) {
const trimmed = String(code).trim();
if (!trimmed) return null;
try {
const res = await this.$http('setting.packageMaterial.search', { keyword: trimmed }, '', false);
const list = Array.isArray(res?.data) ? res.data : [];
// 优先按 material_code 精确匹配,再按 barcodes 数组匹配
const matched = list.find(item => {
if (String(item.material_code || '') === trimmed) return true;
const barcodes = item.barcodes || [];
return barcodes.some(b => String(b.barcode || '') === trimmed);
}) || list[0];
return matched || null;
} catch (error) {
console.error('[包材匹配] 查询失败', error);
return null;
}
},
async handlePackageCodeSearch(value) {
const code = value || this.packageInputCode;
if (!code) {
uni.showToast({ title: '请输入包材条码', icon: 'none' });
return;
}
const trimmed = String(code).trim();
uni.showLoading({ title: '绑定包材中...', mask: true });
try {
const matched = await this.tryMatchPackageMaterial(trimmed);
if (matched) {
this.scannedPackageMaterial = matched;
this.packageInputCode = matched.material_name || matched.material_code || trimmed;
await this.$nextTick();
this.$forceUpdate();
// 连续加载,不隐藏,继续自动绑定
await this.autoBindPackage(matched, trimmed);
} else {
this.scannedPackageMaterial = null;
uni.hideLoading();
uni.showToast({ title: '无记录', icon: 'none' });
this.speakVoice('无记录');
}
} catch (error) {
uni.hideLoading();
const msg = String(error?.message || error?.msg || '查询包材失败');
uni.showToast({ title: msg, icon: 'none' });
}
},
async autoBindPackage(matched, trimmed) {
const waybillNo = String(this.orderInfo.waybill_code || '').trim();
if (!waybillNo) {
uni.showToast({ title: '缺少运单号,无法自动绑定', icon: 'none' });
return;
}
const packageMaterialId = String(matched.id || '');
if (!packageMaterialId) {
uni.showToast({ title: '包材数据异常,缺少ID', icon: 'none' });
return;
}
uni.showLoading({ title: '绑定中...', mask: true });
try {
const res = await this.$http('order.bindExpressPackageMaterialByWaybill', {
waybill_code: waybillNo,
package_material_id: packageMaterialId
}, '', false);
if (res && res.flag !== 'failure') {
uni.hideLoading();
uni.showToast({ title: '包材绑定成功', icon: 'success' });
this.speakVoice('包材更改为' + (matched.material_name || ''));
} else {
uni.hideLoading();
uni.showToast({ title: (res && res.msg) || '绑定失败', icon: 'none' });
this.speakVoice('绑定失败');
}
} catch (error) {
uni.hideLoading();
const msg = String(error?.message || error?.msg || '绑定失败');
uni.showToast({ title: msg, icon: 'none' });
this.speakVoice('绑定失败');
}
},
handleReset() {
this.resetForm();
},
resetForm() {
this.scanInput = '';
this.orderInfo = { order_id: 0, waybill_code: '' };
this.suggestedPkg = null;
this.packageInputCode = '';
this.packageFocused = false;
this.scannedPackageMaterial = null;
this.showOrderInfo = false;
this.scanFocused = true;
}
}
};
</script>
<style lang="scss" scoped>
.package-binding-page {
.normar_container {
min-height: 100vh;
background-color: #f5f5f5;
}
/* ===== 左侧手动输入按钮 ===== */
::v-deep .nav-title .left {
min-width: auto;
gap: 0;
}
::v-deep .nav-title .title {
padding-left: 40rpx;
}
::v-deep .nav-title .right {
width: auto;
min-width: 80rpx;
}
.nav-manual-input {
display: flex;
align-items: center;
justify-content: center;
width: 210rpx;
height: 52rpx;
background: #fa8c16;
border-radius: 8rpx;
&--inactive {
background: #2979ff;
}
.nav-manual-input-text {
font-size: 32rpx;
color: #ffffff;
white-space: nowrap;
}
}
/* ===== 重置按钮 ===== */
.nav-reset {
padding: 8rpx 0;
.nav-reset-text {
font-size: 32rpx;
color: #fff;
background-color: #2979ff;
padding: 8rpx 24rpx;
border-radius: 8rpx;
white-space: nowrap;
}
}
/* ===== 扫码输入区 ===== */
.scan-section {
padding: 20rpx 24rpx;
background: #fff;
margin-bottom: 16rpx;
.scan-input-wrap {
display: flex;
align-items: center;
gap: 16rpx;
.scan-camera-btn {
width: 72rpx;
height: 72rpx;
display: flex;
align-items: center;
justify-content: center;
background: #f0f7ff;
border-radius: 8rpx;
flex-shrink: 0;
}
}
}
/* ===== 信息展示列表 ===== */
.info-list {
background: #fff;
margin: 0 24rpx 16rpx;
&--no-scan {
margin-top: 20rpx;
}
border-radius: 12rpx;
overflow: hidden;
.info-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 24rpx;
border-bottom: 1rpx solid #f0f0f0;
&--header {
background-color: #ffedd5;
}
&:last-child {
border-bottom: none;
}
.info-label {
font-size: 32rpx;
color: #000;
width: 160rpx;
flex-shrink: 0;
text-align: center;
}
.info-value {
font-size: 32rpx;
color: #000;
flex: 1;
text-align: center;
&--highlight {
color: #2979ff;
font-weight: 500;
}
}
.info-value-input {
flex: 1;
flex-direction: row;
align-items: center;
display: flex;
gap: 8rpx;
::v-deep .u-input__content__field-wrapper__field {
text-align: center !important;
font-size: 40rpx !important;
}
::v-deep .u-textarea {
text-align: center;
}
.scan-camera-btn-sm {
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
background: #f0f7ff;
border-radius: 8rpx;
flex-shrink: 0;
}
}
}
}
/* ===== 自动绑定提示 ===== */
.submit-section {
padding: 40rpx 24rpx;
.auto-bind-tip {
display: flex;
align-items: center;
justify-content: center;
padding: 24rpx;
background: #f0f7ff;
border-radius: 12rpx;
border: 1rpx solid #b3d8ff;
.auto-bind-tip-text {
font-size: 28rpx;
color: #2979ff;
text-align: center;
}
}
}
}
</style>