WMS_PDA/src/pages/warehouse/inventoryCountLocation.vue
liufumingren fd3228eac3 refactor(warehouse): 盘点列表/详情拆分复用组件并新增库位明细子页
- 抽出 inventoryCount-comps/:countModel 纯函数 + 记录卡片/工具栏/汇总面板/搜索面板/明细面板/宫格/添加商品弹窗等

- 新增 pages/warehouse/inventoryCountLocation 库位明细子页(workDataDetail + 扫码预填添加商品 + 左滑删除记录)

- apis.js 补 result / workData / workDataDetail / recordDelete / addDetail / addLocationDetail 别名

- pages.json 注册新页面
2026-10-05 02:47:24 +08:00

525 lines
16 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="inv-loc-page">
<view class="normar_container">
<page-nav :title="navTitle">
<template #right>
<view v-if="editable" class="nav-more-btn" :class="{ 'nav-more-btn--disabled': submitting }" @click="showActionsPopup = true">
<u-icon name="more-dot-fill" size="40rpx" color="#303133"></u-icon>
</view>
</template>
</page-nav>
<!-- 扫码/输入商品条码 → 直接打开「添加商品记录」弹窗 -->
<count-search-panel
v-if="editable"
v-model="scanKeyword"
placeholder="扫描/输入商品条码,点「添加」记入本库位"
action-text="添加"
@search="handleKeywordAdd"
@clear="handleKeywordClear"
@scan="handleCameraScan"
/>
<scroll-view
class="inv-loc-scroll"
scroll-y
refresher-enabled
:refresher-triggered="refreshing"
refresher-background="#f8f9fa"
@refresherrefresh="handleRefresh"
@refresherrestore="handleRefresherRestore"
>
<view class="inv-loc-card">
<view class="inv-loc-card__row">
<text class="inv-loc-card__label">盘点单号</text>
<text class="inv-loc-card__value">{{ checkNo || '--' }}</text>
</view>
<view class="inv-loc-card__row">
<text class="inv-loc-card__label">库位</text>
<text class="inv-loc-card__value">{{ locationCode || '--' }}</text>
</view>
<view v-if="showLocationName" class="inv-loc-card__row">
<text class="inv-loc-card__label">库位名称</text>
<text class="inv-loc-card__value">{{ locationName }}</text>
</view>
<view class="inv-loc-card__row">
<text class="inv-loc-card__label">盘点明细</text>
<text class="inv-loc-card__value">#{{ detailId || '--' }}</text>
</view>
</view>
<view v-if="loading && !row" class="inv-loc-tip">加载中...</view>
<view v-if="!editable" class="inv-loc-tip">当前盘点任务状态不可作业,仅可查看</view>
<count-location-detail :row="row" :editable="editable" @delete="handleRecordDelete" />
<view v-if="!row && !loading" class="inv-loc-tip">未获取到库位明细数据,请返回列表重试</view>
<view class="safe-area-bottom"></view>
</scroll-view>
</view>
<!-- 右上角「...」菜单(标记空库位收在这里) -->
<count-actions-popup
:show="showActionsPopup"
title="库位操作"
:can-mark-empty="editable"
:submitting="submitting"
@close="showActionsPopup = false"
@mark-empty="handleMenuMarkEmpty"
/>
<!-- 添加商品记录(stock.check.report) -->
<count-record-add-popup
ref="recordAddPopup"
:show="showRecordAdd"
:detail-id="detailId"
:location="locationCode"
:store-id="storeId"
:prefill-keyword="pendingBarcode"
@close="closeAddPopup"
@confirm="handleRecordAddConfirm"
/>
</view>
</template>
<script>
import CountSearchPanel from './inventoryCount-comps/CountSearchPanel.vue';
import CountLocationDetail from './inventoryCount-comps/CountLocationDetail.vue';
import CountRecordAddPopup from './inventoryCount-comps/CountRecordAddPopup.vue';
import CountActionsPopup from './inventoryCount-comps/CountActionsPopup.vue';
import {
normalizeScanResult,
extractObjectPayload,
normalizeLocationWorkDetail,
buildReportParams
} from './inventoryCount-comps/countModel.js';
/** query 参数解码(H5 可能已解码,容错处理非法百分号) */
function decodeParam(value) {
const raw = String(value === undefined || value === null ? '' : value);
if (!raw) return '';
try {
return decodeURIComponent(raw);
} catch (error) {
return raw;
}
}
/**
* 库位盘点明细页(从盘点作业宫格列表点击/扫码进入)
* 入参:detail_id(盘点明细ID,必需,兼容 id/detailId 别名)+ task_id(盘点任务ID,上报用)
* + check_no / store_id / status(展示与搜索用,可选)
* 数据:stock.check.workDataDetail(仅传 detail_id 即可拿到该库位的单行作业数据)
* 能力:查看该库位状态与商品记录 + 添加商品记录 / 标记空库位(check.report)
*/
export default {
components: {
CountSearchPanel,
CountLocationDetail,
CountRecordAddPopup,
CountActionsPopup
},
data() {
return {
taskId: '',
detailId: '',
checkNo: '',
storeId: null,
taskStatus: '',
row: null,
loading: false,
refreshing: false,
scanKeyword: '',
showRecordAdd: false,
showActionsPopup: false,
pendingBarcode: '',
submitting: false
};
},
computed: {
navTitle() {
return this.row && this.row.title ? this.row.title : '库位明细';
},
// 仅 approved/checking 可作业(未传 status 时先按可作业处理,由接口 400 兜底)
editable() {
if (!this.taskStatus) return true;
return this.taskStatus === 'approved' || this.taskStatus === 'checking';
},
showLocationName() {
return !!this.locationName && this.locationName !== this.locationCode;
},
// 库位编码/名称直接从明细数据取(接口不再需要列表页传)
locationCode() {
return String((this.row && this.row.location) || '');
},
locationName() {
return String((this.row && this.row.locationName) || '');
}
},
onLoad(options = {}) {
// 明细ID:std 字段为 detail_id,兼容 id / detailId 别名(页面只需这一个 id 即可拉明细)
this.detailId = String(options.detail_id || options.detailId || options.id || '').trim();
// 任务ID:check.report 需要;兼容 id 与 task_id 两种写法
this.taskId = String(options.task_id || options.taskId || options.id || '').trim();
this.checkNo = decodeParam(options.check_no || options.checkNo);
this.taskStatus = decodeParam(options.status).trim().toLowerCase();
const storeId = options.store_id === undefined || options.store_id === null ? '' : String(options.store_id);
this.storeId = storeId === '' || storeId === 'null' || storeId === 'undefined' ? null : storeId;
this.loadRow();
this.syncBroadcastWithMode();
},
onShow() {
this.syncBroadcastWithMode();
},
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(`[inventoryCountLocation] broadcast/${action} 调用失败:`, error));
}
} catch (error) {
console.warn(`[inventoryCountLocation] broadcast/${action} 调用异常:`, error);
}
},
syncBroadcastWithMode() {
if (this.editable) {
this.safeDispatchBroadcast('setCallbacks', { onResult: this.handleBroadcastResult });
this.safeDispatchBroadcast('start');
} else {
this.safeDispatchBroadcast('stop');
}
},
handleBroadcastResult(result) {
if (!this.editable) return;
const code = normalizeScanResult(result);
if (!code) return;
this.handleScanKeyword(code);
},
handleCameraScan() {
if (!this.editable) return;
uni.scanCode({
onlyFromCamera: true,
scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'],
success: (res) => {
const code = normalizeScanResult(res);
if (code) this.handleScanKeyword(code);
},
fail: () => {}
});
},
/** 扫码回填:弹窗已打开时直接带码查询,便于连续扫码录入 */
handleScanKeyword(code) {
const keyword = String(code || '').trim();
if (!keyword || !this.editable) return;
this.scanKeyword = keyword;
if (this.showRecordAdd) {
const popup = this.$refs.recordAddPopup;
if (popup && typeof popup.applyKeyword === 'function') {
popup.applyKeyword(keyword);
}
return;
}
this.openAddPopup(keyword);
},
handleKeywordAdd() {
if (!this.editable) return;
const keyword = String(this.scanKeyword || '').trim();
if (!keyword) {
uni.showToast({ title: '请输入或扫描商品条码', icon: 'none' });
return;
}
this.handleScanKeyword(keyword);
},
handleKeywordClear() {
this.scanKeyword = '';
},
// ===== 添加商品记录 / 标记空库位 =====
openAddPopup(barcode) {
this.pendingBarcode = String(barcode || '').trim();
this.showRecordAdd = true;
},
closeAddPopup() {
this.showRecordAdd = false;
this.pendingBarcode = '';
},
// 右上角菜单:标记空库位(先关菜单再确认)
handleMenuMarkEmpty() {
this.showActionsPopup = false;
this.handleMarkEmpty();
},
handleMarkEmpty() {
if (!this.detailId) {
uni.showToast({ title: '缺少盘点明细ID', icon: 'none' });
return;
}
uni.showModal({
title: '标记空库位',
content: `确认库位「${this.locationCode || this.detailId}」为空?该库位已有的商品记录会被清除。`,
confirmText: '确认',
cancelText: '取消',
success: async (res) => {
if (!res.confirm) return;
await this.submitReport({ isEmpty: true }, '已标记空库位');
}
});
},
handleRecordAddConfirm(payload) {
// 提交成功后收起抽屉并清空表单;失败时保留输入便于重试
if (!payload || !payload.detailId) return;
this.submitReport(payload, '已添加', { closePopup: true });
},
/** 清空添加弹窗的输入框(搜索词/选中项/数量) */
resetAddPopupForm() {
const popup = this.$refs.recordAddPopup;
if (popup && typeof popup.resetForm === 'function') {
popup.resetForm();
}
},
/** 删除单条商品记录(stock.check.recordDelete,按 record_id) */
handleRecordDelete(child) {
if (!this.editable) return;
const recordId = child && child.recordId;
if (!recordId) {
uni.showToast({ title: '该记录不支持删除', icon: 'none' });
return;
}
if (!this.taskId) {
uni.showToast({ title: '缺少盘点任务ID', icon: 'none' });
return;
}
uni.showModal({
title: '删除商品记录',
content: `确认删除「${child.title || '该商品'}」本条记录?`,
confirmText: '删除',
confirmColor: '#f56c6c',
cancelText: '取消',
success: async (res) => {
if (!res.confirm) return;
await this.removeRecords([recordId]);
}
});
},
/** 调用 recordDelete 并刷新当前库位明细 */
async removeRecords(recordIds) {
if (!this.editable || !this.taskId) return false;
const ids = (Array.isArray(recordIds) ? recordIds : [recordIds])
.map(id => Number(id))
.filter(id => Number.isFinite(id) && id > 0);
if (!ids.length) return false;
if (this.submitting) return false;
this.submitting = true;
try {
const response = await this.$http('check.recordDelete', {
id: Number(this.taskId),
record_ids: ids
});
if (response?.flag === 'failure') {
uni.showToast({ title: response.message || '删除失败', icon: 'none' });
return false;
}
const payload = extractObjectPayload(response);
const deleted = Number(payload.deleted_count);
const skipped = Array.isArray(payload.skipped_ids) ? payload.skipped_ids.length : 0;
let title = '已删除';
if (Number.isFinite(deleted)) title = `已删除 ${deleted} 条`;
if (skipped) title += `,跳过 ${skipped} 条`;
uni.showToast({ title, icon: 'none', duration: 1600 });
await this.loadRow();
return true;
} catch (error) {
console.error('[inventoryCountLocation] 删除作业记录失败:', error);
uni.showToast({ title: '删除失败', icon: 'none' });
return false;
} finally {
this.submitting = false;
}
},
/** 统一上报(check.report):添加商品记录 / 标记空库位 */
async submitReport(payload, successText, options = {}) {
if (!this.editable || !this.taskId) return false;
if (!payload || !payload.detailId) {
uni.showToast({ title: '缺少盘点明细ID', icon: 'none' });
return false;
}
const isEmpty = !!payload.isEmpty;
const quantity = Number(payload.quantity);
if (!isEmpty && (!Number.isInteger(quantity) || quantity < 0)) {
uni.showToast({ title: '实盘数量须为不小于0的整数', icon: 'none' });
return false;
}
if (this.submitting) return false;
this.submitting = true;
try {
const response = await this.$http('check.report', buildReportParams(this.taskId, {
detailId: payload.detailId,
location: payload.location,
quantity,
stockId: payload.stockId,
barcode: payload.barcode,
itemName: payload.itemName,
isEmpty
}));
if (response?.flag === 'failure') {
uni.showToast({ title: response.message || '上报失败', icon: 'none' });
return false;
}
uni.showToast({ title: successText || '已记录', icon: 'success', duration: 600 });
await this.loadRow();
// 提交成功后清空表单并收起底部抽屉
if (options.closePopup) {
this.resetAddPopupForm();
this.closeAddPopup();
}
return true;
} catch (error) {
console.error('[inventoryCountLocation] 盘点上报失败:', error);
uni.showToast({ title: '盘点上报失败', icon: 'none' });
return false;
} finally {
this.submitting = false;
}
},
// ===== 下拉刷新 =====
async handleRefresh() {
// 防重入:同一手势可能被重复触发
if (this.refreshing) return;
this.refreshing = true;
try {
await this.loadRow();
} finally {
this.refreshing = false;
}
},
handleRefresherRestore() {
this.refreshing = false;
},
// ===== 数据加载(stock.check.workDataDetail,只需传 detail_id)=====
async loadRow() {
if (!this.detailId) {
uni.showToast({ title: '缺少盘点明细ID', icon: 'none' });
return null;
}
this.loading = true;
try {
const response = await this.$http('check.workDataDetail', {
detail_id: Number(this.detailId)
});
if (response?.flag === 'failure') {
// 状态不允许(如已完成)时后端返回 400,按只读展示
this.taskStatus = '';
this.row = null;
uni.showToast({ title: response.message || '明细加载失败', icon: 'none' });
return null;
}
const row = normalizeLocationWorkDetail(extractObjectPayload(response));
this.row = row;
return row;
} catch (error) {
console.error('[inventoryCountLocation] 库位明细加载失败:', error);
uni.showToast({ title: '库位明细加载失败', icon: 'none' });
return null;
} finally {
this.loading = false;
}
}
}
};
</script>
<style lang="scss" scoped>
.inv-loc-page {
height: 100vh;
background: #f8f9fa;
}
.normar_container {
height: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
}
.inv-loc-scroll {
flex: 1;
min-height: 0;
height: auto;
padding: 20rpx 0 0;
box-sizing: border-box;
}
.inv-loc-card {
margin: 0 12rpx 16rpx;
padding: 8rpx 28rpx;
background: #ffffff;
border: 2rpx solid #ebedf2;
border-radius: 20rpx;
box-shadow: 0 6rpx 20rpx rgba(31, 45, 61, 0.06);
}
.inv-loc-card__row {
display: flex;
align-items: flex-start;
padding: 14rpx 0;
border-bottom: 1rpx solid #f5f6f8;
&:last-child {
border-bottom: none;
}
}
.inv-loc-card__label {
flex-shrink: 0;
width: 160rpx;
font-size: 28rpx;
color: #8a9099;
}
.inv-loc-card__value {
flex: 1;
min-width: 0;
font-size: 30rpx;
color: #303133;
word-break: break-all;
}
.inv-loc-tip {
margin: 0 12rpx 16rpx;
padding: 24rpx;
background: #fff8e6;
border: 2rpx solid #ffe1a8;
border-radius: 16rpx;
font-size: 28rpx;
color: #b8811a;
text-align: center;
}
.safe-area-bottom {
height: 20rpx;
}
/* 右上角「...」菜单入口 */
.nav-more-btn {
width: 64rpx;
height: 64rpx;
display: flex;
align-items: center;
justify-content: center;
}
.nav-more-btn--disabled {
opacity: 0.45;
pointer-events: none;
}
</style>