WMS_PDA/pages/work/storage/relocation.vue

786 lines
21 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="relocation-page">
<view class="normar_container">
<page-nav title="移库作业"></page-nav>
<scroll-view class="container" scroll-y>
<u-form class="form-box" :model="formData" ref="formRef" label-width="170rpx">
<view class="section">
<u-form-item label="移库模式" required>
<u-radio-group v-model="formData.relocationMode" @change="handleModeChange">
<u-radio v-for="item in relocationModeOptions" :key="item.value" :name="item.value" :label="item.label"></u-radio>
</u-radio-group>
</u-form-item>
</view>
<view class="section">
<view class="section-title">库位信息</view>
<u-form-item label="移出库位" required>
<view class="scan-input-wrap">
<u-input
v-model="formData.sourceSpaceKeyword"
placeholder="请扫描或输入移出库位"
clearable
@focus="setActiveScanField('sourceSpace')"
@blur="handleSpaceBlur('sourceSpace')"
/>
<view class="scan-camera-btn" @click="handleFieldScan('sourceSpace')">
<u-icon name="camera" size="34rpx" color="#2979ff"></u-icon>
</view>
</view>
<text v-if="formData.sourceSpaceName" class="field-tip">已识别:{{ formData.sourceSpaceName }}</text>
</u-form-item>
<u-form-item label="移入库位" required>
<view class="scan-input-wrap">
<u-input
v-model="formData.targetSpaceKeyword"
placeholder="请扫描或输入移入库位"
clearable
@focus="setActiveScanField('targetSpace')"
@blur="handleSpaceBlur('targetSpace')"
/>
<view class="scan-camera-btn" @click="handleFieldScan('targetSpace')">
<u-icon name="camera" size="34rpx" color="#2979ff"></u-icon>
</view>
</view>
<text v-if="formData.targetSpaceName" class="field-tip">已识别:{{ formData.targetSpaceName }}</text>
</u-form-item>
</view>
<view class="section">
<view class="section-title">商品信息</view>
<u-form-item v-if="formData.relocationMode === 'product'" label="商品/条码" required>
<view class="scan-input-wrap">
<u-input
v-model="formData.productKeyword"
placeholder="请扫描商品条码或输入关键字"
clearable
@focus="setActiveScanField('productKeyword')"
@blur="handleProductBlur"
/>
<view class="scan-camera-btn" @click="handleFieldScan('productKeyword')">
<u-icon name="camera" size="34rpx" color="#2979ff"></u-icon>
</view>
</view>
</u-form-item>
<view v-if="formData.relocationMode === 'product' && selectedStock" class="stock-card">
<view class="stock-card__header">
<text class="stock-card__title">{{ getStockName(selectedStock) }}</text>
<u-tag text="已选中" type="success" size="mini"></u-tag>
</view>
<view class="stock-card__row">
<text class="label">条码:</text>
<text class="value">{{ getStockBarcode(selectedStock) }}</text>
</view>
<view class="stock-card__row">
<text class="label">当前库位:</text>
<text class="value">{{ getStockSpace(selectedStock) }}</text>
</view>
<view class="stock-card__row">
<text class="label">可用库存:</text>
<text class="value">{{ getStockQuantity(selectedStock) }}</text>
</view>
</view>
<u-form-item v-if="formData.relocationMode === 'product'" label="移库数量" required>
<view class="quantity-input-wrap">
<u-input
v-model="formData.moveQuantity"
type="number"
placeholder="请输入移库数量"
@input="handleMoveQuantityInput"
@blur="handleMoveQuantityBlur"
></u-input>
<view class="quantity-all-view-btn" @click="handleFillAllQuantity">
<text>全部</text>
</view>
</view>
</u-form-item>
<view v-else class="mode-tip-card">
<text class="mode-tip-card__title">按仓位移库</text>
<text class="mode-tip-card__desc">当前模式下无需选择商品和数量,提交时默认按原库位整体移至目标库位。</text>
</view>
<u-form-item label="备注">
<u-input v-model="formData.remark" type="textarea" placeholder="请输入备注信息" height="120"></u-input>
</u-form-item>
</view>
</u-form>
</scroll-view>
<view class="submit-area">
<u-button type="primary" :loading="submitting" @click="handleSubmit">确认移库</u-button>
<u-button @click="handleReset">重置</u-button>
</view>
</view>
<u-popup :show="selectorVisible" mode="bottom" :closeable="true" border-radius="20" @close="selectorVisible = false">
<view class="selector-popup">
<view class="popup-header">
<text class="popup-title">请选择{{ selectorType === 'stock' ? '商品库存' : '库位' }}</text>
</view>
<scroll-view class="popup-list" scroll-y>
<view
v-for="(item, index) in selectorList"
:key="item.id || item.space_id || index"
class="popup-item"
@click="handleSelectorConfirm(item)"
>
<template v-if="selectorType === 'stock'">
<text class="popup-item__title">{{ getStockName(item) }}</text>
<text class="popup-item__desc">条码:{{ getStockBarcode(item) }}</text>
<text class="popup-item__desc">库位:{{ getStockSpace(item) }}</text>
</template>
<template v-else>
<text class="popup-item__title">{{ getSpaceName(item) }}</text>
<text class="popup-item__desc">编码:{{ getSpaceCode(item) }}</text>
</template>
</view>
</scroll-view>
</view>
</u-popup>
<u-loading-page :loading="loading" bg-color="rgba(255,255,255,0.9)"></u-loading-page>
</view>
</template>
<script>
export default {
data() {
return {
loading: false,
submitting: false,
activeScanField: 'productKeyword',
relocationModeOptions: [
{ label: '按商品', value: 'product' },
{ label: '按仓位', value: 'space' }
],
selectorVisible: false,
selectorType: '',
selectorField: '',
selectorList: [],
selectedStock: null,
formData: {
relocationMode: 'product',
sourceSpaceKeyword: '',
sourceSpaceId: null,
sourceSpaceName: '',
targetSpaceKeyword: '',
targetSpaceId: null,
targetSpaceName: '',
productKeyword: '',
moveQuantity: '',
remark: ''
}
}
},
onLoad() {
this.safeDispatchBroadcast('setCallbacks', {
onResult: this.handleBroadcastResult
});
this.safeDispatchBroadcast('start');
},
onShow() {
this.safeDispatchBroadcast('setCallbacks', {
onResult: this.handleBroadcastResult
});
this.safeDispatchBroadcast('resume');
},
onHide() {
this.safeDispatchBroadcast('pause');
},
onUnload() {
this.safeDispatchBroadcast('stop');
},
methods: {
safeDispatchBroadcast(action, payload) {
try {
const task = this.$store.dispatch(`broadcast/${action}`, payload);
if (task && typeof task.catch === 'function') {
task.catch(error => {
console.warn(`[Relocation] broadcast/${action} 调用失败:`, error);
});
}
} catch (error) {
console.warn(`[Relocation] broadcast/${action} 调用异常:`, error);
}
},
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 '';
},
setActiveScanField(field) {
this.activeScanField = field;
},
handleModeChange(mode) {
this.formData.relocationMode = mode;
if (mode === 'space') {
this.selectedStock = null;
this.formData.productKeyword = '';
this.formData.moveQuantity = '';
this.activeScanField = 'sourceSpace';
return;
}
this.activeScanField = 'productKeyword';
},
handleBroadcastResult(result) {
const rawCode = this.normalizeUniScanResult(result);
if (!rawCode) {
return;
}
this.applyScanResult(rawCode, this.activeScanField || 'productKeyword');
},
handleFieldScan(field) {
this.setActiveScanField(field);
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.applyScanResult(rawCode, field);
},
fail: (error) => {
const errMsg = String(error?.errMsg || '').toLowerCase();
if (errMsg.includes('cancel')) {
return;
}
uni.showToast({
title: '扫码失败,请重试',
icon: 'none'
});
}
});
},
applyScanResult(rawCode, field) {
if (field === 'sourceSpace' || field === 'targetSpace') {
const keywordKey = field === 'sourceSpace' ? 'sourceSpaceKeyword' : 'targetSpaceKeyword';
this.formData[keywordKey] = rawCode;
this.searchSpace(field, rawCode);
return;
}
this.formData.productKeyword = rawCode;
this.searchStock(rawCode);
},
handleSpaceBlur(field) {
const keyword = field === 'sourceSpace' ? this.formData.sourceSpaceKeyword : this.formData.targetSpaceKeyword;
if (keyword) {
this.searchSpace(field, keyword);
}
},
handleProductBlur() {
if (this.formData.productKeyword) {
this.searchStock(this.formData.productKeyword);
}
},
async searchSpace(field, keyword) {
const rawKeyword = String(keyword || '').trim();
if (!rawKeyword) {
return;
}
this.loading = true;
try {
const res = await this.$http('common.searchSpace', {
keyword: rawKeyword
});
if (res.code !== 200) {
throw new Error(res.message || '库位搜索失败');
}
const list = Array.isArray(res.data) ? res.data : [];
if (Number(res.total || list.length) === 1 && list[0]) {
this.applySpaceSelection(field, list[0]);
return;
}
if (list.length > 1) {
this.selectorType = 'space';
this.selectorField = field;
this.selectorList = list;
this.selectorVisible = true;
return;
}
uni.showToast({
title: '未找到匹配库位',
icon: 'none'
});
} catch (error) {
console.error('搜索库位失败:', error);
uni.showToast({
title: '库位查询失败',
icon: 'none'
});
} finally {
this.loading = false;
}
},
applySpaceSelection(field, item) {
const isSource = field === 'sourceSpace';
this.formData[isSource ? 'sourceSpaceId' : 'targetSpaceId'] = item?.id || item?.space_id || null;
this.formData[isSource ? 'sourceSpaceName' : 'targetSpaceName'] = this.getSpaceName(item);
this.formData[isSource ? 'sourceSpaceKeyword' : 'targetSpaceKeyword'] = this.getSpaceCode(item);
},
async searchStock(keyword) {
const rawKeyword = String(keyword || '').trim();
if (!rawKeyword) {
return;
}
this.loading = true;
try {
const res = await this.$http('stock.warehouse.search', {
keyword: rawKeyword
});
if (res.code !== 200) {
throw new Error(res.message || '库存查询失败');
}
const list = Array.isArray(res.data) ? res.data : [];
if (Number(res.total || list.length) === 1 && list[0]) {
this.applyStockSelection(list[0]);
return;
}
if (list.length > 1) {
this.selectorType = 'stock';
this.selectorField = 'stock';
this.selectorList = list;
this.selectorVisible = true;
return;
}
this.selectedStock = null;
uni.showToast({
title: '未找到匹配库存',
icon: 'none'
});
} catch (error) {
console.error('搜索库存失败:', error);
uni.showToast({
title: '库存查询失败',
icon: 'none'
});
} finally {
this.loading = false;
}
},
applyStockSelection(item) {
this.selectedStock = item;
this.formData.productKeyword = this.getStockBarcode(item) || this.getStockName(item);
if (!this.formData.moveQuantity) {
const quantity = this.getStockQuantity(item);
this.formData.moveQuantity = quantity && quantity !== '--' ? String(quantity) : '';
}
},
handleSelectorConfirm(item) {
if (this.selectorType === 'stock') {
this.applyStockSelection(item);
} else {
this.applySpaceSelection(this.selectorField, item);
}
this.selectorVisible = false;
},
getSpaceName(item) {
return item?.name || item?.space_name || item?.title || item?.barcode || item?.code || '未命名库位';
},
getSpaceCode(item) {
return item?.barcode || item?.code || item?.space_code || item?.name || '';
},
getStockName(item) {
return item?.item_name || item?.product_name || item?.name || item?.goods_name || '未命名商品';
},
getStockBarcode(item) {
if (Array.isArray(item?.barcodes) && item.barcodes.length) {
return item.barcodes.filter(Boolean).join(';');
}
return item?.barcode || item?.goods_code || item?.item_code || '--';
},
getStockSpace(item) {
return item?.space?.name || item?.space_name || item?.space_barcode || item?.space?.barcode || '--';
},
getStockQuantity(item) {
return item?.available_quantity || item?.actual_quantity || item?.quantity || item?.stock || '--';
},
getAvailableStockNumber() {
if (!this.selectedStock) {
return 0;
}
const quantity = this.getStockQuantity(this.selectedStock);
const normalizedQuantity = Number(String(quantity).replace(/,/g, '').trim());
return Number.isFinite(normalizedQuantity) && normalizedQuantity > 0 ? normalizedQuantity : 0;
},
normalizeMoveQuantity(value) {
const sanitized = String(value || '').replace(/\D/g, '');
if (!sanitized) {
return '';
}
return sanitized.replace(/^0+(?=\d)/, '');
},
handleMoveQuantityInput(value) {
this.formData.moveQuantity = this.normalizeMoveQuantity(value);
},
handleMoveQuantityBlur() {
this.formData.moveQuantity = this.normalizeMoveQuantity(this.formData.moveQuantity);
this.validateMoveQuantity(true);
},
validateMoveQuantity(showToast = false) {
if (this.formData.relocationMode !== 'product') {
return true;
}
const quantity = String(this.formData.moveQuantity || '').trim();
if (!quantity) {
if (showToast) {
uni.showToast({ title: '请输入移库数量', icon: 'none' });
}
return false;
}
if (!/^[1-9]\d*$/.test(quantity)) {
if (showToast) {
uni.showToast({ title: '移库数量需为正整数', icon: 'none' });
}
return false;
}
const maxQuantity = this.getAvailableStockNumber();
if (maxQuantity > 0 && Number(quantity) > maxQuantity) {
if (showToast) {
uni.showToast({ title: '移库数量不能大于可用库存', icon: 'none' });
}
return false;
}
return true;
},
handleFillAllQuantity() {
if (!this.selectedStock) {
uni.showToast({
title: '请先选择移库商品',
icon: 'none'
});
return;
}
const quantity = this.getStockQuantity(this.selectedStock);
if (quantity === '--' || quantity === '' || quantity === null || quantity === undefined) {
uni.showToast({
title: '未获取到可用库存',
icon: 'none'
});
return;
}
this.formData.moveQuantity = String(quantity);
},
async handleSubmit() {
if (!this.formData.sourceSpaceKeyword || !this.formData.sourceSpaceName) {
uni.showToast({ title: '请先识别移出库位', icon: 'none' });
return;
}
if (!this.formData.targetSpaceKeyword || !this.formData.targetSpaceName) {
uni.showToast({ title: '请先识别移入库位', icon: 'none' });
return;
}
if (this.formData.relocationMode === 'product' && !this.selectedStock) {
uni.showToast({ title: '请选择移库商品', icon: 'none' });
return;
}
if (this.formData.relocationMode === 'product' && !this.validateMoveQuantity(true)) {
return;
}
if (this.formData.sourceSpaceId && this.formData.targetSpaceId && this.formData.sourceSpaceId === this.formData.targetSpaceId) {
uni.showToast({ title: '移出库位和移入库位不能相同', icon: 'none' });
return;
}
uni.showModal({
title: '提示',
content: `当前项目暂未配置${this.formData.relocationMode === 'product' ? '按商品' : '按仓位'}移库提交接口,页面和扫码流程已接入。`,
showCancel: false
});
},
handleReset() {
this.formData = {
relocationMode: 'product',
sourceSpaceKeyword: '',
sourceSpaceId: null,
sourceSpaceName: '',
targetSpaceKeyword: '',
targetSpaceId: null,
targetSpaceName: '',
productKeyword: '',
moveQuantity: '',
remark: ''
};
this.selectedStock = null;
this.selectorVisible = false;
this.selectorType = '';
this.selectorField = '';
this.selectorList = [];
this.activeScanField = 'productKeyword';
}
}
}
</script>
<style lang="scss" scoped>
.relocation-page {
min-height: 100vh;
background: #f8f8f8;
}
.container {
flex: 1;
height: 100rpx;
}
.form-box {
padding: 20rpx;
}
.section {
background-color: #fff;
border-radius: 16rpx;
padding: 30rpx;
margin-bottom: 30rpx;
.section-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
margin-bottom: 30rpx;
padding-bottom: 20rpx;
border-bottom: 2rpx solid #f0f0f0;
}
}
.scan-input-wrap {
display: flex;
align-items: center;
gap: 16rpx;
width: 100%;
::v-deep .u-input {
flex: 1;
}
}
.quantity-input-wrap {
display: flex;
align-items: center;
gap: 16rpx;
width: 100%;
::v-deep .u-input {
flex: 1;
}
}
.quantity-all-view-btn {
width: 76rpx;
height: 76rpx;
border-radius: 12rpx;
background: #2979ff;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
box-shadow: 0 4rpx 12rpx rgba(41, 121, 255, 0.18);
text {
font-size: 22rpx;
font-weight: 500;
color: #ffffff;
line-height: 1;
}
&:active {
opacity: 0.85;
}
}
.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;
}
}
.field-tip {
display: block;
margin-top: 12rpx;
font-size: 24rpx;
color: #909399;
}
.mode-tip-card {
margin: 10rpx 0 24rpx;
padding: 24rpx;
border-radius: 12rpx;
background: #f6fdf8;
border: 1rpx solid #d7f0df;
&__title {
display: block;
font-size: 30rpx;
font-weight: 600;
color: #2e7d32;
margin-bottom: 10rpx;
}
&__desc {
display: block;
font-size: 24rpx;
line-height: 1.6;
color: #606266;
}
}
.stock-card {
margin: 10rpx 0 24rpx;
padding: 24rpx;
border-radius: 12rpx;
background: #f8fbff;
border: 1rpx solid #dbeafe;
&__header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14rpx;
}
&__title {
font-size: 30rpx;
font-weight: 600;
color: #303133;
flex: 1;
padding-right: 20rpx;
}
&__row {
display: flex;
margin-top: 10rpx;
.label {
width: 140rpx;
font-size: 26rpx;
color: #606266;
}
.value {
flex: 1;
font-size: 26rpx;
color: #303133;
word-break: break-all;
}
}
}
.submit-area {
display: flex;
flex-direction: row;
padding: 20rpx;
gap: 20rpx;
border-top: 1px solid rgba(0, 0, 0, .08);
background-color: #fff;
}
.selector-popup {
padding-bottom: env(safe-area-inset-bottom);
.popup-header {
padding: 40rpx 30rpx 20rpx;
border-bottom: 1rpx solid #f0f0f0;
}
.popup-title {
font-size: 36rpx;
font-weight: 600;
color: #333;
}
.popup-list {
max-height: 700rpx;
}
.popup-item {
padding: 28rpx 30rpx;
border-bottom: 1rpx solid #f5f5f5;
&__title {
display: block;
font-size: 30rpx;
font-weight: 600;
color: #303133;
margin-bottom: 10rpx;
}
&__desc {
display: block;
font-size: 24rpx;
color: #909399;
line-height: 1.6;
}
}
}
</style>