pda-cli/src/pages/storegoods/storeGoodsPurchaseInMixin.js
HongxuanG 470a474d70 fix(storegoods): 修复重扫仓位时旧码拼接问题
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 09:44:48 +08:00

637 lines
20 KiB
JavaScript
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.

/**
* 成品采购进仓单表单 mixin(新增/编辑共用)
*
* 1. 先填表头(供应商 / 仓库 / 营销体系 / 订单类型 / 备注)提交 add,拿到 id 后可扫码
* 2. 先扫仓位(getWarehouseBin:传 qr_code + warehouse_id),校验后扫布匹二维码
* 3. 扫码走 updateFpmPrcInOrder(arrange_type: 1 录入 / 3 删除,带 warehouse_bin_id、纸筒/包装重量)
* 4. 手机扫码成功展示详情后自动再次调起扫码,形成连续扫
*/
import util from '@/common/util';
import scanMixin from '@/common/scanMixin.js';
import { initTts, speak } from '@/common/tts.js';
import {
AUDIT_STATUS_MAP,
ARRANGE_TYPE,
BUSINESS_UNIT_TYPE_PRODUCT_SUPPLIER,
WAREHOUSE_TYPE_FINISHED,
IN_ORDER_TYPE_PURCHASE,
IN_ORDER_TYPE_MAP,
SALE_MODE,
SALE_MODE_MAP,
SALE_MODE_OPTIONS,
DEFAULT_PAPER_TUBE_WEIGHT,
DEFAULT_PACKAGE_WEIGHT,
normalizeOrder,
mapToSelectOptions,
canScanAtStatus,
isLockedOrderStatus,
getFormStatusActionButtons,
orderToFormFields,
confirmOrderStatusAction,
DRAFT_AUDIT_STATUS,
DRAFT_AUDIT_STATUS_NAME,
getEmptyScanDetail,
buildScanDetailFromScanResponse,
buildScanUpdateParams,
pickDefaultWarehouse,
validateScanWeights,
} from '@/common/storeGoodsPurchaseIn';
export default {
mixins: [scanMixin],
data() {
return {
pageMode: 'add',
orderId: 0,
form: this.getEmptyForm(),
headerSaved: false,
selectShow: false,
selectList: [],
selectType: '',
canScan: false,
QRBarCode: '',
BarCodeDelStatus: false,
BillDataMessage: '',
scanDetail: getEmptyScanDetail(),
auditStatus: 0,
supplierOptions: [],
warehouseOptions: [],
saleSystemOptions: [],
saleModeOptions: SALE_MODE_OPTIONS,
dropdownLoading: false,
/** 仓位 */
warehouseBinId: 0,
warehouseBinCode: '',
warehouseBinName: '',
/** 手机连续扫码开关:成功后自动再调起;取消/失败则关闭 */
phoneContinuousScan: false,
/** 手机扫当前目标:bin | qr */
phoneScanTarget: 'bin',
};
},
computed: {
isReadonly() {
return this.pageMode === 'view';
},
isEditable() {
return this.pageMode === 'add' || this.pageMode === 'edit';
},
headerEditable() {
return this.pageMode === 'add' && !this.headerSaved;
},
/** 纸筒/包装重量随扫码提交,保存前后及可扫编辑态均可改 */
canEditWeights() {
return this.isEditable && (this.headerEditable || this.canScan);
},
statusActionButtons() {
return getFormStatusActionButtons(this.headerSaved, this.orderId, this.auditStatus);
},
displayAuditStatus() {
if (this.pageMode === 'add' && !this.headerSaved) return DRAFT_AUDIT_STATUS;
return this.auditStatus;
},
displayAuditStatusName() {
if (this.pageMode === 'add' && !this.headerSaved) return DRAFT_AUDIT_STATUS_NAME;
return AUDIT_STATUS_MAP[this.auditStatus] || '';
},
/** 录入需先选仓位;删除模式仅需单据可扫 */
canScanQr() {
if (!this.canScan) return false;
if (this.BarCodeDelStatus) return true;
return !!this.warehouseBinId;
},
/** 扫码栏在可扫状态下始终展示(含删除勾选) */
showQrScanBar() {
return this.canScan;
},
qrDisabledTip() {
if (!this.canScan) return '请先提交保存后再扫码';
if (!this.warehouseBinId && !this.BarCodeDelStatus) return '请先扫描仓位后再扫布匹二维码';
return '当前状态不可扫码';
},
},
onShow() {
this.isPageActive = true;
initTts();
if (this.canScan && this.isEditable) {
this.registerOrderScanBroadcast();
}
},
onHide() {
this.isPageActive = false;
this.phoneContinuousScan = false;
this.unregisterScanBroadcast();
},
onUnload() {
this.isPageActive = false;
this.phoneContinuousScan = false;
this.unregisterScanBroadcast();
},
methods: {
clearWarehouseBin() {
this.warehouseBinId = 0;
this.warehouseBinCode = '';
this.warehouseBinName = '';
this.phoneContinuousScan = false;
},
/** 重扫仓位:先清空输入框再填入,避免残留旧码拼接 */
applyWarehouseBinScan(scanResult) {
const code = String(scanResult || '').trim().replace(/[\r\n]/g, '');
if (!code) return;
this.warehouseBinCode = '';
this.$nextTick(() => {
this.warehouseBinCode = code;
this.$nextTick(() => this.resolveWarehouseBin());
});
},
handleBinPhoneScan() {
if (!this.canScan || !this.orderId) {
this.showError('请先提交保存后再扫码');
return;
}
this.phoneContinuousScan = false;
this.phoneScanTarget = 'bin';
this.openPhoneScan({
callback: (scanResult) => {
this.applyWarehouseBinScan(scanResult);
},
fail: () => {},
});
},
handlePhoneScan() {
if (!this.canScan || !this.orderId) {
this.showError('请先提交保存后再扫码');
return;
}
// 删除模式可直接扫布匹码;录入模式无仓位时先扫仓位
if (!this.BarCodeDelStatus && !this.warehouseBinId) {
this.handleBinPhoneScan();
return;
}
this.phoneScanTarget = 'qr';
this.phoneContinuousScan = true;
this.openContinuousPhoneScan();
},
openContinuousPhoneScan() {
if (!this.phoneContinuousScan || !this.canScanQr || !this.isPageActive) {
this.phoneContinuousScan = false;
return;
}
this.openPhoneScan({
callback: (scanResult) => {
this.QRBarCode = String(scanResult || '').trim().replace(/[\r\n]/g, '');
this.$nextTick(() => this.handleScan({ fromPhone: true }));
},
fail: () => {
this.phoneContinuousScan = false;
},
});
},
scheduleNextPhoneScan() {
if (!this.phoneContinuousScan || !this.canScanQr || !this.isPageActive) {
this.phoneContinuousScan = false;
return;
}
setTimeout(() => {
this.openContinuousPhoneScan();
}, 400);
},
registerOrderScanBroadcast() {
this.registerScanBroadcast((scanResult) => {
const code = String(scanResult || '').trim().replace(/[\r\n]/g, '');
if (!code) return;
// 删除模式:直接按布匹码删除;未扫仓位时枪扫优先解析为仓位
if (!this.BarCodeDelStatus && !this.warehouseBinId) {
this.applyWarehouseBinScan(code);
return;
}
this.QRBarCode = code;
this.$nextTick(() => this.handleScan());
});
},
resolveWarehouseBin() {
const code = (this.warehouseBinCode || '').trim();
if (!this.canScan || !this.orderId) {
this.showError('请先提交保存后再扫码');
return;
}
if (!code) return;
if (!this.form.warehouse_id) {
this.showError('单据缺少仓库信息');
return;
}
uni.showLoading({ title: '识别仓位...' });
return this.$u.api.warehouseBin.getByQrCode({
qr_code: code,
warehouse_id: Number(this.form.warehouse_id),
})
.then((data) => {
uni.hideLoading();
if (!data || !data.id) {
throw new Error('未识别到仓位');
}
// 返回 physical_warehouse_id,与单据仓库校验
if (
data.physical_warehouse_id
&& String(data.physical_warehouse_id) !== String(this.form.warehouse_id)
) {
this.warehouseBinId = 0;
this.warehouseBinName = '';
throw new Error('该仓位不属于当前仓库');
}
this.warehouseBinId = data.id;
this.warehouseBinCode = data.code || data.qr_code || code;
this.warehouseBinName = data.name || '';
this.BillDataMessage = '';
speak('仓位成功');
uni.showToast({ title: '仓位识别成功', icon: 'success' });
})
.catch((e) => {
uni.hideLoading();
this.warehouseBinId = 0;
this.warehouseBinCode = '';
this.warehouseBinName = '';
this.showError(e.message || '仓位识别失败');
});
},
syncCanScanFromStatus(status) {
const nextCanScan = this.isEditable && canScanAtStatus(status);
if (this.canScan && !nextCanScan) {
this.phoneContinuousScan = false;
this.unregisterScanBroadcast();
}
this.canScan = nextCanScan;
},
loadBaseDropdowns() {
if (this.dropdownLoading) return Promise.resolve();
this.dropdownLoading = true;
if (this.headerEditable) this.applyDefaultSaleSystem();
return Promise.all([
this.ensureSupplierOptions().catch((e) => {
console.error('加载供应商失败', e);
return [];
}),
this.ensureWarehouseOptions().catch((e) => {
console.error('加载仓库失败', e);
return [];
}),
this.ensureSaleSystemOptions().catch((e) => {
console.error('加载营销体系失败', e);
return [];
}),
]).finally(() => {
this.dropdownLoading = false;
});
},
ensureSupplierOptions() {
if (this.supplierOptions.length) return Promise.resolve(this.supplierOptions);
return this.$u.api.businessUnit.list({ unit_type_id: BUSINESS_UNIT_TYPE_PRODUCT_SUPPLIER })
.then((res) => {
this.supplierOptions = mapToSelectOptions(res);
return this.supplierOptions;
});
},
ensureWarehouseOptions() {
if (this.warehouseOptions.length) return Promise.resolve(this.warehouseOptions);
return this.$u.api.physicalWarehouse.getDropdownList({ warehouse_type_id: WAREHOUSE_TYPE_FINISHED })
.then((res) => {
this.warehouseOptions = mapToSelectOptions(res);
if (this.headerEditable && !this.form.warehouse_id) {
this.applyDefaultWarehouse();
}
return this.warehouseOptions;
});
},
ensureSaleSystemOptions() {
if (this.saleSystemOptions.length) return Promise.resolve(this.saleSystemOptions);
return this.$u.api.saleSystem.getDropdownList()
.then((res) => {
this.saleSystemOptions = mapToSelectOptions(res);
if (this.headerEditable) this.applyDefaultSaleSystem();
return this.saleSystemOptions;
});
},
applyDefaultWarehouse() {
const app = getApp();
const picked = pickDefaultWarehouse(
this.warehouseOptions,
app && app.globalData ? app.globalData.StoreNameID : '',
app && app.globalData ? app.globalData.StoreName : '',
);
if (!picked) return;
this.form.warehouse_id = picked.value;
this.form.warehouse_name = picked.label;
},
applyDefaultSaleSystem() {
if (this.form.sale_system_id) {
if (!this.form.sale_system_name && this.saleSystemOptions.length) {
const found = this.saleSystemOptions.find(
(item) => String(item.value) === String(this.form.sale_system_id),
);
if (found) this.form.sale_system_name = found.label;
}
return;
}
const app = getApp();
const gd = app && app.globalData ? app.globalData : {};
const id = gd.PlanDepartmentID;
if (!id) return;
this.form.sale_system_id = id;
this.form.sale_system_name = gd.PlanDepartmentName || '';
if (!this.form.sale_system_name && this.saleSystemOptions.length) {
const found = this.saleSystemOptions.find((item) => String(item.value) === String(id));
if (found) this.form.sale_system_name = found.label;
}
},
getEmptyForm() {
const app = getApp();
const gd = app && app.globalData ? app.globalData : {};
return {
order_no: '',
biz_unit_id: '',
biz_unit_name: '',
warehouse_id: '',
warehouse_name: '',
sale_system_id: gd.PlanDepartmentID || '',
sale_system_name: gd.PlanDepartmentName || '',
sale_mode: SALE_MODE.BULK,
sale_mode_name: SALE_MODE_MAP[SALE_MODE.BULK],
in_order_type: IN_ORDER_TYPE_PURCHASE,
in_order_type_name: IN_ORDER_TYPE_MAP[IN_ORDER_TYPE_PURCHASE],
paper_tube_weight: String(DEFAULT_PAPER_TUBE_WEIGHT),
package_weight: String(DEFAULT_PACKAGE_WEIGHT),
remark: '',
item_data: [],
};
},
loadOrder(id, options = {}) {
const { rejectIfLocked = false } = options;
return this.$u.api.fpmPrcInOrder.detail({ id })
.then((res) => {
const order = normalizeOrder(res);
if (!order) {
this.showError('单据不存在');
return null;
}
if (rejectIfLocked && isLockedOrderStatus(order.audit_status)) {
this.showError('当前状态不可编辑');
setTimeout(() => uni.navigateBack(), 1500);
return null;
}
this.orderId = order.id;
this.auditStatus = order.audit_status;
this.headerSaved = true;
this.form = orderToFormFields(order);
this.clearWarehouseBin();
this.syncCanScanFromStatus(order.audit_status);
if (this.canScan) this.registerOrderScanBroadcast();
return order;
})
.catch((e) => {
this.showError(e.message || '加载单据失败');
return null;
});
},
pickerSelectFun(type) {
if (!this.headerEditable) return;
this.selectType = type;
const openPicker = (list) => {
if (!list.length) {
this.showError('暂无可选数据');
return;
}
this.selectList = list;
this.selectShow = true;
};
if (type === '供应商') {
this.ensureSupplierOptions()
.then(openPicker)
.catch((e) => this.showError(e.message || '加载供应商失败'));
} else if (type === '仓库名称') {
this.ensureWarehouseOptions()
.then(openPicker)
.catch((e) => this.showError(e.message || '加载仓库失败'));
} else if (type === '营销体系') {
this.ensureSaleSystemOptions()
.then(openPicker)
.catch((e) => this.showError(e.message || '加载营销体系失败'));
} else if (type === '订单类型') {
openPicker(this.saleModeOptions);
}
},
selectConfirmFun(e) {
const item = e[0];
if (this.selectType === '供应商') {
this.form.biz_unit_id = item.value;
this.form.biz_unit_name = item.label;
} else if (this.selectType === '仓库名称') {
this.form.warehouse_id = item.value;
this.form.warehouse_name = item.label;
} else if (this.selectType === '营销体系') {
this.form.sale_system_id = item.value;
this.form.sale_system_name = item.label;
} else if (this.selectType === '订单类型') {
this.form.sale_mode = item.value;
this.form.sale_mode_name = item.label;
}
},
validateForm() {
if (!this.form.biz_unit_id) return '请选择供应商';
if (!this.form.warehouse_id) return '请选择仓库名称';
if (!this.form.sale_system_id) return '请选择营销体系';
if (!this.form.sale_mode) return '请选择订单类型';
return '';
},
buildAddParam() {
return {
inOrderType: IN_ORDER_TYPE_PURCHASE,
biz_unit_id: Number(this.form.biz_unit_id),
warehouse_id: Number(this.form.warehouse_id),
sale_system_id: Number(this.form.sale_system_id),
sale_mode: Number(this.form.sale_mode),
remark: this.form.remark || '',
item_data: [],
};
},
submitSave() {
if (this.headerSaved) {
this.showError('表头已保存,请直接扫码');
return;
}
const err = this.validateForm();
if (err) {
this.showError(err);
return;
}
uni.showLoading({ title: '保存中...' });
this.$u.api.fpmPrcInOrder.add(this.buildAddParam())
.then((res) => {
this.orderId = (res && res.id) || 0;
if (!this.orderId) {
throw new Error('保存失败,未返回单据ID');
}
return this.$u.api.fpmPrcInOrder.detail({ id: this.orderId });
})
.then((detail) => {
uni.hideLoading();
const order = normalizeOrder(detail);
this.applyOrder(order, { keepWeights: true });
this.headerSaved = true;
this.clearWarehouseBin();
this.syncCanScanFromStatus(order.audit_status);
this.showSuccess('保存成功,请先扫描仓位');
this.registerOrderScanBroadcast();
})
.catch((e) => {
uni.hideLoading();
this.showError(e.message || '保存失败');
});
},
applyOrder(order, options = {}) {
if (!order) return;
this.auditStatus = order.audit_status;
const paperTubeWeight = this.form.paper_tube_weight;
const packageWeight = this.form.package_weight;
Object.assign(this.form, orderToFormFields(order));
if (options.keepWeights) {
this.form.paper_tube_weight = paperTubeWeight;
this.form.package_weight = packageWeight;
}
},
handleScan(options = {}) {
const fromPhone = !!options.fromPhone;
const code = (this.QRBarCode || '').trim();
if (!this.canScan || !this.orderId) {
this.showError('请先提交保存后再扫码');
this.QRBarCode = '';
this.phoneContinuousScan = false;
return;
}
const isDelete = this.BarCodeDelStatus;
if (!isDelete && !this.warehouseBinId) {
this.showError('请先扫描仓位');
this.QRBarCode = '';
this.phoneContinuousScan = false;
return;
}
if (!code) {
this.QRBarCode = '';
if (fromPhone) this.scheduleNextPhoneScan();
return;
}
const arrangeType = isDelete ? ARRANGE_TYPE.DEL : ARRANGE_TYPE.IN;
this.doScanUpdate(code, arrangeType, { fromPhone });
},
BarCodeDelChange(e) {
this.BarCodeDelStatus = (e.detail.value || []).length > 0;
this.BillDataMessage = this.BarCodeDelStatus ? '删除模式:扫描细码即可删除' : '';
},
getFineCodeScanCode(fineCode) {
if (!fineCode) return '';
return fineCode.qr_code
|| fineCode.long_code
|| fineCode.scan_code
|| fineCode.digital_code
|| fineCode.bar_code
|| fineCode.short_code
|| '';
},
onDeleteFineCode(fineCode) {
if (!this.canScan || !this.orderId) {
this.showError('当前状态不可删除');
return;
}
const scanCode = this.getFineCodeScanCode(fineCode);
if (!scanCode) {
this.showError('该细码缺少条码,无法删除');
return;
}
uni.showModal({
title: '提示',
content: `确认删除细码 ${scanCode}?`,
success: (res) => {
if (!res.confirm) return;
this.doScanUpdate(scanCode, ARRANGE_TYPE.DEL);
},
});
},
doScanUpdate(scanCode, arrangeType, options = {}) {
const { fromPhone = false } = options;
const isDelete = arrangeType === ARRANGE_TYPE.DEL;
if (!isDelete) {
const weightErr = validateScanWeights(this.form.paper_tube_weight, this.form.package_weight);
if (weightErr) {
this.showError(weightErr);
this.QRBarCode = '';
this.phoneContinuousScan = false;
return;
}
}
uni.showLoading({ title: isDelete ? '删除中...' : '处理中...' });
return this.$u.api.fpmPrcInOrder.scanUpdate(buildScanUpdateParams({
id: this.orderId,
scanCode,
arrangeType,
// 删除不强制仓位;录入带当前仓位
warehouseBinId: isDelete ? 0 : this.warehouseBinId,
paperTubeWeight: this.form.paper_tube_weight,
packageWeight: this.form.package_weight,
}))
.then((scanRes) => {
this.scanDetail = buildScanDetailFromScanResponse(scanRes);
this.BillDataMessage = isDelete ? '删除成功' : '扫描成功';
this.QRBarCode = '';
if (isDelete) {
speak('已删除');
uni.showToast({ title: '删除成功', icon: 'success' });
} else {
speak('成功');
}
return this.$u.api.fpmPrcInOrder.detail({ id: this.orderId })
.then((detail) => {
const order = normalizeOrder(detail);
if (order) {
this.form.item_data = order.item_data;
this.auditStatus = order.audit_status;
}
})
.catch(() => null);
})
.then(() => {
uni.hideLoading();
if (fromPhone) this.scheduleNextPhoneScan();
})
.catch((e) => {
uni.hideLoading();
this.phoneContinuousScan = false;
const message = e.message || (isDelete ? '删除失败' : '扫码失败');
this.showError(message);
this.QRBarCode = '';
});
},
handleStatusAction(action) {
confirmOrderStatusAction(this, {
orderId: this.orderId,
action,
onSuccess: () => this.$u.api.fpmPrcInOrder.detail({ id: this.orderId })
.then((detail) => {
uni.hideLoading();
const order = normalizeOrder(detail);
if (!order) return;
this.applyOrder(order);
this.syncCanScanFromStatus(order.audit_status);
this.showSuccess('操作成功');
}),
});
},
showError(message) {
util.playErrorAudio();
uni.showModal({ title: '提示', content: message, showCancel: false });
},
showSuccess(message) {
util.playSuccessAudio();
uni.showToast({ title: message, icon: 'success' });
},
},
};