Files
yunzerwebsiteallinone/uniapp/pages/erp/stock/batch.vue
T
2026-10-19 01:13:17 +08:00

448 lines
9.5 KiB
Vue

<template>
<view class="page">
<view class="filter-bar">
<view class="search-box">
<FaIcon name="magnifying-glass" color="#94a3b8" :size="16" />
<input
v-model="keyword"
class="search-input"
placeholder="搜索商品名称 / 编码 / 批次"
confirm-type="search"
@confirm="refresh"
/>
<text v-if="keyword" class="search-clear" @tap="clearKeyword">清空</text>
</view>
<view class="chip-row">
<view class="chip" :class="{ active: warehouseId }" @tap="openWarehouse">
<text>{{ warehouseName || '全部仓库' }}</text>
</view>
<view class="chip" :class="{ active: onlyPositive }" @tap="toggleOnly">
<text>仅有库存</text>
</view>
</view>
<view class="chip-row">
<view
v-for="opt in EXPIRE_STATE_OPTIONS"
:key="opt.value || 'all'"
class="chip mini"
:class="{ active: expireState === opt.value }"
@tap="onState(opt.value)"
>
<text>{{ opt.label }}</text>
</view>
</view>
</view>
<view class="sum-bar">
<text class="sum-item">共 {{ total }} 个批次</text>
<text class="sum-item">本页货值 ¥{{ moneyText(sumAmount) }}</text>
</view>
<view v-if="loading && !list.length" class="state-box">
<text class="state-text">加载中...</text>
</view>
<view v-else-if="!list.length" class="state-box">
<u-empty mode="search" text="没有符合条件的批次" />
</view>
<template v-else>
<view v-for="row in list" :key="batchKey(row)" class="b-card" @tap="openLedger(row)">
<view class="b-top">
<text class="b-name">{{ row.product_name }}</text>
<u-tag
v-if="expireLabel(row)"
:text="expireLabel(row)"
:type="row.expired ? 'error' : 'warning'"
size="mini"
/>
</view>
<view class="b-batch">
<FaIcon name="tags" color="#2563eb" :size="12" />
<text class="b-no">{{ row.batch_no }}</text>
<text class="b-wh">{{ row.warehouse_name || '未分仓' }}</text>
</view>
<text class="b-sub">{{ [row.product_code, row.spec].filter(Boolean).join(' · ') }}</text>
<view class="b-grid">
<view class="b-cell">
<text class="cell-label">数量</text>
<text class="cell-value strong">{{ qtyText(row.quantity) }} {{ row.unit || '' }}</text>
</view>
<view class="b-cell">
<text class="cell-label">成本</text>
<text class="cell-value">¥{{ moneyText(row.cost_price) }}</text>
</view>
<view class="b-cell">
<text class="cell-label">货值</text>
<text class="cell-value">¥{{ moneyText(row.amount) }}</text>
</view>
<view class="b-cell">
<text class="cell-label">到期</text>
<text class="cell-value">{{ row.expire_date || '—' }}</text>
</view>
</view>
<view class="b-hint">
<FaIcon name="clock-rotate-left" color="#94a3b8" :size="12" />
<text>看这个批次的收发流水</text>
</view>
</view>
<view v-if="finished" class="load-done">
<text>{{ list.length ? '没有更多了' : '' }}</text>
</view>
</template>
<ErpPicker
:show="picker.show"
title="选择仓库"
placeholder="搜索仓库"
:options="warehouseOptions"
:model-value="warehouseId"
@update:show="picker.show = false"
@select="onWarehouse"
/>
</view>
</template>
<script setup>
/**
* 批次与效期台账。批次只从入库流水回溯到期日,未登记效期的批次到期列留空不算异常。
* 点行跳到该商品+该批次的收发流水,做效期倒查不用再手动筛。
*/
import { computed, ref } from 'vue'
import { onLoad, onReachBottom, onShow } from '@dcloudio/uni-app'
import FaIcon from '@/components/FaIcon.vue'
import ErpPicker from '@/components/erp/ErpPicker.vue'
import { EXPIRE_STATE_OPTIONS, fetchBatches, fetchWarehouses, moneyText, qtyText } from '@/api/erp.js'
const PAGE_SIZE = 20
const list = ref([])
const total = ref(0)
const page = ref(1)
const loading = ref(false)
const finished = ref(false)
const keyword = ref('')
const warehouseId = ref('')
const warehouseName = ref('')
const expireState = ref('')
const onlyPositive = ref(true)
const warehouses = ref([])
const picker = ref({ show: false })
const warehouseOptions = computed(() => [
{ value: '', label: '全部仓库' },
...warehouses.value.map((w) => ({
value: w.id,
label: w.warehouse_name,
sub: w.warehouse_code || ''
}))
])
onLoad((query = {}) => {
if (query.warehouse_id) {
warehouseId.value = String(query.warehouse_id)
warehouseName.value = decodeURIComponent(query.warehouse_name || '')
}
if (query.expire_state) expireState.value = String(query.expire_state)
})
onShow(() => {
refresh()
loadWarehouses()
})
onReachBottom(() => {
if (finished.value || loading.value) return
loadMore()
})
async function loadWarehouses() {
if (warehouses.value.length) return
try {
warehouses.value = await fetchWarehouses({ onlyEnabled: true })
} catch (e) {
// 仓库字典拉不到只是不能按仓筛
}
}
async function refresh() {
page.value = 1
finished.value = false
list.value = []
await loadPage()
}
async function loadMore() {
page.value += 1
await loadPage()
}
async function loadPage() {
loading.value = true
try {
const data = await fetchBatches({
page: page.value,
pageSize: PAGE_SIZE,
keyword: keyword.value.trim(),
warehouseId: warehouseId.value,
expireState: expireState.value,
onlyPositive: onlyPositive.value
})
const rows = data.list || []
list.value = page.value === 1 ? rows : [...list.value, ...rows]
total.value = data.total || 0
finished.value = list.value.length >= total.value
} catch (e) {
if (page.value > 1) page.value -= 1
} finally {
loading.value = false
}
}
const sumAmount = computed(() => list.value.reduce((sum, r) => sum + (Number(r.amount) || 0), 0))
function clearKeyword() {
keyword.value = ''
refresh()
}
function toggleOnly() {
onlyPositive.value = !onlyPositive.value
refresh()
}
function onState(value) {
if (expireState.value === value) return
expireState.value = value
refresh()
}
function openWarehouse() {
picker.value.show = true
}
function onWarehouse(opt) {
warehouseId.value = String(opt.value ?? '')
warehouseName.value = warehouseId.value ? opt.label : ''
refresh()
}
function expireLabel(row) {
if (row.expired) return '已过期'
const d = row.expire_in_days
if (d === null || d === undefined || String(d) === '') return ''
return d <= 30 ? `${d} 天后到期` : ''
}
function batchKey(row) {
return `${row.product_id}-${row.warehouse_id}-${row.batch_no}`
}
function openLedger(row) {
const q = `product_id=${row.product_id}&warehouse_id=${row.warehouse_id || ''}&batch_no=${encodeURIComponent(
row.batch_no || ''
)}`
uni.navigateTo({ url: `/pages/erp/stock/ledger?${q}` })
}
</script>
<style lang="scss" scoped>
@import '@/src/styles/page-common.scss';
.page {
min-height: 100vh;
background: $color-bg-page;
padding-bottom: 40rpx;
}
.filter-bar {
position: sticky;
top: 0;
z-index: 10;
background: $color-bg-page;
padding: 20rpx $page-padding-x 8rpx;
}
.search-box {
display: flex;
align-items: center;
gap: 12rpx;
height: 72rpx;
padding: 0 24rpx;
background: $color-card;
border-radius: $radius-full;
box-shadow: $shadow-card;
.search-input {
flex: 1;
font-size: 26rpx;
color: $color-text;
}
.search-clear {
font-size: 24rpx;
color: $color-text-muted;
}
}
.chip-row {
display: flex;
flex-wrap: wrap;
gap: 14rpx;
margin-top: 18rpx;
}
.chip {
display: inline-flex;
align-items: center;
gap: 6rpx;
padding: 10rpx 22rpx;
border-radius: $radius-full;
background: $color-card;
font-size: 24rpx;
color: $color-text-secondary;
box-shadow: $shadow-card;
&.mini {
padding: 8rpx 18rpx;
font-size: 22rpx;
box-shadow: none;
background: #f1f5f9;
}
&.active {
background: #2563eb;
color: #ffffff;
}
}
.sum-bar {
display: flex;
justify-content: space-between;
margin: 6rpx $page-padding-x 16rpx;
padding: 0 8rpx;
}
.sum-item {
font-size: 24rpx;
color: $color-text-muted;
}
.b-card {
@include card;
margin: 0 $page-padding-x 20rpx;
padding: 24rpx 28rpx;
&:active {
opacity: 0.85;
}
}
.b-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
}
.b-name {
flex: 1;
min-width: 0;
font-size: 29rpx;
font-weight: 500;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.b-batch {
display: flex;
align-items: center;
gap: 10rpx;
margin-top: 12rpx;
}
.b-no {
padding: 4rpx 14rpx;
border-radius: $radius-sm;
background: $color-primary-bg;
color: $color-primary-dark;
font-size: 23rpx;
font-weight: 500;
}
.b-wh {
font-size: 23rpx;
color: $color-text-secondary;
}
.b-sub {
display: block;
margin-top: 8rpx;
font-size: 22rpx;
color: $color-text-muted;
}
.b-grid {
display: flex;
gap: 10rpx;
margin-top: 18rpx;
padding-top: 16rpx;
border-top: 1rpx solid $color-divider;
}
.b-cell {
flex: 1;
min-width: 0;
.cell-label {
display: block;
font-size: 21rpx;
color: $color-text-muted;
}
.cell-value {
display: block;
margin-top: 6rpx;
font-size: 24rpx;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&.strong {
font-weight: 600;
color: $color-primary-dark;
}
}
}
.b-hint {
display: flex;
align-items: center;
gap: 8rpx;
margin-top: 16rpx;
font-size: 21rpx;
color: $color-text-muted;
}
.load-done {
display: flex;
justify-content: center;
padding: 8rpx 0 24rpx;
font-size: 24rpx;
color: $color-text-muted;
}
.state-box {
display: flex;
flex-direction: column;
align-items: center;
padding: 120rpx 40rpx;
}
.state-text {
font-size: 28rpx;
color: $color-text-muted;
}
</style>