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

455 lines
9.9 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" @tap="openPicker('warehouse')">
<text>{{ warehouseName || '全部仓库' }}</text>
<FaIcon name="chevron-right" color="#94a3b8" :size="12" />
</view>
<view class="chip" :class="{ active: groupByProduct }" @tap="toggleGroup">
<text>{{ groupByProduct ? '按商品汇总' : '分仓分批次' }}</text>
</view>
<view class="chip" :class="{ active: onlyStock }" @tap="toggleOnlyStock">
<text>有货</text>
</view>
</view>
<scroll-view class="state-scroll" scroll-x :show-scrollbar="false">
<view class="state-list">
<view
v-for="opt in SAFETY_STATE_OPTIONS"
:key="opt.value || 'all'"
class="state-item"
:class="{ active: safetyState === opt.value }"
@tap="onState(opt.value)"
>
<text>{{ opt.label }}</text>
</view>
</view>
</scroll-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, i) in list" :key="rowKey(row, i)" class="s-card" @tap="openProduct(row)">
<view class="s-top">
<text class="s-name">{{ row.product_name }}</text>
<u-tag
v-if="row.safety_state && row.safety_state !== 'normal'"
:text="safetyStateLabel(row.safety_state)"
:type="row.safety_state === 'low' ? 'warning' : 'primary'"
size="mini"
/>
</view>
<text class="s-sub">
{{ [row.warehouse_name, row.batch_no ? '批次 ' + row.batch_no : ''].filter(Boolean).join(' · ') || '未分仓' }}
<text v-if="row.category_name"> · {{ row.category_name }}</text>
</text>
<view class="s-grid">
<view class="s-cell">
<text class="cell-label">数量</text>
<text class="cell-value strong">{{ qtyText(row.quantity) }} {{ row.unit || '' }}</text>
</view>
<view class="s-cell">
<text class="cell-label">成本价</text>
<text class="cell-value">¥{{ moneyText(row.cost_price) }}</text>
</view>
<view class="s-cell">
<text class="cell-label">货值</text>
<text class="cell-value">¥{{ moneyText(row.amount) }}</text>
</view>
</view>
<view v-if="Number(row.safety_qty) > 0 || Number(row.max_qty) > 0" class="s-limit">
<text v-if="Number(row.safety_qty) > 0">安全 {{ qtyText(row.safety_qty) }}</text>
<text v-if="Number(row.max_qty) > 0">最高 {{ qtyText(row.max_qty) }}</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 {
SAFETY_STATE_OPTIONS,
fetchStocks,
fetchWarehouses,
moneyText,
qtyText,
safetyStateLabel
} from '@/api/erp.js'
const PAGE_SIZE = 20
const list = ref([])
const total = ref(0)
const sumAmount = ref(0)
const page = ref(1)
const loading = ref(false)
const finished = ref(false)
const keyword = ref('')
const warehouseId = ref('')
const warehouseName = ref('')
const safetyState = ref('')
const groupByProduct = ref(false)
const onlyStock = ref(true)
const warehouses = ref([])
/** 从商品详情跳过来时锁定单个商品,端上把 id 存下来随查询一起下发 */
const productId = 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 || '',
extra: `${w.stock_kinds || 0} 种`
}))
])
onLoad((query = {}) => {
if (query.warehouse_id) {
warehouseId.value = String(query.warehouse_id)
warehouseName.value = decodeURIComponent(query.warehouse_name || '')
}
if (query.product_id) productId.value = String(query.product_id)
if (query.group === 'product') groupByProduct.value = true
})
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 fetchStocks({
page: page.value,
pageSize: PAGE_SIZE,
keyword: keyword.value.trim(),
warehouseId: warehouseId.value,
productId: productId.value,
groupBy: groupByProduct.value ? 'product' : '',
safetyState: safetyState.value,
onlyStock: onlyStock.value
})
const rows = data.list || []
list.value = page.value === 1 ? rows : [...list.value, ...rows]
total.value = data.total || 0
sumAmount.value = data.sumAmount || 0
finished.value = list.value.length >= total.value
} catch (e) {
if (page.value > 1) page.value -= 1
} finally {
loading.value = false
}
}
function clearKeyword() {
keyword.value = ''
refresh()
}
function toggleGroup() {
groupByProduct.value = !groupByProduct.value
refresh()
}
function toggleOnlyStock() {
onlyStock.value = !onlyStock.value
refresh()
}
function onState(value) {
if (safetyState.value === value) return
safetyState.value = value
refresh()
}
function openPicker() {
picker.value.show = true
}
function onWarehouse(opt) {
warehouseId.value = String(opt.value ?? '')
warehouseName.value = warehouseId.value ? opt.label : ''
refresh()
}
function rowKey(row, i) {
return `${row.product_id}-${row.warehouse_id || 0}-${row.batch_no || ''}-${i}`
}
function openProduct(row) {
if (!row.product_id) return
uni.navigateTo({ url: `/pages/erp/basic/product-detail?id=${row.product_id}` })
}
</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;
gap: 16rpx;
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;
&.active {
background: #2563eb;
color: #ffffff;
}
}
.state-scroll {
margin-top: 16rpx;
white-space: nowrap;
/* scroll-view 不设 border-box,左右内边距会把最后几个标签顶出屏幕 */
box-sizing: border-box;
}
.state-list {
display: inline-flex;
gap: 14rpx;
padding-bottom: 10rpx;
}
.state-item {
padding: 8rpx 20rpx;
border-radius: $radius-full;
background: $color-card;
font-size: 23rpx;
color: $color-text-muted;
&.active {
background: $color-primary-bg;
color: $color-primary-dark;
font-weight: 500;
}
}
.sum-bar {
display: flex;
justify-content: space-between;
margin: 0 $page-padding-x 16rpx;
padding: 0 8rpx;
}
.sum-item {
font-size: 24rpx;
color: $color-text-muted;
}
.s-card {
@include card;
margin: 0 $page-padding-x 20rpx;
padding: 24rpx 28rpx;
&:active {
opacity: 0.85;
}
}
.s-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
}
.s-name {
flex: 1;
font-size: 29rpx;
font-weight: 500;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.s-sub {
display: block;
margin-top: 8rpx;
font-size: 23rpx;
color: $color-text-muted;
}
.s-grid {
display: flex;
margin-top: 18rpx;
padding-top: 16rpx;
border-top: 1rpx solid $color-divider;
}
.s-cell {
flex: 1;
min-width: 0;
.cell-label {
display: block;
font-size: 22rpx;
color: $color-text-muted;
}
.cell-value {
display: block;
margin-top: 6rpx;
font-size: 26rpx;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&.strong {
font-weight: 600;
color: $color-primary-dark;
}
}
}
.s-limit {
display: flex;
gap: 24rpx;
margin-top: 12rpx;
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>