Files
crm_uiapp/src/utils/scan.ts

86 lines
3.2 KiB
TypeScript
Raw Normal View History

/**
*
*
*/
export interface ScanItem {
warehouseId: number
productId: number
count: number
orderItemId?: number
}
/**
*
*
* @param raw
* @return
*
*/
export function normalizeScanCode(raw: string): string {
return (raw || '').replace(/[\r\n]/g, '').trim()
}
/**
*
* `uni.scanCode`
* @param result uni-app
* @return
*
*/
export function normalizeCameraScanResult(result?: { result?: string } | null): string {
return normalizeScanCode(result?.result || '')
}
/**
*
* +
* @param item
* @param withOrderConstraint
* @return
*
*/
export function buildScanMergeKey(item: ScanItem, withOrderConstraint = false): string {
const segments = [item.warehouseId, item.productId]
// 订单约束扫码时需要把订单明细编号带入合并键,避免不同订单行误合并。
if (withOrderConstraint) {
segments.push(item.orderItemId || 0)
}
return segments.join(':')
}
/**
*
*
* @param items
* @param scannedItem
* @param withOrderConstraint
* @return
*
*/
export function mergeScannedItem(
items: ScanItem[],
scannedItem: ScanItem,
withOrderConstraint = false,
): ScanItem[] {
const targetKey = buildScanMergeKey(scannedItem, withOrderConstraint)
const existedIndex = items.findIndex(item => buildScanMergeKey(item, withOrderConstraint) === targetKey)
if (existedIndex === -1) {
return [...items, scannedItem]
}
return items.map((item, index) => {
if (index !== existedIndex) {
return item
}
// 命中同一合并键时只累计数量,避免把已录入明细拆成多行,影响 PDA 连续扫码效率。
return {
...item,
count: Number(item.count || 0) + Number(scannedItem.count || 0),
}
})
}